根据用户消息创建嵌入

时间:2021-06-22 17:49:10

标签: python discord discord.py

我对 discord.py 很陌生,所以我在这里发布。如何发出命令,以便当用户说 !embed (message here) #channelhere 时,它​​将用户消息转换为嵌入,然后将其发布到指定的频道?一个解释会很好,所以我可以尝试理解它。

3 个答案:

答案 0 :(得分:0)

我拥有的示例代码:

例如:我输入 !embed hello / wow

@bot.command() #used before a command
async def embed(ctx, *, content: str): #when the person uses !embed it will do what is under.  Ctx is needed to store the channel of the message, author of message and other properties. content is the argument, or what you put after the !embed (in this case hello / wow)- we state that it is a string(str) or plain text if that is more clear
    title, description= content.split('/') #the title and description of the embed are separated by "/" In this cas, title is "hello",description is "wow"
    embed = discord.Embed(title=title, description=description, color=0x00ff40) #making the embed (MUST HAVE TITLE AND DESCRIPTION)
    await ctx.send(embed=embed) #sending the embed in THE SAME CHANNEL YOU SENT THE COMMAND IN (as shown via ctx)
    await ctx.message.delete() #deleting the original message by you to clean the channel up(as shown by ctx.message)

就特定渠道而言,可能会稍微复杂一些:但是这应该可以完美运行

答案 1 :(得分:0)

基本上,您必须创建一个名为 channel 的变量,其中包含频道 ID,例如:channel = bot.get_channel(636399538650742795)。然后你创建一个带参数的命令,这个参数是要嵌入的消息,你将参数存储在一个变量中,创建一个名为 embed 的变量,这个变量将是 embed = discord.Embed(value=argument),我们将用 await channel.send(embed=embed) 发送。实际代码如下所示:

@bot.command()
async def embed(ctx, argument: str):
    channel = bot.get_channel(636399538650742795)
    embed = discord.Embed()
    embed.add_field(name="Message", value=argument)
    await channel.send(embed=embed)

要获取频道 ID,只需右键单击要获取其 ID 的频道,然后单击复制 ID。考虑到您对 discord.py 非常陌生,我使用 @bot.command() 因为我用 bot = commands.Bot(command_prefix="$")

定义它

答案 2 :(得分:0)

根据我看到的 2 个答案,您可以通过将频道转换为频道 ID 来将嵌入内容发送到特定频道。

代码如下:

@client.command()
async def echoembed(ctx, channel : discord.TextChannel, *, Text : str):        # full command (!echoembed #channelhere Title here | Descrpition here)
    Title, Description = Text.split(' | ')                                     # You need to split the arguements using |, change to other symbols whatever you want
    channel_id = channel.id                                                    # this will convert the channel name into the channel id
    channel_send = client.get_channel(channel_id)                              # we will get the channel using the channel id
    embed = discord.Embed(title = Title, description = Description, color = 0x2F3136)
    embed.timestamp = datetime.datetime.utcnow()                               # this will tell the time today
    embed.set_footer(text = "\u200b", icon_url = ctx.author.avatar_url)        # the \u200b is an empty character
    await channel_send.send(embed = embed)                                     # and finally, we can send the embed

如果您是 discord.py 的新手,我建议您在进入复杂的内容之前需要阅读文档。

文档将被链接 Here

稍后谢谢我 :D