You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord Bot查询SQLite数据库时Embed消息不显示无控制台报错问题求助

问题排查与修复方案

已确认的核心错误点

  • 命令前缀不匹配:你的需求是用?作为命令前缀触发查询,但两版代码的判断逻辑都是匹配$开头的消息,输入?2完全不会触发后续查询逻辑,自然没有Embed返回。
  • 第一版代码参数类型错误:第一版中直接把完整消息内容作为查询参数传入SQL,比如输入$2时参数是字符串"$2",如果数据库id字段为整数类型,永远查不到对应数据,不会执行发Embed的分支。
  • 未开启消息内容权限:如果使用discord.py v2.0及以上版本,默认关闭普通消息内容读取权限,就算代码逻辑正确也无法识别用户发送的命令内容。

修复步骤

  1. 开启消息内容意图
    首先前往Discord开发者平台的Bot管理页,在「Privileged Gateway Intents」分类下启用Message Content Intent权限,同时修改Bot初始化代码:
import discord
import sqlite3
from sqlite3 import Error

# 初始化时开启所需意图
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
database_file = "你的数据库文件路径"
  1. 修改命令前缀匹配规则
    把原本的if message.content.startswith('$'):替换为if message.content.startswith('?'):,匹配你需要的?数字触发逻辑。
  2. 优化错误处理逻辑
    删除原有代码中查询数据库出错时的exit(-1)语句,避免单次查询错误直接终止Bot进程,可改为返回对应报错提示给用户。

修正后完整可用代码

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    # 匹配?开头的命令
    if message.content.startswith('?'):
        message_slices = message.content.split()
        try:
            # 提取?后面的数字作为id
            arg_id = int(message_slices[0][1:])
        except ValueError:
            embed = discord.Embed(title="无效参数", color=0x0000FF)
            embed.set_footer(icon_url='https://pbs.twimg.com/profile_images/1325672283881484289/oaGtVIOD_400x400.png', text='Created by @Expected')
            await message.channel.send(embed=embed)
            return
        
        try:
            conn = sqlite3.connect(database_file)
            req = conn.cursor()
            req.execute("SELECT * FROM space2 WHERE id=?", (arg_id,))
            row = req.fetchone()
            
            if row is not None:
                embed = discord.Embed(title='NFT', color=0x0000FF)
                embed.set_thumbnail(url='https://www.pngkit.com/png/detail/231-2316751_database-database-icon-png.png')
                embed.add_field(name='id', value=str(row[0]), inline=False)
                embed.add_field(name='CATEGORIE', value=str(row[1]), inline=False)
                embed.add_field(name='RANK', value=str(row[2]), inline=False)
                embed.add_field(name='SCORE', value=str(row[3]), inline=False)
                embed.add_field(name='POWER AREA GRAVITY 0', value=str(row[4]), inline=False)
                embed.add_field(name='POWER EYES WEAPONS', value=str(row[5]), inline=False)
                embed.set_footer(icon_url='https://pbs.twimg.com/profile_images/1325672283881484289/oaGtVIOD_400x400.png', text='Created by @Expected')
                await message.channel.send(embed=embed)
            else:
                embed = discord.Embed(title="未查询到对应数据", color=0x0000FF)
                embed.set_footer(icon_url='https://pbs.twimg.com/profile_images/1325672283881484289/oaGtVIOD_400x400.png', text='Created by @Expected')
                await message.channel.send(embed=embed)
                
            req.close()
            conn.close()
        except Error as e:
            print(e)
            embed = discord.Embed(title="数据库查询出错", color=0x0000FF)
            embed.set_footer(icon_url='https://pbs.twimg.com/profile_images/1325672283881484289/oaGtVIOD_400x400.png', text='Created by @Expected')
            await message.channel.send(embed=embed)

# 末尾添加client.run('你的Bot Token')启动即可

内容的提问来源于stack exchange,提问作者Phil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 23:57:04