[英]How to fix it? (python3, discord.py and sqlite3)
如何解决? (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 sqlite3 库中执行查询不返回所选项目,而是您需要对 select 第一个匹配条目执行类似操作:
cursor.execute("SOME QUERY")
result = cursor.fetchone()
或 select 所有匹配项:
cursor.execute("SOME QUERY")
results = cursor.fetchall()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.