我正在编写一个discord的机器人,我添加了斜杠命令.我使用sqlite3创建一个数据库.

其中一个命令有问题,因为它在不一致时返回The application did not respond.我通过添加一些打印语句判断了代码中的错误之处,并意识到这是因为cursor.execute.以下是代码:

@commands.hybrid_command(name='addoc', with_app_command=True)
# @commands.cooldown(rate=1, per=60, type=commands.BucketType.user)
async def addoc(self, ctx: CustomContext, name, age, nationality, gender, sexuality, universe, desc, picture: discord.Attachment):
        """ Add OC to the database with respect to user and guild id """
        # Get guild and user roles
        guild_id = str(ctx.guild.id)
        user_roles = extract_role_ids(ctx.message.author.roles)  # roles formatting needed to only keep the actual id
        print('check 1')
        try:
            # Check if permissions have been set for the server
            roles_list = self.roles_dict[guild_id]
            # Check is user is allowed to use the database
            print('check 2')
            if any(role in roles_list for role in user_roles):
                user_id = ctx.message.author.id

                print('check 3')
                cursor = self.db.cursor()
                print('check 4')
                cursor.execute('INSERT INTO "guild_{}" VALUES(?,?,?,?,?,?,?,?,?,?)'.format(guild_id), (name, age, nationality, gender, sexuality, universe, desc, picture.url))
                print('check 5')
                self.db.commit()

                # Close cursor
                cursor.close()
                self.db.close()


                await ctx.send(f'Character successfully added for <@{user_id}>!')

            else:
                await ctx.send("**If you think you should be able to add a character to the database, contact your local admins.**")

        except KeyError:
            await ctx.send("**Please set the authorized roles first with `addrole` before adding an OC.**")

由于某种原因,代码在判断4时太慢,斜杠命令只是停止(不打印check 5).

我在添加斜杠命令之前没有这个问题,所以有什么方法可以解决这个问题吗?通过以某种方式使代码更快?

我认为这可能来自图片输入,所以我添加了一个.url来只获得链接,但这并没有解决这个问题.

EDIT 1: I have good contact with the developer behind the postgreslite. He made sure everything was asynchronous and updated the library. I also tried 100 but the bot just keep thinking indefinitely without sending a response. I said that already but I did not have any issue when it was not a slash command (101 instead of 102.

推荐答案

所以问题来自SQL,因为我将user_id设置为主键.把id INTEGER PRIMARY KEY分加到表上就解决了问题.我还确保我有正确的行数(我错过了guild_id行).

Python相关问答推荐

级联框架中的行、列索引

枢轴框架值及其单位

如何模拟4个粒子相互移动的运动?

分析历史数据:多年来每天每小时的平均值

当一个子指标符合条件时 Select 多指标

Pandas 修改原始excel

将列中的滚动值集转换为单元格中的单个值

解析讨论论坛只给我第一个用户 comments ,但没有给我其他用户回复

如何在超时的情况下同步运行Matplolib服务器端?该过程随机挂起

使用regex分析具有特定字符的字符串(如果它们存在)

如何根据日期和时间将状态更新为已过期或活动?

返回nxon矩阵的diag元素,而不使用for循环

理解Python的二分库:澄清bisect_left的使用

使用Keras的线性回归参数估计

Matlab中是否有Python的f-字符串等效物

如何在箱形图中添加绘制线的传奇?

Python中的嵌套Ruby哈希

更改键盘按钮进入'

C#使用程序从Python中执行Exec文件

在pandas数据框中计算相对体积比指标,并添加指标值作为新列