我是python的新手,正在制作不和谐的bot,所以很抱歉,如果这是一个菜鸟问题,但我遇到了麻烦。
一直试图弄清楚几个小时。
我正在编写一个简单的机器人,它将遍历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)
是否重要。
问题在于您的函数名称和列表名称都是maps
。因此,当您运行时choice = random.sample(list(maps,4)
,它认为您是在指功能maps
而不是列表。为了解决这个问题,您要么想要
更改函数的名称(也更改命令的名称)。您可以通过更改async def maps(ctx):
为async def newCommandName(ctx):
(使用所需的函数新名称更改newCommandName)来完成此操作。
更改maps
列表的名称。我不知道这个定义在哪里,但是我会假设它是这样的maps = []
。取而代之的是,您需要将名称更改为类似的名称mapsList
,然后更改要使用的列表的所有引用mapsList
。
另外,作为旁注,choice = random.sample(list(maps,4)
应choice = random.sample(list(maps),4)
改为。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句