python - 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)
解决方案
我一开始也犯了同样的错误。
@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)
尚未测试,但这应该可以解决您的问题。
推荐阅读
- python - 在 python 中,有没有比 for 循环更快的方法来标记矩阵(3D 数组)?
- c# - 如何在这段代码中用 do-while 替换 while?
- postgresql - 在另一个表中插入一个表中的值作为外键
- javascript - Javascript 根据值从下拉列表中获取选项
- c - 从 8 位到 32 位的位复制
- php - PHP 和 MySQL,使用外键从另一个表中获取数据
- android - 使用 Constraintlayout 和 dp,实际设备中布局大小不同
- java - @CreationTimestamp 和 @UpdateTimestamp 不适用于 JPA 2.0.2
- c# - 如何为 Swagger UI 定义参数的默认值?
- arrays - 添加新值时使用 onEdit(e) 更新单元格