Discord bot上的函数已定义的错误

时间:2019-04-09 08:30:17

标签: python discord

通过添加新命令从第一个Discord机器人构建。创建第二个命令时,即使代码位于不同的标题下,我也会在代码中先前收到错误“功能已定义”。

@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行(在第一个函数中)已经定义了异步操作

1 个答案:

答案 0 :(得分: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)