自制Discord机器人脏话计数器无响应问题求助
Let's walk through what's going wrong with your Discord bot and fix it up so it can start detecting curse words properly!
1. Your Curse Word Detection Logic Is Broken
Right now, you're using message.content.startswith(str(Curse)) to check for curse words—and that's not doing what you think it is.
When you convert your Curse list to a string with str(Curse), it turns something like ["shit", "fuck"] into the literal string "['shit', 'fuck']". So your bot will only respond if someone sends a message that starts exactly with that entire string—not if they use any of the curse words in the list.
Here's the fix: we'll check if any curse word from your list exists in the message (and make it case-insensitive so it catches uppercase/lowercase variations):
# Convert message content to lowercase to catch all case variations lower_content = message.content.lower() # Loop through each curse word in your list for curse_word in Curse: if curse_word.lower() in lower_content: await message.channel.send('Noo! stop speaking the language of the naughty nation!') break # Stop checking after the first match to avoid duplicate messages
2. You're Missing Required Discord Intents (Critical for Discord.py v2.0+)
Starting with Discord.py version 2.0, bots don't get access to message content by default. If you don't enable the message_content intent, your on_message event might fire, but message.content will be empty—so your bot can't see what users are typing at all.
To fix this:
- Add intent configuration at the top of your code:
intents = discord.Intents.default() intents.message_content = True
- Pass the intents when creating your client:
client = discord.Client(intents=intents)
- Don't forget this step! Go to your bot's page in the Discord Developer Portal:
- Navigate to your bot
- Go to the "Privileged Gateway Intents" section
- Toggle on "Message Content Intent"
- Save your changes
3. Verify Your Curse List Import
Make sure your List.py file has a valid list of curse words. It should look something like this:
# List.py Curse = ["shit", "fuck", "asshole"] # Replace with your actual curse words
If Curse isn't a list of strings (e.g., it's a single string, or a different data type), your loop won't work correctly.
4. Quick Fixes for Small Issues
- You have a typo:
"lanuguage"should be"language"in your response message (doesn't break functionality, but makes it look cleaner!) - Double-check your bot token: Ensure the string passed to
client.run()is your actual bot token (no extra spaces, quotes, or typos). If the token was wrong, you'd see an error in the console, but it's worth confirming.
Full Fixed Code
Here's the complete corrected code putting all these fixes together:
import discord import os from List import Curse # Set up required intents intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) @client.event async def on_ready(): print('We have logged in as {0.user}'.format(client)) @client.event async def on_message(message): if message.author == client.user: return # Check for curse words (case-insensitive) lower_content = message.content.lower() for curse_word in Curse: if curse_word.lower() in lower_content: await message.channel.send('Noo! stop speaking the language of the naughty nation!') break # Replace with your actual bot token client.run("YOUR_DISCORD_BOT_TOKEN")
Once you make these changes, restart your bot, send a message with a curse word from your list, and it should respond as expected!
内容的提问来源于stack exchange,提问作者t3c6n0hvnt

