Discord.py机器人无法响应消息问题求助(Replit环境)
Discord笑话机器人无响应问题解决
问题根源
你的机器人在线但不生效,核心原因有三个:
- 未开启
message_content意图:Discord v8版本后,默认不再授予机器人读取消息内容的权限,必须显式声明才能获取message.content,否则无法识别!joke指令。 - 未排除机器人自身消息:如果不跳过机器人自己发送的消息,可能引发逻辑循环或指令误判。
- 开发者平台特权意图未启用:即使代码中开启了
members意图,也需要在Discord后台确认对应特权意图处于启用状态。
修复后的代码
import discord import random # 配置完整的意图权限 intents = discord.Intents.default() intents.members = True intents.message_content = True # 新增:开启消息内容读取权限 client = discord.Client(intents=intents) jokes = [ "What do you call a factory that makes okay products? A satisfactory.", "Dear Math, grow up and solve your own problems.", "What did the janitor say when he jumped out of the closet? Supplies!", "Have you heard about the chocolate record player? It sounds pretty sweet.", "What did the ocean say to the beach? Nothing, it just waved.", "Why do seagulls fly over the ocean? Because if they flew over the bay, we'd call them bagels.", "I only know 25 letters of the alphabet. I don't know y.", "How does the moon cut his hair? Eclipse it.", "What did one wall say to the other? I'll meet you at the corner.", "What did the zero say to the eight? That belt looks good on you.", "A skeleton walks into a bar and says, 'Hey, bartender. I'll have one beer and a mop.'", "Where do fruits go on vacation? Pear-is!", "I asked my dog what's two minus two. He said nothing.", "What did Baby Corn say to Mama Corn? Where's Pop Corn?", "What's the best thing about Switzerland? I don't know, but the flag is a big plus.", "What does a sprinter eat before a race? Nothing, they fast!", "Where do you learn to make a banana split? Sundae school.", "What has more letters than the alphabet? The post office!", "Dad, did you get a haircut? No, I got them all cut!", "What do you call a poor Santa Claus? St. Nickel-less.", "I got carded at a liquor store, and my Blockbuster card accidentally fell out. The cashier said never mind.", "Where do boats go when they're sick? To the boat doc.", "I don't trust those trees. They seem kind of shady.", "My wife is really mad at the fact that I have no sense of direction. So I packed up my stuff and right!", "How do you get a squirrel to like you? Act like a nut.", "Why don't eggs tell jokes? They'd crack each other up.", "I don't trust stairs. They're always up to something.", "What do you call someone with no body and no nose? Nobody knows.", "Did you hear the rumor about butter? Well, I'm not going to spread it!", "Why couldn't the bicycle stand up by itself? It was two tired.", "What did one hat say to the other? Stay here! I'm going on ahead.", "Why did Billy get fired from the banana factory? He kept throwing away the bent ones." ] @client.event async def on_ready(): print('Logged in as {0.user}'.format(client)) @client.event async def on_message(message): # 跳过机器人自身消息,避免逻辑异常 if message.author == client.user: return if message.content == '!joke': joke = random.choice(jokes) await message.channel.send(joke) @client.event async def on_member_join(member): channel = member.guild.system_channel if channel is not None: joke = random.choice(jokes) welcome_msg = f"Welcome {member.mention}! Here's a joke to make you feel at home:\n\n{joke}" await channel.send(welcome_msg) client.run('Secret Token')
必须完成的后台配置
- 开启Discord开发者平台的特权意图:
- 打开Discord开发者应用页面,找到你的机器人项目
- 进入「Bot」标签页,下滑到「Privileged Gateway Intents」区域
- 开启「Server Members Intent」和「Message Content Intent」选项
- 验证Replit依赖版本:
- 在Replit的Shell中执行
pip show discord.py,确认版本为v2.x - 若版本低于v2.x,执行
pip install --upgrade discord.py更新依赖
- 在Replit的Shell中执行
内容的提问来源于stack exchange,提问作者dcbobo
相关产品推荐
相关产品推荐

