简体   繁体   English

Discord 机器人不响应命令(Python)

[英]Discord Bot Not Responding to Commands (Python)

I've just gotten into writing discord bots.我刚刚开始编写 discord 机器人程序。 While trying to follow online instructions and tutorials, my bot would not respond to commands.在尝试按照在线说明和教程进行操作时,我的机器人不会响应命令。 It responded perfectly fine to on_message(), but no matter what I try it won't respond to commands.它对 on_message() 的响应非常好,但无论我尝试什么,它都不会响应命令。 I'm sure it's something simple, but I would appreciate the help.我确信这很简单,但我会很感激你的帮助。

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)

Picture of me prompting the bot and the results我提示机器人的图片和结果

I made the same mistake at first.我一开始也犯了同样的错误。

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

This function overiding the on_message event so it is never sent to bot.command()此函数覆盖 on_message 事件,因此它永远不会发送到 bot.command()

To fix it you just have to add await bot.process_commands(message) at the end of the on_message function:要修复它,您只需在 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)

Haven't tested yet but that should fix your issue.尚未测试,但这应该可以解决您的问题。

Ok.好的。 First of all, the only import statement that you need at the top is from discord.ext import commands .首先,顶部唯一需要的 import 语句是from discord.ext import commands The other two are not necessary.另外两个不是必须的。

Second of all, I tried messing around with your code myself and found that the on_message() function seems to interfere with the commands so taking that out should help.其次,我尝试自己弄乱您的代码,发现on_message()函数似乎干扰了命令,因此将其删除应该会有所帮助。

Third of all, I only found this out when I duplicated one of my own working bots and slowly changed all of the code until it was identical to yours.第三,我只是在复制我自己的一个工作机器人并慢慢更改所有代码直到它与您的完全相同时才发现这一点。 For some reason python didn't like it when I just copied and pasted your code.出于某种原因,当我刚刚复制并粘贴您的代码时,python 不喜欢它。 I have never seen something like this before so I honestly don't know what to say other than that your code is correct and should work as long as you take the on_message() function out.我以前从未见过这样的事情,所以老实说,除了你的代码是正确的并且只要你on_message()函数就应该可以工作,我不知道该说什么。

Here's the final code that I got working:这是我开始工作的最终代码:

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.command()
async def dosomething(ctx):
    await ctx.send("I did something")

bot.run(TOKEN)

As you can see the only things that I have changed from your code are that I removed the redundant imports at the top, and I deleted the on_message() function.正如您所看到的,我从您的代码中更改的唯一内容是我删除了顶部的冗余导入,并删除了on_message()函数。 It works perfectly like this on my end so I would suggest that you re-type it out like this in a new file and see if that works.它在我这边工作得非常完美,所以我建议你在一个新文件中像这样重新输入它,看看它是否有效。

If that doesn't work for you then my next guess would be that there is a problem with your installation of discord.py so you might try uninstalling it and then reinstalling it.如果这对您不起作用,那么我的下一个猜测是您安装的discord.py存在问题,因此您可以尝试卸载它然后重新安装它。

If none of that helps let me know and I will see if I can help you find anything else that might be the cause of the problem.如果这些都没有帮助,请告诉我,我会看看是否可以帮助您找到可能导致问题的其他任何原因。

just put client.process_commands(message)只需将 client.process_commands(message)

in on_message event at last..最后在 on_message 事件中..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM