modified: bot.py

This commit is contained in:
SimolZimol
2024-10-29 18:24:13 +01:00
parent 4e217cc2ff
commit 3901b65a1e

58
bot.py
View File

@@ -278,6 +278,46 @@ def save_global_permission(user_id, permission_level):
#----------------------------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------------------------
async def update_all_users():
connection = connect_to_database()
cursor = connection.cursor()
# Hole alle Guild-IDs aus der guilds-Datenbank
cursor.execute("SELECT guild_id FROM guilds")
guilds = cursor.fetchall()
for (guild_id,) in guilds:
guild = client.get_guild(int(guild_id))
if not guild:
continue # Falls der Bot auf diesem Server nicht mehr aktiv ist, überspringen
# Aktualisiere Benutzerdaten
for member in guild.members:
user_id = member.id
user_data = load_user_data(user_id, guild_id)
# Setze Nickname und Profilbild
update_user_data(user_id, guild_id, "nickname", member.display_name)
update_user_data(user_id, guild_id, "profile_picture", str(member.avatar_url))
# Falls leave_date gesetzt ist und der Nutzer wieder auf dem Server ist, entferne leave_date
if user_data["leave_date"]:
update_user_data(user_id, guild_id, "leave_date", None)
# Überprüfe alle User in `user_data`, ob sie noch Mitglied im Server sind
cursor.execute("SELECT user_id FROM user_data WHERE guild_id = %s", (guild_id,))
all_users = cursor.fetchall()
for (user_id,) in all_users:
member = guild.get_member(int(user_id))
if member is None: # Benutzer hat den Server verlassen
# Setze leave_date, wenn der Nutzer nicht mehr auf dem Server ist
leave_date = datetime.date.today()
update_user_data(user_id, guild_id, "leave_date", leave_date)
cursor.close()
connection.close()
def save_giveaway_to_db(guild_id, platform, name, prize_uuid, game_key): def save_giveaway_to_db(guild_id, platform, name, prize_uuid, game_key):
connection = connect_to_database() connection = connect_to_database()
cursor = connection.cursor() cursor = connection.cursor()
@@ -578,6 +618,7 @@ background_data = read_background_data("background_data.txt")
@client.event @client.event
async def on_ready(): async def on_ready():
update_user_data_task.start()
client.loop.create_task(process_ai_queue()) client.loop.create_task(process_ai_queue())
logger.info("Bot is ready!") logger.info("Bot is ready!")
logger.info(f"Logged in as: {client.user.name}") logger.info(f"Logged in as: {client.user.name}")
@@ -600,6 +641,13 @@ async def on_ready():
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
logger.info("Failed to connect to version server.") logger.info("Failed to connect to version server.")
await update_all_users()
@tasks.loop(hours=24) # Läuft alle 24 Stunden
async def update_user_data_task():
await update_all_users()
@client.event @client.event
async def on_command_error(ctx, error): async def on_command_error(ctx, error):
logger.error(f"An error occurred while executing the command: {error}") logger.error(f"An error occurred while executing the command: {error}")
@@ -763,7 +811,6 @@ async def askmultus(ctx, *, prompt: str):
else: else:
await ctx.send("You don't have enough points to use this command.") await ctx.send("You don't have enough points to use this command.")
executor = concurrent.futures.ThreadPoolExecutor() executor = concurrent.futures.ThreadPoolExecutor()
async def process_ai_queue(): async def process_ai_queue():
@@ -830,8 +877,6 @@ async def process_ai_queue():
logger.error(f"Error in process_ai_queue: {e}") logger.error(f"Error in process_ai_queue: {e}")
await asyncio.sleep(5) await asyncio.sleep(5)
@client.hybrid_command() @client.hybrid_command()
async def vision(ctx, image_url: str): async def vision(ctx, image_url: str):
"""Analyzes the content of an image.""" """Analyzes the content of an image."""
@@ -957,7 +1002,6 @@ async def leave(ctx):
else: else:
await ctx.send("I am not in a voice channel.") await ctx.send("I am not in a voice channel.")
@client.hybrid_command() @client.hybrid_command()
async def toggle_feature(ctx, feature: str, state: str): async def toggle_feature(ctx, feature: str, state: str):
"""Allows admin to enable or disable features.""" """Allows admin to enable or disable features."""
@@ -987,7 +1031,6 @@ async def toggle_feature(ctx, feature: str, state: str):
await ctx.send("Please specify 'on' or 'off'.") await ctx.send("Please specify 'on' or 'off'.")
@client.hybrid_command() @client.hybrid_command()
async def version(ctx): async def version(ctx):
"""Displays the current version of the bot.""" """Displays the current version of the bot."""
@@ -1045,7 +1088,6 @@ async def addnotes(ctx, type: str, *, source: str):
else: else:
await ctx.send("Invalid type. Use 'txt' for text files or 'url' for website URLs.") await ctx.send("Invalid type. Use 'txt' for text files or 'url' for website URLs.")
@client.hybrid_command() @client.hybrid_command()
async def asknotes(ctx, *, question: str): async def asknotes(ctx, *, question: str):
"""Asks a question about the saved notes.""" """Asks a question about the saved notes."""
@@ -1076,8 +1118,6 @@ async def asknotes(ctx, *, question: str):
embed.add_field(name="Request Received", value="Your request has been added to the queue. Processing it now...") embed.add_field(name="Request Received", value="Your request has been added to the queue. Processing it now...")
await ctx.send(embed=embed) await ctx.send(embed=embed)
@client.hybrid_command() @client.hybrid_command()
async def delnotes(ctx): async def delnotes(ctx):
"""Deletes all saved notes and the asknotes history for the user.""" """Deletes all saved notes and the asknotes history for the user."""
@@ -1098,8 +1138,6 @@ async def delnotes(ctx):
else: else:
await ctx.send(f"No notes found for user {ctx.author.name}.") await ctx.send(f"No notes found for user {ctx.author.name}.")
try: try:
loop.run_until_complete(client.start(TOKEN)) loop.run_until_complete(client.start(TOKEN))
except KeyboardInterrupt: except KeyboardInterrupt: