thimabo

Untitled

Jan 30th, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. # Check if the SQLite database file exists
  2. if not os.path.exists('work_hours.db'):
  3. print("Database file 'work_hours.db' not found. Creating a new one.")
  4. open('work_hours.db', 'w').close() # Create an empty file
  5.  
  6. # Create or connect to the SQLite database
  7. conn = sqlite3.connect('work_hours.db')
  8. cursor = conn.cursor()
  9. cursor.execute('''
  10. CREATE TABLE IF NOT EXISTS work_hours (
  11. user_id INTEGER,
  12. total_hours INTEGER,
  13. clock_in DATETIME,
  14. clock_out DATETIME
  15. )
  16. ''')
  17. conn.commit()
  18.  
  19. @bot.tree.command()
  20. async def inklokken(interaction:discord.Interaction):
  21. user_id = interaction.author.id
  22. clock_in_time = datetime.now()
  23.  
  24. # Check if the user is already clocked in
  25. cursor.execute('SELECT * FROM work_hours WHERE user_id = ? AND clock_out IS NULL', (user_id,))
  26. existing_entry = cursor.fetchone()
  27.  
  28. embed = discord.Embed(title="Gewerkte uren", color=kleur)
  29. amsterdam_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  30. embed.set_thumbnail(url=logo)
  31. embed.set_image(url=banner_logo)
  32. embed.set_footer(text=f"Tijd: {amsterdam_time}")
  33.  
  34. if existing_entry:
  35. embed.add_field(name=f"{user_id} heeft:", value=f"is al ingeklokt", inline=False)
  36. else:
  37. cursor.execute('INSERT INTO work_hours (user_id, total_hours, clock_in) VALUES (?, 0, ?)', (user_id, clock_in_time))
  38. conn.commit()
  39. embed.add_field(name=f"{user_id} heeft:", value=f"succesvol ingeklokt", inline=False)
  40.  
  41. await interaction.response.send_message(laadgif)
  42. time.sleep(4)
  43. await interaction.edit_original_response(embed=embed, content=None)
  44.  
  45. @bot.tree.command()
  46. async def uitklokken(interaction:discord.Interaction):
  47. user_id = interaction.author.id
  48. clock_out_time = datetime.now()
  49.  
  50. # Update the clock_out time for the latest entry of the user
  51. cursor.execute('UPDATE work_hours SET clock_out = ? WHERE user_id = ? AND clock_out IS NULL', (clock_out_time, user_id))
  52.  
  53. # Calculate the hours worked and update the total_hours
  54. cursor.execute('SELECT clock_in, clock_out FROM work_hours WHERE user_id = ? AND clock_out IS NOT NULL', (user_id,))
  55. entries = cursor.fetchall()
  56.  
  57. total_hours = 0
  58. for entry in entries:
  59. clock_in = datetime.strptime(entry[0], '%Y-%m-%d %H:%M:%S.%f') # Add %f for microseconds
  60. clock_out = datetime.strptime(entry[1], '%Y-%m-%d %H:%M:%S.%f')
  61. total_hours += (clock_out - clock_in).total_seconds()
  62.  
  63. cursor.execute('UPDATE work_hours SET total_hours = ? WHERE user_id = ?', (total_hours, user_id))
  64. conn.commit()
  65.  
  66. embed = discord.Embed(title="Gewerkte uren", color=kleur)
  67. amsterdam_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  68. embed.set_thumbnail(url=logo)
  69. embed.set_image(url=banner_logo)
  70. embed.set_footer(text=f"Tijd: {amsterdam_time}")
  71. embed.add_field(name=f"{user_id} heeft:", value=f"succesvol uitgeklokt", inline=False)
  72.  
  73. await interaction.response.send_message(laadgif)
  74. time.sleep(4)
  75. await interaction.edit_original_response(embed=embed, content=None)
  76.  
  77.  
  78. @bot.tree.command()
  79. async def check_hours(interaction:discord.Interaction):
  80. user_id = interaction.author.id
  81.  
  82. # Retrieve the total hours worked for the user
  83. cursor.execute('SELECT total_hours FROM work_hours WHERE user_id = ?', (user_id,))
  84. total_hours = cursor.fetchone()
  85.  
  86. embed = discord.Embed(title="Gewerkte uren", color=kleur)
  87. amsterdam_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  88. embed.set_thumbnail(url=logo)
  89. embed.set_image(url=banner_logo)
  90. embed.set_footer(text=f"Tijd: {amsterdam_time}")
  91.  
  92. if total_hours is not None:
  93. hours, remainder = divmod(total_hours[0], 3600)
  94. minutes, seconds = divmod(remainder, 60)
  95. embed.add_field(name=f"{user_id} heeft:", value=f"{int(hours)} uur en {int(minutes)} minuten gewerkt", inline=False)
  96. else:
  97. embed.add_field(name=f"{user_id} heeft:", value=f"geen werkuren gevonden", inline=False)
  98.  
  99. await interaction.response.send_message(laadgif)
  100. time.sleep(4)
  101. await interaction.edit_original_response(embed=embed, content=None)
Advertisement
Add Comment
Please, Sign In to add comment