如何让更新命令工作?上面脚本的其余部分有效,但更新失败 [英] How do I get the update command to work? The rest of the script above works but update fails

查看:33
本文介绍了如何让更新命令工作?上面脚本的其余部分有效,但更新失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@commands.command()
@commands.cooldown(1, 8, commands.BucketType.user)
async def Beg(self, ctx):
    possibility = randint(1, 10)
    if possibility == 5:
        await ctx.send("I'll just give you this <:DiscordError:879192336960196619>")
    else:
        amount = randint(25, 175)
        outcomes = [f"If you're going to keep begging me I guess I'll give you ${amount}", f"How about you get a job for yourself? But I guess I'll give you ${amount}", f"I'll give you ${amount} but you owe me cuddles after this!", f"Let me see what's in my wallet.. Oh it's ${amount}! Take it!"]
        outcomes1 = random.choice(outcomes)
        await ctx.send(outcomes1)
        db = sqlite3.connect("main.sqlite")
        cursor = db.cursor()
        result = cursor.fetchone()
        sql = f"UPDATE main SET money = * WHERE member_id = {ctx.message.author.id}"
        val = (result[1] + amount)
        cursor.execute(sql, val)
        db.commit()
        await ctx.send("Sent!")
        cursor.close()
        db.close()

正如我所说,更新命令不起作用.结果消息工作正常.它已发送,没有任何更新.

As I said, the update command doesn't work. The outcome messages works fine. It get's sent, nothing get's updated.

推荐答案

作为最佳实践,我建议将所有 ctx 确认消息移到函数末尾.我更喜欢这种方式,因为它有助于作为一种调试方式(如果前面的所有步骤都成功,则会发送确认消息).

As a best practice, I recommend moving all ctx confirmation messages to the end of the function. I prefer it that way, because it'll help serve as a sort of debugging (if all prior steps are successful, then a confirmation message is sent).

我建议删除 cursor.close() 因为该方法不适用于光标对象.您打开和关闭数据库连接,而不是游标.

I recommend removing cursor.close() as that method isn't applicable to the cursor object. You open and close the database connection, not the cursor.

result[1] 也应该是 result[0].在不知道您的数据库是如何设置的情况下,在尝试对其进行数学运算之前确保 result[0] 是一个整数也是值得的.

result[1] should be result[0], as well. Without knowing how your db is set up, it also might be worthwhile to ensure that result[0] is an integer before trying to do math on it.

这篇关于如何让更新命令工作?上面脚本的其余部分有效,但更新失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆