I’m working on a Discord bot and found some code that searches Wikipedia for keywords. The issue is when I add this code to my bot, all my other commands stop responding. I’m not sure what’s causing this conflict.
@client.event
async def on_message(msg):
content_parts = msg.content.split()
if content_parts[0].lower() == "!search":
keywords = content_parts[1:]
try:
await client.send_message(msg.channel, get_wiki_info(keywords))
except wikipedia.exceptions.DisambiguationError as error:
await client.send_message(msg.channel, error)
I think maybe I should convert this to use @client.command decorator instead but I’m not sure how to modify the code properly. Also having trouble with multi-word searches like ‘space station’ because it only processes the last word and throws disambiguation errors. Any suggestions would be helpful!
yeah, same thing happened to me! you need await client.process_commands(msg) at the end of your on_message function or it’ll block all your other commands. for multiword searches, join the keywords with spaces: ' '.join(keywords) instead of passing a list.
The problem is you’re using on_message without handling commands properly. When you override this event, it blocks all messages from reaching the command handler. Switch to the @client.command decorator instead - it’ll clean up your code. Try this: @client.command() async def search(ctx, *, query): await ctx.send(get_wiki_info(query)). The * grabs everything after ‘search’ as one argument, which fixes your multi-word query issue. This also solves the event conflict since it works within the commands framework.
Your on_message event is killing all your commands. When you don’t call process_commands at the end, it grabs every message and blocks the bot from seeing other commands. Just add await client.process_commands(msg) as the last line in your on_message function. Better yet, rewrite it as a proper command using the commands extension - way cleaner. For the multi-word thing, you’re sending a list to get_wiki_info when it wants a string. Try ' '.join(keywords) to combine them. I had this exact issue when I started - spent hours wondering why everything broke.