【问题标题】:Updating information in database issue更新数据库问题中的信息
【发布时间】:2021-07-07 00:14:03
【问题描述】:

您好,我正在尝试更新数据库中的一些条目,但在更新值中指定的正确信息时遇到问题。

我下面的代码块显示了一个允许用户给用户 XP 的命令。我想要实现的是更新两个值 val_1val_2 但是 val_2 xp-amount, total_xp-amount 在调用命令时错误地从指定的 amount 中扣除更大的数量(数字)。例如,当 !xp @user 5 被调用时,它应该给提到的@user 5 XP,并从调用该命令的用户那里获得 5XP。

这是我的工作:

async def xp(self, ctx, user: discord.Member, amount: int):

    conn = psycopg2.connect(DATABASE_URL, sslmode='require')
    cursor = conn.cursor()
    cursor.execute(f"SELECT lvl, xp, total_xp FROM levels WHERE guild_id = {ctx.guild.id} AND user_id = {user.id}")
    result2 = cursor.fetchone()
    xp = int(result2[1])
    total_xp = int(result2[2])

    if user.id == ctx.message.author.id:
        await ctx.send("You can't give yourself XP.")
        return

    if int(amount) > 50:
        await ctx.send("You can only give a maximum of 50 XP.")
        return

    #SQL 
    sql = ("UPDATE levels SET xp=%s, total_xp=%s WHERE guild_id=%s and user_id=%s")

    #The member recieving XP
    val_1 = (xp+amount, total_xp+amount, str(ctx.guild.id), str(user.id))
    cursor.execute(sql, val_1)

    #The member giving XP 
    val_2 = (xp-amount, total_xp-amount, str(ctx.guild.id), str(ctx.message.author.id))
    cursor.execute(sql, val_2)
    
    conn.commit()
    cursor.close()
    conn.close()

    #return something here
    await ctx.send(f"{ctx.message.author.name} has given {amount} XP to 
    {user.mention}.") 

我们将不胜感激

【问题讨论】:

  • 有什么问题?您是否收到任何错误/回溯?结果是什么?预期的结果是什么?请添加尽可能多的详细信息
  • 这个问题是我的代码val_2 = (xp-amount, total_xp-amount, str(ctx.guild.id), str(ctx.message.author.id)) 中的这一行没有带走xp-amount, total_xp-amount, 的正确值,它取的是更大的值,而不是使用命令调用的值。没有错误或回溯。

标签: python-3.x postgresql discord.py


【解决方案1】:

首先,您没有得到两个用户的总经验值,只有调用命令的人。

之前:

cursor.execute(f"SELECT lvl, xp, total_xp FROM levels WHERE guild_id = {ctx.guild.id} AND user_id = {user.id}")
    result2 = cursor.fetchone()
    xp = int(result2[1])
    total_xp = int(result2[2])

    if user.id == ctx.message.author.id:
        await ctx.send("You can't give yourself XP.")
        return

    if int(amount) > 50:
        await ctx.send("You can only give a maximum of 50 XP.")
        return

    #SQL 
    sql = ("UPDATE levels SET xp=%s, total_xp=%s WHERE guild_id=%s and user_id=%s")

    #The member recieving XP
    val_1 = (xp+amount, total_xp+amount, str(ctx.guild.id), str(user.id))
    cursor.execute(sql, val_1)

    #The member giving XP 
    val_2 = (xp-amount, total_xp-amount, str(ctx.guild.id), str(ctx.message.author.id))
    cursor.execute(sql, val_2)

固定:

cursor.execute(f"SELECT lvl, xp, total_xp FROM levels WHERE guild_id = {ctx.guild.id} AND user_id = {ctx.message.author.id}")
    result2 = cursor.fetchone()
    xp = int(result2[1])
    sender_total_xp = int(result2[2])

    if user.id == ctx.message.author.id:
        await ctx.send("You can't give yourself XP.")
        return

    if int(amount) > 50:
        await ctx.send("You can only give a maximum of 50 XP.")
        return

    cursor.execute(f"SELECT lvl, xp, total_xp FROM levels WHERE guild_id = {ctx.guild.id} AND user_id = {user.id}")

    result = cursor.fetchone()
    receiver_total_xp = int(result[2])

    #SQL 
    sql = ("UPDATE levels SET xp=%s, total_xp=%s WHERE guild_id=%s and user_id=%s")

    #The member recieving XP
    val_1 = (xp+amount, receiver_total_xp+amount, str(ctx.guild.id), str(user.id))
    cursor.execute(sql, val_1)

    #The member giving XP 
    val_2 = (xp-amount, sender_total_xp-amount, str(ctx.guild.id), str(ctx.message.author.id))
    cursor.execute(sql, val_2)

如您所见,您一直在从数据库请求不正确的数据,因为您使用的是 user.id(这是目标 id,即接收 xp 的人)而不是 ctx.message.author.id是调用命令的人。

还有一个问题是将发送者的 xp 设置为 -5 接收者的 xp。

【讨论】:

    猜你喜欢
    • 2017-01-29
    • 2022-01-21
    • 2014-12-17
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 2011-12-03
    • 1970-01-01
    相关资源
    最近更新 更多