功能已在Discord bot上定义错误

问题描述 投票:0回答:1

通过添加新命令从第一个Discord bot构建。在创建第二个命令时,我在代码中得到错误“函数已定义”,即使它位于不同的标题下。

@client.command()
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('!hello'):
        msg = 'Hello {0.author.mention}'.format(message)
        await client.send_message(message.channel, msg)

My first command above, worked fine.

@client.command()
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('!roll'):
        msg = 'So, {0.author.mention} you rolled for a {}'.format(random.randint(1,20))
        await client.send_message(message.channel, msg)

第二个有一个错误,说先前已在第10行定义了异步(在第一个函数中)

python discord
1个回答
0
投票

通过使用def,您正在定义一个函数。函数必须是唯一的,并且不能使用相同的名称定义两次。

解决方案可以

@client.command()
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('!hello'):
        msg = 'Hello {0.author.mention}'.format(message)
        await client.send_message(message.channel, msg)
    elif message.content.startswith('!roll'):
        msg = 'So, {0.author.mention} you rolled for a {}'.format(random.randint(1,20))
        await client.send_message(message.channel, msg)
© www.soinside.com 2019 - 2024. All rights reserved.