如何让Bot发送随机的预定义嵌入消息?

时间:2020-06-25 10:07:21

标签: discord.py

每当我运行命令时,它都会从字面上响应文本,而不是嵌入文本。这是我的代码:

@client.command()
async def item(ctx):
    items = [
    'embed = discord.Embed(title = "embed1", description = "test1")', 
    'embed = discord.Embed(title = "embed2", description = "test2")']
    randomitem = random.choice(items)
    await ctx.send (randomitem)

2 个答案:

答案 0 :(得分:0)

尝试一下:

@client.command()
async def item(ctx):
    items = [
    discord.Embed(title = "embed1", description = "test1"), 
    discord.Embed(title = "embed2", description = "test2")]
    randomitem = random.choice(items)
    await ctx.send(embed=randomitem) # embed= - for send discord.Embed

答案 1 :(得分:0)

items列表中填充了字符串,而不是实际的嵌入对象。 因此要获得所需的结果,您需要像这样删除嵌入对象周围的引号:

items = [ embed = discord.Embed(title = "embed1", description = "test1"), 
          embed = discord.Embed(title = "embed2", description = "test2")]

由于引号,Python只是将它们视为字符串。

编写代码的更好方法是

items = [discord.Embed(title = "embed1", description = "test1"),
         discord.Embed(title = "embed2", description = "test2")]
return ctx.send(embed=random.choice(items))