首页 > 解决方案 > Discord Bot 不响应命令(Python)

问题描述

我刚刚开始写不和谐的机器人。在尝试遵循在线说明和教程时,我的机器人不会响应命令。它对 on_message() 的响应非常好,但无论我尝试什么,它都不会响应命令。我敢肯定这很简单,但我会很感激你的帮助。

import discord
from discord.ext.commands import Bot
from discord.ext import commands

bot = commands.Bot(command_prefix='$')
TOKEN = '<token-here>'

@bot.event
async def on_ready():
    print(f'Bot connected as {bot.user}')
    
@bot.event
async def on_message(message):
    if message.content == 'test':
        await message.channel.send('Testing 1 2 3')
        
@bot.command(name='go')
async def dosomething(ctx):
    print("command called") #Tried putting this in help in debugging
    await message.channel.send("I did something")


        
bot.run(TOKEN)

我提示机器人和结果的图片

标签: pythoncommanddiscordbots

解决方案


我一开始也犯了同样的错误。

@bot.event
async def on_message(message):
    if message.content == 'test':
        await message.channel.send('Testing 1 2 3')

此函数覆盖 on_message 事件,因此它永远不会发送到 bot.command()

要修复它,您只需在 on_message 函数的末尾添加 await bot.process_commands(message) :

async def on_message(message):
    if message.content == 'test':
        await message.channel.send('Testing 1 2 3')
    await bot.process_commands(message)

尚未测试,但这应该可以解决您的问题。


推荐阅读