## Version 1.3.2

import calendar
import random
import sqlite3
import time

import nextcord
from nextcord import Intents
from nextcord.ext import commands
from nextcord.ui import Select, View



## discord stuff
intents = Intents.default()
intents.messages = True
intents.guilds = True
intents = nextcord.Intents.all()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
bot.slash_commands = True


## sql connection
connection = sqlite3.connect("test.db")
cursor = connection.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS economy (user_id INTEGER, days INTEGER, work INTEGER, coins INTEGER, time_left INTEGER, time_left_days INTEGER)")
cursor.execute("CREATE TABLE IF NOT EXISTS shop (user_id INTEGER, role_number INTEGER)")
cursor.execute("CREATE TABLE IF NOT EXISTS gay_response (word STR)")
connection.commit()
@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}')
    bot.get_guild(
    )
    print("Ready!")

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    user_id =  (message.author.id)
    cursor.execute("SELECT user_id FROM eceonomy")
    all_users = cursor.fetchall()
    for users in all_users:
      if users == user_id:
        return
      else:
        cursor.execute("INSERT INTO economy (user_id, days, work, coins, time_left, time_left_days) VALUES (?, '100', '0', '0', '0', '0')", (user_id,))
        cursor.execute("INSERT INTO shop (user_id, role_number) VALUES (?, '0')", (user_id,))
        connection.commit()
        return
    ## just to annoy :3
    cursor.execute("SELECT word FROM gay_response")
    gay = cursor.fetchone()
    for gay in gay:
      if gay in message.content.lower():
        await message.channel.send('u are gay <@' + str(message.author.id) + '>')


      
## work command
@bot.slash_command(name="work", description="Work for coins")
async def work(ctx):
  user_id = ctx.user.id
  cursor.execute("SELECT time_left FROM economy WHERE user_id = ?", (user_id,))
  time_left = cursor.fetchone()
  for rowt in time_left:
          
    current_GMT = time.gmtime()
    time_stamp = calendar.timegm(current_GMT)
    if rowt < time_stamp:

      cursor.execute("SELECT work FROM economy WHERE user_id = ?", (user_id,))
      work = cursor.fetchone()
      for roww in work:
        work = roww + 1
        cursor.execute("UPDATE economy SET work = ? WHERE user_id = ?", (work, user_id))
        connection.commit()
      num = random.randint(1,3)
      if num == 1:
        cursor.execute("SELECT coins FROM economy WHERE user_id = ?", (user_id,))
        coins = cursor.fetchone()
        for rowc in coins:
          coins = rowc + 140 + work * 10
          cursor.execute("UPDATE economy SET coins = ? WHERE user_id = ?", (coins, user_id))
          connection.commit()                
        await ctx.send('Du hilfst der **Panzertruppe** indem du einen **Leopard2A7** wartest! Du verdienst **'+ str(140 + work * 10) +' Kristalle!** Hole sie dir in **1 Stunde** ab!')
        time_stamp += 3600
        cursor.execute("UPDATE economy SET time_left = ? WHERE user_id = ?", (time_stamp, user_id))
        connection.commit()
              
      if num == 2:
        cursor.execute("SELECT coins FROM economy WHERE user_id = ?", (user_id,))
        coins = cursor.fetchone()
        for rowc in coins:
          coins = rowc + 340 + work * 10
          cursor.execute("UPDATE economy SET coins = ? WHERE user_id = ?", (coins, user_id))
          connection.commit()
        await ctx.send('Du springst als **Fallschirmjäger** von der **A-400M** ab, du erhältst dafür **' + str(340 + work * 10)+ ' Kristalle!** Hole sie dir in **2 Stunden** ab!')
        time_stamp += 7200
        cursor.execute("UPDATE economy SET time_left = ? WHERE user_id = ?", (time_stamp, user_id))
        connection.commit()
              
      if num == 3:
        cursor.execute("SELECT coins FROM economy WHERE user_id = ?", (user_id,))
        coins = cursor.fetchone()
        for rowc in coins:
          coins = rowc + 590 + work * 10
          cursor.execute("UPDATE economy SET coins = ? WHERE user_id = ?", (coins, user_id))
          connection.commit()
        await ctx.send('Du wirst als **Geleitschutz** für den **Kanzler** eingesetzt. Du erhältst dafür **'+ str(590 + work * 10) +' Kristalle!** Hole sie dir in **3 Stunden** ab!')
        time_stamp += 10800
        cursor.execute("UPDATE economy SET time_left = ? WHERE user_id = ?", (time_stamp, user_id))
        connection.commit()
    else:
      zeitg = round((rowt - time_stamp )/ 60)
      if zeitg >= 180:
        zeit = zeitg - 180
        if zeit == 0:
          await ctx.send('Du musst noch **3** Stunden warten!')
        else:
          await ctx.send('Du musst noch **3** Stunden und **' + str(zeit) + '** Minuten warten!')
      elif zeitg >= 120:
        zeit = zeitg - 120
        if zeit == 0:
          await ctx.send('Du musst noch **2** Stunden warten!')
        else:
          await ctx.send('Du musst noch **2** Stunden und **' + str(zeit) + '** Minuten warten!')
      elif zeitg >= 60:
        zeit = zeitg - 60
        if zeit == 0:
          await ctx.send('Du musst noch **1** Stunden warten!')
        else:
          await ctx.send('Du musst noch **1** Stunden und **' + str(zeit) + '** Minuten warten!')
      elif zeitg == 0:
        await ctx.send('Du musst noch ein paar Sekunden warteb!')
      else:
        await ctx.send('Du musst noch **' + str(zeitg) + '** Minuten warten!')


## daily command
@bot.slash_command(name="daily", description="Daily reward")
async def daily(ctx):
  user_id = ctx.user.id
  cursor.execute("SELECT time_left_days FROM economy WHERE user_id = ?", (user_id,))
  time_left_days = cursor.fetchone()
  for rowtd in time_left_days:
        
    current_GMT = time.gmtime()
    time_stamp = calendar.timegm(current_GMT)
    if rowtd < time_stamp:
      time_left_days = time_stamp + 86400
      cursor.execute("UPDATE economy SET time_left_days = ? WHERE user_id = ?", (time_left_days, user_id))
      connection.commit()
          
      cursor.execute("SELECT days FROM economy WHERE user_id = ?", (user_id,))
      days = cursor.fetchone()
      for rowd in days:
        num = random.randint(1,10)
        days = round(rowd * (num / 100 + 1), 2)
        cursor.execute("UPDATE economy SET days = ? WHERE user_id = ?", (days, user_id))
        connection.commit()
        cursor.execute("SELECT coins FROM economy WHERE user_id = ?", (user_id,))
        coins = cursor.fetchone()
        for rowc in coins:
          coins = round(rowc + days, 2)
          cursor.execute("UPDATE economy SET coins = ? WHERE user_id = ?", (coins, user_id))
          connection.commit
      await ctx.send('Du hast deine **Tageskarte** abgeholt! Du erhältst **'+ str(days) +' Kristalle**!')
    else:
      await ctx.send('Du kannst morgen deine nächsten Bonus abholen!')

## shop command
@bot.slash_command(name="shop", description="Shop")
async def shop(ctx):
  await ctx.send('Im Shop gibt es folgende Rollen:\n\n- Unteroffizier **[Hauptfeldwebel]** für **5.530 Kristalle**\n- Unteroffizier **[Stabsfeldwebel]** für **9.320 Kristalle**\n- Offizier **[Oberleutnant]** für **13.430 Kristalle**\n- Offzier **[Major]** für **16.890 Kristalle**\n- General **[Admiral]** für **20.000 Kristalle**\n\nUm eine Rolle zu kaufen, schreibe **/buy** gefolgt von der Rolle inder eckigen Klammer ([]). Beachte, du musst die verherige Rolle schon gekauft haben um die nächst höhere zu kaufen!')

## buy command
@bot.slash_command(name="buy", description="Kaufe eine Rolle aus dem Shop")
async def buy(
    interaction: nextcord.Interaction,
    number: str = nextcord.SlashOption(
        name="picker",
        choices={"1": 1, "Stabsfeldwebel": 2, "Oberleutnant": 3, "Major": 4, "Admiral": 5},
    ),
):
  await interaction.response.send_message(f"You chose {number}!")

  view = View()
  view.add_item(select)
  await ctx.send("Wähle eine Rolle:", view=view)
  print("1")
  interaction = await bot.wait_for("Rollen", check=lambda i: i.user == ctx.user)
  print("2")
  selected_option = interaction.values[0]
  await ctx.send(f"You selected: {selected_option}")

  user_id = ctx.user.id
  cursor.execute("SELECT coins FROM economy WHERE user_id = ?", (user_id,))
  coins = cursor.fetchone()
  cursor.execute("SELECT role_number FROM shop WHERE user_id = ?", (user_id,))
  role_number = cursor.fetchone()
  if selected_option == 'Hauptfeldwebel':
    for rowc in coins:
      if rowc >= 5530:
        for rowr in role_number:
          if rowr == 0:
            role_id = 1127589071494070292
            await user_id.add_roles(role_id)
            role_number = 1
            coins = rowc - 5530
            cursor.execute("UPDATE shop SET role_number = ? WHERE user_id = ?", (role_number, user_id))
            cursor.execute("UPDATE economy SET coins = ? WHERE user_id = ?", (coins, user_id))
            connection.commit()
            await ctx.send('Du hast dir das **Hauptfeldwebel** gekauft!')
          else:
            await ctx.send('Du hast bereits das **Hauptfeldwebel** gekauft!')
      else: 
        await ctx.send('Du hast nicht genug Kristalle!')
            
  elif selected_option == 'Stabsfeldwebel':
    for rowc in coins:
      if rowc >= 9320:
        for rowr in role_number:
          if rowr == 1:
            role_id = 1127589517310832741
            await user_id.add_roles(role_id)
            role_number = 2
            coins = rowc - 9320
            cursor.execute("UPDATE shop SET role_number = ? WHERE user_id = ?", (role_number, user_id))
            cursor.execute("UPDATE economy SET coins = ? WHERE user_id = ?", (coins, user_id))
            connection.commit()
            await ctx.send('Du hast dir das **Stabsfeldwebel** gekauft!')
          elif rowr <= 2:
            await ctx.send('Du hast bereits diese Role oder ein höhere!')
          else:
            await ctx.send('Du musst erst die vorherige Rolle kaufen!')
      else: 
        await ctx.send('Du hast nicht genug Kristalle!')

  elif selected_option == 'Oberleutnant':
    for rowc in coins:
      if rowc >= 13430:
        for rowr in role_number:
          if rowr == 2:
            role_id = 1127589698559283211
            await user_id.add_roles(role_id)
            role_number = 3
            coins = rowc - 13430
            cursor.execute("UPDATE shop SET role_number = ? WHERE user_id = ?", (role_number, user_id))
            cursor.execute("UPDATE economy SET coins = ? WHERE user_id = ?", (coins, user_id))
            connection.commit()
            await ctx.send('Du hast dir das **Oberleutnant** gekauft!')
          elif rowr <= 3:
            await ctx.send('Du hast bereits diese Role oder ein höhere!')
          else:
            await ctx.send('Du musst erst die vorherige Rolle kaufen!')
      else: 
        await ctx.send('Du hast nicht genug Kristalle!')

  elif selected_option == 'Major':
    for rowc in coins:
      if rowc >= 16890:
        for rowr in role_number:
          if rowr == 3:
            role_id = 1127589799591677954
            await user_id.add_roles(role_id)
            role_number = 4
            coins = rowc - 16890
            cursor.execute("UPDATE shop SET role_number = ? WHERE user_id = ?", (role_number, user_id))
            cursor.execute("UPDATE economy SET coins = ? WHERE user_id = ?", (coins, user_id))
            connection.commit()
            ctx.send('Du hast dir das **Major** gekauft!')
            await ctx.send('Du hast bereits diese Role oder ein höhere!')
          elif rowr <= 4:
            ctx.send("f*ck you, no role for you >:3")
          else:
            await ctx.send('Du musst erst die vorherige Rolle kaufen!')
      else: 
        await ctx.send('Du hast nicht genug Kristalle!')
  elif selected_option == 'Admiral': 

    for rowc in coins:
      if rowc >= 20000:
        for rowr in role_number:
          if rowr == 4:
            role_id = 1127589934501482526
            await user_id.add_roles(role_id)
            role_number = 5
            coins = rowc - 20000
            cursor.execute("UPDATE shop SET role_number = ? WHERE user_id = ?", (role_number, user_id))
            cursor.execute("UPDATE economy SET coins = ? WHERE user_id = ?", (coins, user_id))
            connection.commit()
            await ctx.send('Du hast dir das **Admiral** gekauft!')
          elif rowr <= 5:
            await ctx.send('Du hast bereits diese Role oder ein höhere!')
          else:
            await ctx.send('Du musst erst die vorherige Rolle kaufen!')
      else: 
        await ctx.send('Du hast nicht genug Kristalle!')
  else:
    await ctx.send('Diese Rolle gibt es nicht! Nuthe **p/shop** um die Rollen zu sehen!')

## admin geld transfer
@bot.slash.command(name="transfer", description="überträgt Geld")
async def transfer(ctx, user1: nextcord.Member, amount int, subtract: bool):
  for role in ctx.user.roles:
    if role.permission.adminstrator:
      if subtract:
        user1_id = nextcord.Member.id
        cursor.execute("SELECT coins FROM economy WHERE user_id = ?", (user1_id,))
        row = cursor.fetchone()
        for coins in row:
          newcoins = coins - amount
          if newcoins > 0:
            ctx.send("Nutzer hat zu wenig Geld")
          else:
            cursor.execute("UPDATE economy SET coins = ? WHERE user_id = ?", (newcoins, user1_id,))
            await ctx.send("Updated!")
      else:
        user1_id = nextcord.Member.id
        cursor.execute("SELECT coins FROM economy WHERE user_id = ?", (user1_id,))
        row = cursor.fetchone()
        for coins in row:
          newcoins = coins + amount
          cursor.execute("UPDATE economy SET coins = ? WHERE user_id = ?", (newcoins, user1_id,))
    else:
      await ctx.send("Du hast keine Admin rechte!")


## rank, zeigt geld eines nutzer an, ich bin top 1 :3
@bot.slash_command(name="rank", description="zeige den rank an")
async def rank(ctx, user: nextcord.Member = None):
  user_id = ctx.user.id
  if user is None:
    cursor.execute("DELETE FROM economy WHERE ROWID NOT IN (SELECT MIN(ROWID) FROM economy GROUP BY user_id)")
    cursor.execute("Select user_id, coins FROM economy ORDER BY coins DESC LIMIT 10")
    rows = cursor.fetchall()
    
    message = "Am Reichsten :\n"
    for i, row in enumerate(rows, 1):
        message += f"{i}. <@{row[0]}> hat {row[1]} Kristalle!\n"
      
    embed = nextcord.Embed(
      title="10 reichste Nutzer",
      description="",
      color=0xffffff
    )
    embed.add_field(name="", value=message, inline=True)
    embed.add_field(name="", value="", inline=False)
    embed.set_footer(text="Made by Flue the Fox :3")

    await ctx.send(embed=embed)
  elif user_id == user.id:
    cursor.execute("SELECT coins FROM economy WHERE user_id = ?", (user_id,))
    row = cursor.fetchone()
    for coins in row:
      await ctx.send('Du hast ' + str(coins) + ' Kristalle!')
  else:
    cursor.execute("SELECT coins FROM economy WHERE user_id = ?", (user_id,))
    row = cursor.fetchone()
    for coins in row:
      if coins == 0:
        broke = random.randint(1,10)
        if broke <= 9:
          await ctx.send('<@' + str(user.id) + '> ist broke lol!')
        elif broke == 10:
          await ctx.send('<@' + str(user.id) + '> kein Geld? Hier sind **10** Kristalle als Geschenk.')
          c = 10
          cursor.execute("UPDATE economy SET coins = ? WHERE user_id = ? ", (c, user_id))
      else:
        await ctx.send('<@' + str(user.id) + '> hat ' + str(coins) + ' Kristalle!')

                
                        
      
bot.run(DISCORD_TOKEN)