我正在编写一个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.