Reputation: 15
I was creating a discord bot, and when I used a command(!buy @Role), it crashed with:
cursor.execute("UPDATE users SET cash = cash - {} WHERE id = {}".format(("SELECT cost FROM shop WHERE role_id = {}".format(role.id)).fetchone()[0], ctx.author.id))
AttributeError: 'str' object has no attribute 'fetchone'
I don't understand what's up, because role id is integer! Here code is:
import discord
from discord.ext import commands
import sqlite3
client = commands.Bot(command_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 shop (
role_id INT,
id INT,
cost BIGINT
)""")
@client.command(aliases = ['buy', 'buy-role'])
async def __buy(ctx, role: discord.Role = None):
if role is None:
await ctx.send(f"**{ctx.author}**, choose a role, which you want to buy!")
else:
if role in ctx.author.roles:
await ctx.send(f"**{ctx.author}**, you already have this role!")
elif cursor.execute("SELECT cost FROM shop WHERE role_id = {}".format(role.id)).fetchone()[0] > cursor.execute("SELECT cash FROM users WHERE id = {}".format(ctx.author.id)).fetchone()[0]:
await ctx.send(f'**{ctx.author}**, not enough money!')
else:
await ctx.author.add_roles(role)
cursor.execute("UPDATE users SET cash = cash - {} WHERE id = {}".format(("SELECT cost FROM shop WHERE role_id = {}".format(role.id)).fetchone()[0], ctx.author.id))
connection.commit()
await ctx.message.add_reaction('✅')
client.run('MY TOKEN')
PLEASE HELP!
Upvotes: 0
Views: 1010
Reputation: 26
Try to use
cursor.execute("UPDATE users SET cash = %s WHERE id = %s", [value1, value2])
and you can create a variable for the coins in the database and then subtract the price.
Upvotes: 1