【问题标题】:{Discord.py-rewrite} Bot commands don't work while I have an "On Message" event{Discord.py-rewrite} 当我有“On Message”事件时,Bot 命令不起作用
【发布时间】:2020-12-24 10:23:06
【问题描述】:

我目前正在开发一个不和谐的笑话机器人,但我仍然想要一些功能。我有一些命令,除非我有“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”事件和命令不想同时工作。

编辑

我非常清楚我的“On Message”事件没有多大意义,它是我和我的朋友之间的一个内部笑话。

【问题讨论】:

    标签: python python-3.x discord discord.py discord.py-rewrite


    【解决方案1】:

    覆盖默认提供的on_message() 禁止运行任何额外的命令。要解决此问题,请在 on_message() 事件的末尾添加 bot.process_commands(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
    

    【讨论】:

    • 感谢您的回答,但是,它似乎没有奏效。如果您有任何其他建议,请告诉我,我会尝试的。谢谢。编辑:我把它放在活动的开始和它的工作,谢谢!
    • 没问题,我将编辑我的答案以反映您的解决方案。祝你好运。
    猜你喜欢
    • 2020-08-16
    • 2019-07-23
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 2020-10-13
    • 2021-09-23
    • 2021-05-11
    • 2021-07-27
    相关资源
    最近更新 更多