Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- from discord.ext import commands
- from discord import app_commands
- import mysql.connector
- intents = discord.Intents.default()
- client = commands.Bot(command_prefix="-", intents=intents)
- tree = client.tree
- @client.event
- async def on_ready():
- synced = await tree.sync()
- print(f"Synced {len(synced)} Command(s)")
- db = mysql.connector.connect(
- host="localhost",
- user="root",
- password="",
- database="test"
- )
- cursor = db.cursor(dictionary=True)
- @tree.command(name="set", description="sets user balance")
- @app_commands.describe(balance="amount of money")
- async def set(interaction: discord.Interaction, balance: int):
- sql = "INSERT INTO users (ID, BALANCE) VALUES (%s, %s)"
- val = (interaction.user.id, balance)
- cursor.execute(sql, val)
- db.commit()
- await interaction.response.send_message("Done")
- @tree.command(name="update", description="updates user balance")
- @app_commands.describe(balance="amount of money")
- async def update(interaction: discord.Interaction, balance: int):
- sql = "UPDATE users SET BALANCE=%s WHERE ID=%s"
- val = (balance, interaction.user.id)
- cursor.execute(sql, val)
- db.commit()
- await interaction.response.send_message("Done")
- @tree.command(name="check", description="checks user balance")
- async def check(interaction:discord.Interaction):
- sql = f"SELECT BALANCE FROM users WHERE ID={interaction.user.id}"
- cursor.execute(sql)
- rows = cursor.fetchall()
- # [{'BALANCE': '150'}]
- for row in rows:
- balance = row["BALANCE"]
- await interaction.response.send_message(balance)
- client.run("TOKEN")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement