【问题标题】:(Discord.py) How can I save the ID for a voice channel into an sqlite3 file?(Discord.py) 如何将语音通道的 ID 保存到 sqlite3 文件中?
【发布时间】:2021-11-13 02:15:32
【问题描述】:

我正在尝试让我的 Discord 机器人在用户加入时自动加入语音频道。

为此,机器人首先获取服务器的 ID 和用户所在的语音通道,然后将其保存到 sqlite3 文件中。

下面是我的代码:

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


@bot.command()
async def assign(ctx):
    try:
        global getid
        global getguild
        getid = ctx.author.voice.channel.id
        getguild = ctx.author.guild.id
        c.execute ("INSERT INTO channel_data VALUES (getid, getguild, '1')")
        await ctx.send('Channel assigned.')
    except:
        await ctx.send('Please join a voice channel first.')

我在我的 sqlite3 文件中创建了三行,分别是 id(字符串)、guild_id(字符串)和 theme(字符串) .

我已经使用以下代码创建了它:

c.execute("CREATE TABLE channel_data(id STRING, guild_id STRING, theme STRING)")

但是,这是我得到的:

Ignoring exception in command assign:
Traceback (most recent call last):
  File "C:\Users\erick\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", 
line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:\Users\erick\Python_VisualStudioCode\데이터베이스\리타 v2.py", line 55, in assign
    c.execute("INSERT INTO channel_data VALUES (getid, getguild, '1')")
sqlite3.OperationalError: no such column: getid

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\erick\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 939, in invokeine 939, in invoke
    await ctx.command.invoke(ctx)                                                                              line 863, in invoke
  File "C:\Users\erick\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", 
line 863, in invoke                                                                                            line 94, in wrapped
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\erick\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", getid
line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: no such column: 
getid

请帮忙:(

【问题讨论】:

    标签: python sqlite discord.py


    【解决方案1】:

    对插入命令使用正确的语法。

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

    您可以跳过列部分(id, guild_id, theme),但这里的详细程度可能很好。

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 2021-08-05
    • 2020-09-06
    • 1970-01-01
    • 2021-11-13
    • 2021-08-26
    • 1970-01-01
    • 2021-12-11
    • 2021-04-15
    • 2020-09-24
    相关资源
    最近更新 更多