我目前正在开发一个笑话不和谐机器人,但我仍然需要一些功能。我有一些命令,除非我有“ on message”事件,否则它们都可以正常工作。 “ on message”事件工作正常,但命令无效。我做错了什么吗?我非常困惑,因为过去我曾经进行过“消息传递”事件和命令,并且它们工作得很好。我的代码如下。
import discord
from discord.ext import commands
TOKEN = 'token'
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_ready():
print(f'Logged in as: {bot.user.name}')
print(f'With ID: {bot.user.id}')
@bot.command()
async def ping(ctx):
await ctx.send('Pong! Latency: {0}'.format(round(bot.latency, 1)))
@bot.event
async def on_message(message):
if message.channel.id == 751679038841553008:
if message.author == bot.user:
return
else:
if ''.join(message.content.split()).lower()== "egg":
return
else:
await message.channel.send("{} You fool. You absolute buffoon, it is illegal to say anything other than 'egg' in this server. I hope you feel the shame in side you. Us only saying 'egg' in this channel brings peace to our server, and you thinking your above everyone? above ME? You {}, have messed up. I want you to take a long time to reflect your self.".format(message.author.mention, message.author.mention))
else:
return
bot.run(TOKEN)
因此,总而言之,似乎“ on message”事件和命令不想同时工作。
我非常清楚我的“在线留言”事件没有多大意义,这是我和朋友之间的一个玩笑。
答案 0 :(得分:1)
覆盖提供的默认on_message()
禁止运行任何其他命令。要解决此问题,请在bot.process_commands(message)
事件的末尾添加on_message()
行。
@bot.event
async def on_message(message):
await bot.process_commands(message)
if message.channel.id == 751679038841553008:
if message.author == bot.user:
return
else:
if ''.join(message.content.split()).lower() == "egg":
return
else:
await message.channel.send(
"{} You fool. You absolute buffoon, it is illegal to say anything other than 'egg' in this server. I hope you feel the shame in side you. Us only saying 'egg' in this channel brings peace to our server, and you thinking your above everyone? above ME? You {}, have messed up. I want you to take a long time to reflect your self.".format(
message.author.mention, message.author.mention))
else:
return