Discord.py彩票命令问题:单次购多张票无法发送多Embed
解决Discord斜杠命令彩票批量购买的Embed发送问题
问题背景
我正在开发Discord机器人的彩票命令,希望支持用户单次购买多张彩票,但Discord不允许斜杠命令单次发送多个Embed。尝试将游戏逻辑放入for _ in range(amount):循环后无反应,使用await interaction.followup.send(embed=lottery)时出现"The application did not respond"错误。
原代码如下:
@bot.tree.command(name="lottery", description="Every 1 in 100 lottery wins 1,000,000 chips. Lottery costs 10,000 chips.") @app_commands.describe(amount="Enter the amount of lotteries you'd like to get (max. is set to 5).") async def lottery(interaction: discord.Interaction, amount:int=1): member = interaction.user #Errors db = sqlite3.connect("main.sqlite") cursor = db.cursor() cursor.execute(f"SELECT wallet FROM main WHERE user_id = {member.id}") wallet = cursor.fetchone() try: wallet = wallet[0] except: wallet = wallet if amount > 5: error2=discord.Embed(description="**ERROR:** You can't buy more than 5 lotteries per command!", color=discord.Color.red()) return await interaction.response.send_message(embed=error2) if wallet < 10000*amount: error2=discord.Embed(description="**ERROR:** You don't have enough <:chip:1167488856203984957> to play lottery!", color=discord.Color.red()) return await interaction.response.send_message(embed=error2) #Game Logic winnumber = 55 win = 1000000 formatted_win = "{:,}".format(win) #Format win with commas formatted_new_balance = "{:,}".format(wallet + win) #Format wallet + payout with commas formatted_new_balance2 = "{:,}".format(wallet - 10000) #Format wallet + payout with commas botchoice = random.randint(1,101) if wallet >= 10000: cursor.execute("UPDATE main SET wallet = ? WHERE user_id = ?", (wallet - 10000, member.id)) db.commit() if botchoice == winnumber: cursor.execute("UPDATE main SET wallet = ? WHERE user_id = ?", (wallet + win, member.id)) db.commit() lottery = discord.Embed(description=f"You're insane, you just won **{formatted_win}** <:chip:1167488856203984957>\nNew balance: **{formatted_new_balance}** <:chip:1167488856203984957>", color=discord.Color.green()) lottery.set_author(name=f"{member.name}'s Lottery 🍀", icon_url = member.avatar) lottery.set_thumbnail(url="url") await interaction.response.send_message(embed=lottery) else: lottery = discord.Embed(description=f"Sadly you just lost **10,000** <:chip:1167488856203984957>\nNew balance: **{formatted_new_balance2}** <:chip:1167488856203984957>", color=discord.Color.red()) lottery.set_author(name=f"{member.name}'s Lottery 🍀", icon_url = member.avatar) lottery.set_thumbnail(url="url") await interaction.response.send_message(embed=lottery) cursor.close() db.close()
核心问题分析
- 响应超时:Discord要求斜杠命令必须在3秒内返回初始响应,直接调用
followup.send而未先发送初始响应会触发"应用未响应"错误。 - 逻辑变量错误:原代码的金额计算、钱包更新均基于初始值,循环时未实时更新钱包状态,且重复执行数据库操作效率低下。
优化解决方案
方案1:将所有结果整合到单个Embed(推荐)
把多次抽奖的结果汇总到一个Embed中,既符合Discord的限制,也能让用户直观看到所有抽奖情况:
import discord from discord import app_commands import sqlite3 import random @bot.tree.command(name="lottery", description="每100张彩票有1张中1,000,000筹码,每张彩票花费10,000筹码。") @app_commands.describe(amount="输入购买彩票的数量(最多5张)。") async def lottery(interaction: discord.Interaction, amount:int=1): member = interaction.user # 数据库连接与初始化 db = sqlite3.connect("main.sqlite") cursor = db.cursor() cursor.execute("SELECT wallet FROM main WHERE user_id = ?", (member.id,)) wallet_result = cursor.fetchone() if wallet_result is None: cursor.execute("INSERT INTO main (user_id, wallet) VALUES (?, ?)", (member.id, 0)) db.commit() wallet = 0 else: wallet = wallet_result[0] # 错误校验 if amount > 5: error_embed = discord.Embed(description="**ERROR:** 一次最多购买5张彩票!", color=discord.Color.red()) return await interaction.response.send_message(embed=error_embed) total_cost = 10000 * amount if wallet < total_cost: error_embed = discord.Embed(description="**ERROR:** 你的<:chip:1167488856203984957>不足,无法购买这么多彩票!", color=discord.Color.red()) return await interaction.response.send_message(embed=error_embed) # 一次性扣除所有彩票费用 cursor.execute("UPDATE main SET wallet = ? WHERE user_id = ?", (wallet - total_cost, member.id)) db.commit() current_wallet = wallet - total_cost # 批量处理抽奖逻辑 win_number = 55 win_reward = 1000000 win_count = 0 result_list = [] for idx in range(amount): bot_choice = random.randint(1, 101) if bot_choice == win_number: win_count += 1 current_wallet += win_reward result_list.append(f"第{idx+1}张:🎉 恭喜中奖!获得**{win_reward:,}** <:chip:1167488856203984957>") else: result_list.append(f"第{idx+1}张:😢 未中奖,扣除**10,000** <:chip:1167488856203984957>") # 更新最终钱包余额 cursor.execute("UPDATE main SET wallet = ? WHERE user_id = ?", (current_wallet, member.id)) db.commit() # 生成汇总Embed embed_color = discord.Color.green() if win_count > 0 else discord.Color.red() lottery_embed = discord.Embed( title=f"{member.name}的彩票结果 🍀(中奖{win_count}次)", color=embed_color ) lottery_embed.set_author( name=member.name, icon_url=member.avatar.url if member.avatar else member.default_avatar.url ) lottery_embed.add_field(name="抽奖详情", value="\n".join(result_list), inline=False) lottery_embed.add_field(name="最终余额", value=f"**{current_wallet:,}** <:chip:1167488856203984957>", inline=False) lottery_embed.set_thumbnail(url="你的缩略图URL") # 发送初始响应(必选,避免超时) await interaction.response.send_message(embed=lottery_embed) cursor.close() db.close()
方案2:先发送初始响应再用followup发送结果
如果需要分开展示每一张彩票的结果,必须先发送初始响应,再通过followup发送后续消息:
# 关键代码片段 await interaction.response.send_message("正在处理你的彩票请求...") # 循环处理每张彩票 for idx in range(amount): # 单张彩票逻辑处理(省略重复代码) single_embed = discord.Embed(...) await interaction.followup.send(embed=single_embed)
内容的提问来源于stack exchange,提问作者RedFox_Gamer
相关产品推荐
相关产品推荐

