首页 > 解决方案 > (Discord.py) 我的程序无法为我的 sqlite3 文件添加价值

问题描述

我试图让我的机器人向频道发送消息,当用户对某个表情符号做出反应时,机器人将插入用户所在语音频道的 ID、服务器 ID 和号码用户选择的主题。

下面是我的代码:

global conn
global c
conn = sqlite3.connect('data.db')
c = conn.cursor()

@bot.command() 
async def assign(ctx):
    try:
        global getid
        global getguild
        global gettheme
        
        getid = str(ctx.author.voice.channel.id)
        print (getid)
        getguild = str(ctx.author.guild.id)
        print (getguild)
        global Ftext
        Ftext = ""
        correct = 0
        global Flist
        

        titlename = "Please select a theme"
        embed = discord.Embed(title = titlename, description = Ftext.strip(), color = 0xff6868)
        embed.add_field(name = "1. Lofi", value = "Lofi", inline = False)
        embed.add_field(name = "Gaming✨", value = "NCS, EDM", inline = False)
        Flist = await ctx.send(embed = embed)
        await Flist.add_reaction("")
        await Flist.add_reaction("✨")

    except:
        await ctx.send('Please join a channel first')

这是机器人发送嵌入消息并向其添加反应的部分。

我已经说明了三个变量:

getid是语音通道的 ID,

getguild是服务器的 ID,

gettheme是频道的主题

下面是检测用户反应并将三个变量添加到数据库的代码。

@bot.event
async def on_reaction_add(reaction, users):
    if users.bot == 1:
        pass
    else:
        try:
            await Flist.delete()
        except:
            pass
        else:
            if str(reaction.emoji) == '':
                await reaction.message.channel.send("Set the theme to lo-fi. Channel id = "+str(getid))
                gettheme = '1'
                print(gettheme)
                c.execute("INSERT INTO channel_data (id, guild_id, theme) VALUES (?,?,?)", (getid, getguild, gettheme))
                
            elif str(reaction.emoji) == '✨':
                await reaction.message.channel.send("Set the theme to NCS. Channel id = "+str(getid))
                gettheme = '2'
                print(gettheme)
                c.execute("INSERT INTO channel_data (id, guild_id, theme) VALUES (?,?,?)", (getid, getguild, gettheme))

从代码中可以看出,sqlite3 文件中有三列,分别是 id、guild_id 和主题。这三个都表示为 STRING。

但是,当我运行代码时,我没有收到任何错误,但是当我使用数据库浏览器检查它时,没有数据被添加到数据库中。

另外,我尝试通过编写另一个简单的程序将数据添加到同一个文件中,

import sqlite3

conn = sqlite3.connect('data.db')
c = conn.cursor()

c.execute("INSERT INTO channel_data (id, guild_id, theme) VALUES (?,?,?)", ('1', '2', '3'))

conn.commit()
conn.close()

这工作得很好。

我应该如何解决这个问题?我没有得到任何错误,所以我不确定我应该如何解决它。

标签: pythonsqlitediscorddiscord.py

解决方案


推荐阅读