py py无法识别命令

时间:2020-03-20 10:58:34

标签: python discord

client.event运行正常,连接和消息已注册。但是当我输入'.ping'时无法辨认命令 anaconda发行版上的python 3.7

    import os
    import discord
    from discord.ext import commands
    from dotenv import load_dotenv
    load_dotenv('.env.txt')
    TOKEN = os.getenv('DISCORD_TOKEN')


    client = commands.Bot(command_prefix = '.')

    @client.event
    async def on_ready():
        print(f'{client.user} has connected to Discord!')

    @client.event
    async def on_message(message):
        print(f'{client.user} has sent a message')

    @client.command()
    async def ping(ctx):
        print('test')
        await ctx.send('test') 

    client.run(TOKEN)

1 个答案:

答案 0 :(得分:2)

好的,您需要了解,覆盖默认的on_message会禁止运行任何其他命令。

一个简单的解决方法是在on_message事件的末尾添加一个client.process_commands(message)。 因此,在您的情况下:

    import discord
    from discord.ext import commands
    from dotenv import load_dotenv
    load_dotenv('.env.txt')
    TOKEN = os.getenv('DISCORD_TOKEN')


    client = commands.Bot(command_prefix = '.')

    @client.event
    async def on_ready():
        print(f'{client.user} has connected to Discord!')

    @client.event
    async def on_message(message):
        print(f'{client.user} has sent a message')
        client.process_commands(message)

    @client.command()
    async def ping(ctx):
        print('test')
        await ctx.send('test') 

    client.run(TOKEN)```