我是python新手,如果这是一个noob问题,我会很抱歉地制作discord机器人,但我被卡住了
几个小时来一直在想办法
我正在写一个简单的机器人,它将循环遍历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)
相关问题 更多 >
编程相关推荐