【问题标题】:How to fix it? (python3, discord.py and sqlite3)如何解决? (python3、discord.py 和 sqlite3)
【发布时间】:2020-07-18 21:41:23
【问题描述】:

如何解决? (python3、discord.py 和 sqlite3)

我尝试制作 cosino discord 机器人(30% 获胜 = x2 您的赌注,输 = 输掉您的赌注),如果您有任何想法如何让它变得更好,请告诉我。

如果您对设计有任何想法,请告诉我,我希望看到所有想法。

代码(如果需要所有代码,请告诉我)

    import discord
    from discord.ext import commands
    import random
    import sqlite3
    from config import settings
    C_NAME = "Rucoy Coins"
    
    
    client = commands.Bot(command_prefix = settings['PREFIX'])
    client.remove_command('help')
    
    connection = sqlite3.connect('server.db')
    cursor = connection.cursor()
    
    
    @client.event
    async def on_ready():
        cursor.execute("""CREATE TABLE IF NOT EXISTS users (
            name TEXT,
            id INT,
            cash BIGINT,
            rep INT,
            lvl INT
        )""")
    
        cursor.execute("""CREATE TABLE IF NOT EXISTS shop (
            role_id INT,
            id INT,
            cost BIGINT
        )""")
    
    @client.command(aliases=['play', 'bet'])
    async def __play(ctx, amount=None):
        rand = random.randint(0, 10)
    
        result_userbal = cursor.execute("SELECT cash FROM users WHERE id = {}".format(ctx.author.id))
        if amount > int(result_userbal[0]):
            await ctx.send(f"{ctx.message.author.mention} does not have that many {C_NAME}")
            return
    
        result_userbal = cursor.execute("SELECT cash FROM users WHERE id = {}".format(ctx.author.id))
        if amount < int(result_userbal[0]):
             if rand < 3:
                cursor.execute("UPDATE users SET cash = cash + {} WHERE id = {}".format(amount, ctx.author.id))
                await ctx.send(f"{ctx.message.author.mention} You win {C_NAME}")
                connection.commit()
             else:
               cursor.execute("UPDATE users SET cash = cash - {} WHERE id = {}".format(int(amount), ctx.author.id))
               await ctx.send(f"{ctx.message.author.mention} You win {C_NAME}")
               connection.commit()

你得到的这个错误:*

Ignoring exception in command __play:
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "bot.py", line 106, in __play
    if amount > int(result_userbal[0]):
TypeError: 'sqlite3.Cursor' object is not subscriptable

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

Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'sqlite3.Cursor' object is not subscriptable

【问题讨论】:

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


    【解决方案1】:

    在 python sqlite3 库中执行查询不会返回所选项目,而是您需要执行以下操作来选择第一个匹配条目:

    cursor.execute("SOME QUERY")
    result = cursor.fetchone()
    

    或选择所有匹配项:

    cursor.execute("SOME QUERY")
    results = cursor.fetchall()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      相关资源
      最近更新 更多