【问题标题】:Why my on_message gets called after my command?为什么我的 on_message 在我的命令之后被调用?
【发布时间】:2021-01-07 23:44:06
【问题描述】:

我的代码正在运行。只有一个问题,就是在调用任何命令之后,我的 on_message 都会被立即调用(这会导致一些副作用)

async def delete_on_swear(message):
    if not message:
        return
    db = sqlite3.connect('main.sqlite')
    cursor = db.cursor()
    try:
        guild_id = message.guild.id
    except AttributeError:
        return
    cursor.execute(f'SELECT word FROM badwords WHERE guild_id={guild_id}')
    swears = [swear[0] for swear in cursor.fetchall()]
    if not swears:
        return
    if bot.user == message.author:
        return
    for swearword in swears:
        if not message.channel.is_nsfw() and is_substring(message.content.lower(), swearword):
            await message.delete()
            await message.author.create_dm()
            await message.author.dm_channel.send(
                f'Hi {message.author}, you sent a message containing the following word: {swearword}'
            )
            return```

【问题讨论】:

  • 为什么不呢?要使用机器人命令,用户输入一条消息,对吗?所以还有一条信息需要思考。

标签: python discord.py


【解决方案1】:

on_message 在机器人可以查看的任何频道中发送任何消息后运行。在您的 on_message 顶部,放置以下代码以防止机器人根据自己的消息采取行动:

if message.author == bot.user:
    return

【讨论】:

    猜你喜欢
    • 2016-07-26
    • 1970-01-01
    • 2021-11-14
    • 2021-01-24
    • 2018-12-16
    • 1970-01-01
    相关资源
    最近更新 更多