Advertisement
yasi04

Untitled

Apr 3rd, 2024
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.87 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, old_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 + ((old_score + score) / wins)
  49.             winrate = ((wins * modifier) / (wins + (loses + 1)))
  50.             print(old_score + score, wins, loses, modifier, winrate)
  51.             c.execute("UPDATE users SET loses=loses+1, score=score+?, winrate=? WHERE name=?", (score, f'{winrate}', f'{user}'))
  52.         c.execute("SELECT score FROM users WHERE name=?", (f'{user}',))
  53.         new_score = c.fetchone()[0]
  54.         await ctx.send(f'User {user} got {score} score points and now have {new_score}')
  55.     else:
  56.         await ctx.send("You do not have permissions to use this command.")
  57.     conn.commit()
  58.  
  59.  
  60. @bot.slash_command(name='info', description='Show user info')
  61. async def info(ctx, user: disnake.User):
  62.     if ctx.author.guild_permissions.administrator:
  63.         c.execute("SELECT wins, loses, score, winrate FROM users WHERE name=?", (f'{user}',))
  64.         wins, loses, score, winrate = c.fetchone()
  65.  
  66.         embed = disnake.Embed(
  67.             title=f"Info about {user}",
  68.             description=f"Score: {score}\nWins: {wins}\nLoses: {loses}\nWinrate: {float(winrate):.2f}",
  69.             color=0x00ff00
  70.         )
  71.  
  72.         await ctx.send(embed=embed)
  73.     else:
  74.         await ctx.send("You do not have permissions to use this command.")
  75.     conn.commit()
  76.  
  77.  
  78. @bot.slash_command(name='top10', description='Show top 10 users')
  79. async def showtop10(ctx):
  80.     if ctx.author.guild_permissions.administrator:
  81.         top10 = {}
  82.         c.execute("SELECT name FROM users")
  83.         names = c.fetchall()
  84.         for name in names:
  85.             c.execute("SELECT winrate FROM users WHERE name=?", (name[0],))
  86.             top10[name] = c.fetchone()[0]
  87.         sort_top10 = dict(sorted(top10.items(), key=lambda x: x[1], reverse=True))
  88.         for item in sort_top10:
  89.             top1 = item
  90.             break
  91.         embed = disnake.Embed(
  92.             title=f"Top 10 users",
  93.             description=f"1) {top1[0]} score {sort_top10[top1]}",
  94.             color=0x00ff00
  95.         )
  96.         del sort_top10[top1]
  97.         count = 2
  98.         for item in sort_top10:
  99.             if count != 10:
  100.                 embed.add_field(name='', value=f"{count}) {item[0]} score {sort_top10[item]}", inline=False)
  101.                 count += 1
  102.         await ctx.send(embed=embed)
  103.     else:
  104.         await ctx.send("You do not have permissions to use this command.")
  105.     conn.commit()
  106.  
  107.  
  108. bot.run('TOKEN')
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement