从元组返回随机项目-discord.py

时间:2020-10-11 05:04:53

标签: python python-3.x discord discord.py

我试图使用命令$ random并从元组中发送一个随机项。该代码应该可以工作,但是,我遇到了错误。我试过将元组和random.choice放在bot.command之前,它可以工作并且不会产生错误,但是如果我再次运行该程序,它只会生成一个随机项。它将选择一个项目并简单地返回该项目,而不是每次都返回一个随机项目。

@bot.command(name='random', help='Returns a random, interesting fact.')
async def random(ctx):
    random_messages = ("McDonald’s once made bubblegum-flavored broccoli.",
                       "Some fungi create zombies, then control their minds.",
                       "The first oranges weren’t orange. They were green.")
    random.choice(random_messages)
    await ctx.send(random_messages)

错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Command' object has no attribute 'choice'

编辑:我需要为该函数使用一个与随机模块不同的名称。另外,我通过将random_messages分配给random.choice()命令来更新它。

工作代码:

@bot.command(name='random', help='Returns a random, interesting fact.')
async def randomfact(ctx):
    random_messages = ("McDonald’s once made bubblegum-flavored broccoli.",
                       "Some fungi create zombies, then control their minds.",
                       "The first oranges weren’t orange. They were green.")
    random_messages = random.choice(random_messages)
    await ctx.send(random_messages)

0 个答案:

没有答案
相关问题