Discord.py猜国旗游戏功能异常求助(新手开发者)
Discord.py 猜国旗游戏斜杠命令修复方案
问题分析与修复点
你的猜国旗游戏无法正常运行,核心问题集中在以下几点:
未开启Message Content Intent
Discord.py v2.0及以上版本,必须显式开启message_content权限才能读取用户发送的文本消息。原代码仅使用默认intents,导致wait_for("message")无法捕获用户的答题输入。- 修复:
- 代码中添加
intents.message_content = True - 在Discord开发者平台的Bot设置中,开启Message Content Intent开关
- 代码中添加
- 修复:
斜杠命令响应顺序错误
斜杠命令要求必须先通过ctx.respond()完成首次响应,否则Discord会判定命令超时(即截图中的“应用程序无响应”)。原代码先执行ctx.send()再调用ctx.respond(),违反了Discord的命令响应规则。消息捕获逻辑不严谨
原wait_for的检查条件仅验证了消息作者,未限制频道,会导致用户在其他频道发消息也触发游戏判定,造成逻辑混乱。
修复后的完整代码
import discord from discord.ext import commands from discord.ui import Button, View import random import asyncio # 初始化intents,开启message_content权限 intents = discord.Intents.default() intents.message_content = True # 初始化机器人 bot = commands.Bot(command_prefix='', intents=intents) # 国旗字典:国家名称 -> 表情 flags = { "Poland": "🇵🇱", "Germany": "🇩🇪", "France": "🇫🇷", "Italy": "🇮🇹", "Spain": "🇪🇸", "United Kingdom": "🇬🇧", "Sweden": "🇸🇪", "Norway": "🇳🇴", "Japan": "🇯🇵", "China": "🇨🇳", "Canada": "🇨🇦", "Australia": "🇦🇺", "Russia": "🇷🇺", "Brazil": "🇧🇷", "Argentina": "🇦🇷", "India": "🇮🇳", "Greece": "🇬🇷", "Egypt": "🇪🇬", "Turkey": "🇹🇷", "South Africa": "🇿🇦", } @bot.event async def on_ready(): print("Bot is running...") await bot.change_presence(status=discord.Status.dnd, activity=discord.Activity(type=discord.ActivityType.playing, name="Under Maintenance")) @bot.slash_command(name='help', description='Show a list of available commands') async def help(ctx): embed_help = discord.Embed(title='Available Commands', description='\n- /flags: Play the flag guessing game\n- /ping: Check bot latency\n- /invite: Get bot invite link') await ctx.respond(embed=embed_help) @bot.slash_command(name='ping', description='Show the bot\'s ping') async def ping(ctx): latency = round(bot.latency * 1000) # 修正:将秒转成毫秒,显示更准确 embed_ping = discord.Embed(title=f'Pong! My latency is {latency}ms', color=discord.Color.green()) await ctx.respond(embed=embed_ping) @bot.slash_command(name='flags', description='Play a flag guessing game') async def flag_game(ctx): # 先完成斜杠命令的首次响应,避免超时 await ctx.respond("Flag Game Started! 🎮") # 随机选择国旗 random_flag = random.choice(list(flags.keys())) # 发送"抽取中..."消息 drawing_message = await ctx.send("Drawing... <a:random_:966715531216429076>") await asyncio.sleep(3) # 编辑消息显示国旗 flag_emoji = flags[random_flag] embed = discord.Embed(title='Flag Game', description="Here is the flag of one of the countries. What is the name of this country?") embed.add_field(name="Flag:", value=flag_emoji, inline=False) await drawing_message.edit(content=None, embed=embed) # 等待用户响应,增加频道检查确保仅当前频道有效 try: response = await bot.wait_for( "message", check=lambda m: m.author == ctx.author and m.channel == ctx.channel, timeout=30.0 ) # 验证答案(忽略大小写) if response.content.lower() == random_flag.lower(): await ctx.send(f"Congratulations! Correct answer. It's the flag of {random_flag}. 🎉") else: await ctx.send(f"Sorry, that's not the correct answer. The correct answer is {random_flag}.") except asyncio.TimeoutError: await ctx.send("Time for a response has expired. Game over. ⏰") @bot.slash_command(name='invite', description='Send a bot invite link') async def invite(ctx): embed = discord.Embed( title="Add me!", description="Click here to add me to your server.", color=discord.Color.green(), ) invite_button = Button( style=discord.ButtonStyle.link, label="Add me!", emoji="<:arrow_:966715437662478388>", url="https://discord.com/api/oauth2/authorize?client_id=YOUR_CLIENT_ID&permissions=8&scope=bot%20applications.commands", ) view = View() view.add_item(invite_button) await ctx.respond(embed=embed, view=view) # 替换为你的机器人令牌 bot.run("YOUR_BOT_TOKEN")
额外优化说明
- 修正了
/ping命令的latency计算:将原秒级单位转成毫秒,显示结果更符合用户认知。 - 完善了
/help命令的描述内容,明确列出所有可用命令。 - 更新了
/invite命令的scope参数,添加applications.commands确保斜杠命令能正常同步到服务器。
内容的提问来源于stack exchange,提问作者QwertyCode
相关产品推荐
相关产品推荐

