Advertisement
yasi04

Untitled

Apr 3rd, 2024
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | None | 0 0
  1. import disnake
  2. import sqlite3
  3. from disnake.ext import commands
  4.  
  5.  
  6. bot = commands.Bot(command_prefix="!", help_command=None, intents=disnake.Intents.all())
  7.  
  8. conn = sqlite3.connect('main.db')
  9. c = conn.cursor()
  10.  
  11.  
  12. @bot.event
  13. async def on_ready():
  14.     print(f"Bot {bot.user} is ready to work!")
  15.  
  16.     c.execute("""CREATE TABLE IF NOT EXISTS users (
  17.        name TEXT,
  18.        score BIGINT,
  19.        wins INTEGER,
  20.        loses INTEGER,
  21.        winrate TEXT
  22.    )""")
  23.  
  24.     conn.commit()
  25.  
  26.     for guild in bot.guilds:
  27.         for member in guild.members:
  28.             if c.execute(f"SELECT name FROM users WHERE name = '{member}'").fetchone() is None:
  29.                 c.execute(f"INSERT INTO users VALUES ('{member}', 0, 0, 0, '0')")
  30.             else:
  31.                 pass
  32.     c.execute
  33.  
  34.     conn.commit()
  35.     print('client connected')
  36.  
  37.  
  38. @bot.slash_command(name='counter', description='Add win\lose')
  39. async def addwinlose(ctx, user: disnake.User, score: int, winlose: str = commands.Param(choices=['Win', 'Lose'])):
  40.     if ctx.author.guild_permissions.administrator:
  41.         c.execute("SELECT wins, loses, score FROM users WHERE name=?", (f'{user}',))
  42.         wins, loses, score = c.fetchone()
  43.         if winlose == "Win":
  44.             modifier = 1 + score / (wins + 1)
  45.             winrate = (((wins + 1) * modifier) / ((wins + 1) + loses))
  46.             c.execute("UPDATE users SET wins=wins+1, score=score+?, winrate=? WHERE name=?", (score, f'{winrate}', f'{user}'))
  47.         else:
  48.             modifier = 1 + score / wins
  49.             winrate = ((wins * modifier) / (wins + (loses + 1)))
  50.             c.execute("UPDATE users SET loses=loses+1, score=score+?, winrate=? WHERE name=?", (score, f'{winrate}', f'{user}'))
  51.         c.execute("SELECT score FROM users WHERE name=?", (f'{user}',))
  52.         new_score = c.fetchone[0]
  53.         await ctx.send(f'User {user} got {score} score points and now have {new_score}')
  54.     else:
  55.         await ctx.send("You do not have permissions to use this command.")
  56.     conn.commit()
  57.  
  58.  
  59. @bot.slash_command(name='info', description='Show user info')
  60. async def info(ctx, user: disnake.User):
  61.     if ctx.author.guild_permissions.administrator:
  62.         c.execute("SELECT wins, loses, score, winrate FROM users WHERE name=?", (f'{user}',))
  63.         wins, loses, score, winrate = c.fetchone()
  64.  
  65.         embed = disnake.Embed(
  66.             title=f"Info about {user}",
  67.             description=f"Score: {score}\nWins: {wins}\nLoses: {loses}\nWinrate: {winrate}",
  68.             color=0x00ff00
  69.         )
  70.  
  71.         await ctx.send(embed=embed)
  72.     else:
  73.         await ctx.send("You do not have permissions to use this command.")
  74.     conn.commit()
  75.  
  76.  
  77. @bot.slash_command(name='top10', description='Show top 10 users')
  78. async def showtop10(ctx):
  79.     if ctx.author.guild_permissions.administrator:
  80.         top10 = {}
  81.         c.execute("SELECT name FROM users")
  82.         names = c.fetchall()
  83.         for name in names:
  84.             c.execute("SELECT winrate FROM users WHERE name=?", (name[0],))
  85.             top10[name] = c.fetchone()[0]
  86.         sort_top10 = dict(sorted(top10.items(), key=lambda x: x[1], reverse=True))
  87.         for item in sort_top10:
  88.             top1 = item
  89.             break
  90.         embed = disnake.Embed(
  91.             title=f"Top 10 users",
  92.             description=f"1) {top1[0]} score {sort_top10[top1]}",
  93.             color=0x00ff00
  94.         )
  95.         del sort_top10[top1]
  96.         count = 2
  97.         for item in sort_top10:
  98.             if count != 10:
  99.                 embed.add_field(name='', value=f"{count}) {item[0]} score {sort_top10[item]}", inline=False)
  100.                 count += 1
  101.         await ctx.send(embed=embed)
  102.     else:
  103.         await ctx.send("You do not have permissions to use this command.")
  104.     conn.commit()
  105.  
  106.  
  107. bot.run('TOKEN')
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement