Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

python - Discord.py on_message breaking rest of commands?

After adding an on_message, the rest of my commands stop working. I've seen this question be asked before, I tried adding await client.process_commands(message), that fixed it, but after changing a few things around, it stopped working. This is supposed to detect blacklisted words, delete them, then delete the message after.

@client.event
async def on_message(message):
    if message.author.bot: return
    message.content = message.content.lower().replace(' ', '')
    if "example1" in message.content or "example2" in message.content or "example3" in message.content or "example4" in message.content:
        await message.delete()
        await message.channel.send(f"{message.author.mention} That word is not allowed!", delete_after=1.5)
        await message.delete()
    await client.process_commands(message)
question from:https://stackoverflow.com/questions/65854694/discord-py-on-message-breaking-rest-of-commands

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're modifying message.content before you pass it to process_commands; specifically, you're removing all the spaces. Of course none of your command names are going to be matched against a giant, single-word string.

Don't directly modify message.content. Since you're checking with in, you don't even need to remove the spaces. Your conditional can also be simplified.

Moreover, as effprime pointed out, if you're going to delete offending messages it doesn't make sense to try to process any commands from them. So you need to either delete the message or process commands from it, not both.

bad_words = ['example1', 'example2', 'example3', 'example4']
if any(word in message.content.lower() for word in bad_words):
    await message.delete()
    ...
else:
    await client.process_commands(message)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...