如何修复它?(python3、discord.py和sqlite3)

2024-04-20 08:22:33 发布

您现在位置:Python中文网/ 问答频道 /正文

如何修复它?(python3、discord.py和sqlite3)

我试着做cosino discord bot(30%赢=你赌的2倍,输=你赌的钱),如果你有任何想法如何让它变得更好,请告诉我

如果你有任何设计的想法告诉我,我想看看所有的想法

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

    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

Tags: inpyimportidexecutecashawaitamount
1条回答
网友
1楼 · 发布于 2024-04-20 08:22:33

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

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

或选择所有匹配项:

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

相关问题 更多 >