【问题标题】:Random.choice inside an embed in discord.py not working嵌入 discord.py 中的 Random.choice 不起作用
【发布时间】:2021-06-04 17:47:31
【问题描述】:

我对 client.command 做了同样的事情,并且它有效。但是,我想使用 ?rate me 而不是 ?rateme,因此决定使用 on_message。我没有看到我的代码有任何问题,并且它的执行没有任何错误。但是当我输入 ?rate me in discord 时,没有任何反应。如果有人能帮助我找出问题所在,将不胜感激。

async def on_message(message):
    if message.content.startswith('?rate me'):
        async with message.channel.typing():
            variable_list =[
                '1',
                '2',
                '3',
                '4'
            ]

            embed = discord.Embed(colour=0xc81f9f,
                title="Rating",
                description=f"{message.author.mention} is a {(random.choice(variable_list))}"
            )
            embed.set_footer(text=f"{message.guild.name}")
            embed.timestamp = datetime.datetime.utcnow()

            await message.channel.send(embed=embed) 

【问题讨论】:

  • description=f"{message.author.mention} is a {(random.choice(variable_list))}") 替换成这个,你有一个括号没有任何理由。
  • 尝试在 on_message 中打印一些内容,看看是否可行。
  • 你随机导入了吗?
  • @AdityaTomar 是的,我已经随机导入了。
  • @Karan 对不起,我看不出你在声明中做了什么改变。

标签: python discord.py


【解决方案1】:

您似乎没有将您的函数标记为event。 请使用@client.event@bot.event@commands.Cog.listener。 如果没有这个定义,它将无法工作。 我还建议您重新构建代码以更快地检测错误。我的代码如下所示:

import discord
import random

@client.event / @bot.event / @commands.Cog.listener # Make it as an event that fires if the conditions are True
async def on_message(message):
    if message.content.startswith("?rate me"):
        async with message.channel.typing():
            variable_list = ['1', '2', '3', '4']

            embed = discord.Embed(color=0xc81f9f,
                                  title="Rating")
            embed.description = f"{message.author.mention} is a {(random.choice(variable_list))}" # Set description
            embed.set_footer(text=f"{message.guild.name}")
            embed.timestamp = datetime.datetime.utcnow()

            await message.channel.send(embed=embed)

如果您已经有一个现有的on_message 事件,只需将没有第一部分的代码放入现有事件中,然后您就有了:

    if message.content.startswith("?rate me"):
        async with message.channel.typing():
            variable_list = ['1', '2', '3', '4']

            embed = discord.Embed(color=0xc81f9f,
                                  title="Rating")
            embed.description = f"{message.author.mention} is a {(random.choice(variable_list))}" # Set description
            embed.set_footer(text=f"{message.guild.name}")
            embed.timestamp = datetime.datetime.utcnow()

            await message.channel.send(embed=embed)

(这是因为我们这里不能有多个on_message-event。)

【讨论】:

  • 我确实使用了@client.event,就像我将它用于似乎工作正常的所有其他事件一样。除了这个。我什至复制粘贴了你的整个代码,但它似乎不起作用。 on_ready 触发,然后什么也没有发生。
  • 您的代码中是否已有on_message 事件?
  • 啊,是的,我愿意。我有一个返回随机笑话的 on_message 事件。我的代码中不应该有多个 on_message 事件吗?
  • 不,我不建议这样做。将我发送的代码放入现有事件中。 (见我答案末尾的代码)
  • 该死的,是的,我就是这么做的,而且成功了。但是,当我们可以使用多个@client.command 时,为什么我们不能使用多个 on_message 事件呢?此外,目前,代码看起来很乱。我可以在另一个 py 文件中编写另一个 on_message 事件并在我的主文件中调用它吗?如果是这样,我该怎么做?
猜你喜欢
  • 2022-01-20
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 2021-04-28
  • 2021-10-15
  • 2018-10-04
  • 2021-02-25
  • 2021-07-28
相关资源
最近更新 更多