【发布时间】:2021-01-19 02:28:55
【问题描述】:
我是 python 新手,如果这是一个菜鸟问题,我很抱歉制作不和谐的机器人,但我被困住了。
花了好几个小时才弄明白。
我正在编写一个简单的机器人,它将遍历 28 个对象的列表并随机选择其中的 4 个。然后将这 4 个选项发送到聊天中,以便人们为他们的选择投票。
昨晚我在使用
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!maps'):
await message.delete()
channel = client.get_channel(800086126173225010)
await channel.send('**New poll! Vote below now for tomorrow\'s map!**')
choice = random.sample(maps,4)
for x in range(0, 4):
mapemp.append(emoji[x]+" - "+choice[x])
msg = await channel.send('\n\n'.join(mapemp))
for x in range(0,4):
await msg.add_reaction(emoji[x])
mapemp.clear()
这很好用。但后来我发现了@bot.command 而不是@client.event 所以我试图切换到那个。但是,当我尝试运行它返回的命令时
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'Command' object is not iterable
@bot.command(pass_context=True)
async def maps(ctx):
await ctx.message.delete()
channel = bot.get_channel(800086126173225010)
await channel.send('**New poll! Vote below now for tomorrow\'s map!**')
choice = random.sample(list(maps,4)
for x in range(0, 4):
mapemp.append(emoji[x]+" - "+choice[x])
msg = await channel.send('\n\n'.join(mapemp))
for x in range(0,4):
await msg.add_reaction(emoji[x])
mapemp.clear()
是什么让@bot.command 与@client.event 如此不同以至于我无法重复选择?
我以前没有random.sample(list(maps,4),但是当我尝试只使用random.sample(maps,4) 运行它时,我得到了一个不同的错误。
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: Population must be a sequence or set. For dicts, use list(d).
所以我把它改成了random.sample(list(maps,4),如果这很重要的话。
【问题讨论】:
-
把
random.sample(list(maps,4)改成random.sample(list(maps,4))? -
改成
choice = random.sample(list(maps),4)discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'Command' object is not iterable` -
当我尝试
random.sample(list(maps,4))它返回discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: list() takes at most 1 argument (2 given)
标签: python python-3.x discord discord.py discord.py-rewrite