modified: bot.py
This commit is contained in:
94
bot.py
94
bot.py
@@ -104,13 +104,13 @@ db_cursor = db_connection.cursor()
|
|||||||
def close_database_connection(connection):
|
def close_database_connection(connection):
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|
||||||
def insert_user_data(user_id, guild_id, permission, points, ban, askmultus, filter_value, chat_history, xp=0, level=1):
|
def insert_user_data(user_id, guild_id, permission, points, ban, askmultus, filter_value, chat_history, nickname, profile_picture, join_date, xp=0, level=1):
|
||||||
insert_query = """
|
insert_query = """
|
||||||
INSERT INTO user_data (user_id, guild_id, permission, points, ban, askmultus, filter_value, rank, chat_history, xp, level)
|
INSERT INTO user_data (user_id, guild_id, permission, points, ban, askmultus, filter_value, rank, chat_history, nickname, profile_picture, join_date, xp, level, leave_date)
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||||
"""
|
"""
|
||||||
serialized_chat_history = json.dumps(chat_history)
|
serialized_chat_history = json.dumps(chat_history)
|
||||||
data = (user_id, guild_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history, xp, level)
|
data = (user_id, guild_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history, nickname, profile_picture, join_date, xp, level, None)
|
||||||
try:
|
try:
|
||||||
db_cursor.execute(insert_query, data)
|
db_cursor.execute(insert_query, data)
|
||||||
db_connection.commit()
|
db_connection.commit()
|
||||||
@@ -170,7 +170,6 @@ def load_user_data_from_mysql(user_id, guild_id):
|
|||||||
close_database_connection(connection)
|
close_database_connection(connection)
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
# Wenn das Level und die XP nicht vorhanden sind, initialisieren
|
|
||||||
user_data = {
|
user_data = {
|
||||||
"user_id": result[0],
|
"user_id": result[0],
|
||||||
"guild_id": result[1],
|
"guild_id": result[1],
|
||||||
@@ -181,28 +180,32 @@ def load_user_data_from_mysql(user_id, guild_id):
|
|||||||
"filter_value": result[6],
|
"filter_value": result[6],
|
||||||
"rank": result[7],
|
"rank": result[7],
|
||||||
"chat_history": json.loads(result[8]) if result[8] else [],
|
"chat_history": json.loads(result[8]) if result[8] else [],
|
||||||
"asknotes_history": json.loads(result[9]) if result[9] else [],
|
"nickname": result[9],
|
||||||
"xp": result[10],
|
"profile_picture": result[10],
|
||||||
"level": result[11]
|
"join_date": result[11],
|
||||||
|
"xp": result[12],
|
||||||
|
"level": result[13],
|
||||||
|
"leave_date": result[14]
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
# Wenn keine Benutzerdaten gefunden werden, neue Daten anlegen
|
# Falls keine Benutzerdaten vorhanden sind, neue Daten einfügen
|
||||||
print(f"No data found for user {user_id} in guild {guild_id}. Inserting new data.")
|
|
||||||
user_data = {
|
user_data = {
|
||||||
"user_id": user_id,
|
"user_id": user_id,
|
||||||
"guild_id": guild_id,
|
"guild_id": guild_id,
|
||||||
"permission": 0, # Standardberechtigung
|
"permission": 0,
|
||||||
"points": 0, # Standardpunkte
|
"points": 0,
|
||||||
"ban": 0, # Standardbannstatus
|
"ban": 0,
|
||||||
"askmultus": 0, # Standardwert für askmultus
|
"askmultus": 0,
|
||||||
"filter_value": 0, # Standardwert für Filter
|
"filter_value": 0,
|
||||||
"rank": 0, # Standardrang
|
"rank": 0,
|
||||||
"chat_history": [], # Leerer Chatverlauf
|
"chat_history": [],
|
||||||
"asknotes_history": [], # Leerer Chatverlauf
|
"nickname": None, # Bei Bedarf setzen
|
||||||
"xp": 0, # Standard-XP
|
"profile_picture": None, # Bei Bedarf setzen
|
||||||
"level": 1 # Standardlevel
|
"join_date": datetime.now().date(),
|
||||||
|
"xp": 0,
|
||||||
|
"level": 1,
|
||||||
|
"leave_date": None
|
||||||
}
|
}
|
||||||
|
|
||||||
insert_user_data(
|
insert_user_data(
|
||||||
user_data["user_id"],
|
user_data["user_id"],
|
||||||
user_data["guild_id"],
|
user_data["guild_id"],
|
||||||
@@ -212,6 +215,9 @@ def load_user_data_from_mysql(user_id, guild_id):
|
|||||||
user_data["askmultus"],
|
user_data["askmultus"],
|
||||||
user_data["filter_value"],
|
user_data["filter_value"],
|
||||||
user_data["chat_history"],
|
user_data["chat_history"],
|
||||||
|
user_data["nickname"],
|
||||||
|
user_data["profile_picture"],
|
||||||
|
user_data["join_date"],
|
||||||
user_data["xp"],
|
user_data["xp"],
|
||||||
user_data["level"]
|
user_data["level"]
|
||||||
)
|
)
|
||||||
@@ -272,26 +278,26 @@ def save_global_permission(user_id, permission_level):
|
|||||||
|
|
||||||
#-----------------------------------------------------------------------------------------------------------
|
#-----------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
def save_giveaway_to_db(guild_id, platform, name, prize_uuid, game_key, winner_dc_id=None):
|
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()
|
||||||
insert_query = """
|
insert_query = """
|
||||||
INSERT INTO giveaway_data (guild_id, platform, name, uuid, game_key, winner_dc_id, aktiv)
|
INSERT INTO giveaway_data (guild_id, uuid, platform, name, game_key)
|
||||||
VALUES (%s, %s, %s, %s, %s, %s)
|
VALUES (%s, %s, %s, %s, %s)
|
||||||
"""
|
"""
|
||||||
data = (guild_id, platform, name, str(prize_uuid), game_key, winner_dc_id, False)
|
data = (guild_id, str(prize_uuid), platform, name, game_key)
|
||||||
cursor.execute(insert_query, data)
|
cursor.execute(insert_query, data)
|
||||||
connection.commit()
|
connection.commit()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|
||||||
def update_winner_in_db(prize_uuid, winner_dc_id):
|
def update_winner_in_db(guild_id, prize_uuid, winner_dc_id):
|
||||||
connection = connect_to_database()
|
connection = connect_to_database()
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
update_query = """
|
update_query = """
|
||||||
UPDATE giveaway_data SET winner_dc_id = %s WHERE uuid = %s
|
UPDATE giveaway_data SET winner_dc_id = %s WHERE uuid = %s AND guild_id = %s
|
||||||
"""
|
"""
|
||||||
data = (winner_dc_id, str(prize_uuid))
|
data = (winner_dc_id, str(prize_uuid), guild_id)
|
||||||
cursor.execute(update_query, data)
|
cursor.execute(update_query, data)
|
||||||
connection.commit()
|
connection.commit()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
@@ -300,6 +306,7 @@ def update_winner_in_db(prize_uuid, winner_dc_id):
|
|||||||
class Giveaway:
|
class Giveaway:
|
||||||
def __init__(self, ctx, platform, prize, num_winners, title, subtitle, duration, end_time):
|
def __init__(self, ctx, platform, prize, num_winners, title, subtitle, duration, end_time):
|
||||||
self.ctx = ctx
|
self.ctx = ctx
|
||||||
|
self.guild_id = ctx.guild.id # Speichern der guild_id
|
||||||
self.platform = platform
|
self.platform = platform
|
||||||
self.prize = prize
|
self.prize = prize
|
||||||
self.num_winners = num_winners
|
self.num_winners = num_winners
|
||||||
@@ -312,7 +319,7 @@ class Giveaway:
|
|||||||
self.game_key = f"PREDEFINED_GAME_KEY" # Platzhalter für den tatsächlichen Game-Key
|
self.game_key = f"PREDEFINED_GAME_KEY" # Platzhalter für den tatsächlichen Game-Key
|
||||||
|
|
||||||
# Speichern des Giveaways in der Datenbank
|
# Speichern des Giveaways in der Datenbank
|
||||||
save_giveaway_to_db(ctx.guild.id, self.platform, self.title, self.prize_uuid, self.game_key)
|
save_giveaway_to_db(self.guild_id, self.platform, self.title, self.prize_uuid, self.game_key)
|
||||||
|
|
||||||
def add_participant(self, user):
|
def add_participant(self, user):
|
||||||
if user not in self.participants:
|
if user not in self.participants:
|
||||||
@@ -328,11 +335,11 @@ class Giveaway:
|
|||||||
|
|
||||||
@client.hybrid_command()
|
@client.hybrid_command()
|
||||||
async def startgiveaway(ctx, platform: str, prize: str, num_winners: int, title: str, subtitle: str, duration: str):
|
async def startgiveaway(ctx, platform: str, prize: str, num_winners: int, title: str, subtitle: str, duration: str):
|
||||||
"""Creates a giveaway. Only available for mods and higher."""
|
"""Erstellt ein neues Giveaway, das nur für Admins verfügbar ist."""
|
||||||
guild_id = ctx.guild.id
|
guild_id = ctx.guild.id
|
||||||
user_data = load_user_data(ctx.author.id, guild_id)
|
user_data = load_user_data(ctx.author.id, guild_id)
|
||||||
if user_data["permission"] < 5:
|
if user_data["permission"] < 5:
|
||||||
await ctx.send("You do not have permission to create a giveaway.")
|
await ctx.send("Du hast keine Berechtigung, ein Giveaway zu erstellen.")
|
||||||
return
|
return
|
||||||
|
|
||||||
if duration.endswith("m"):
|
if duration.endswith("m"):
|
||||||
@@ -342,7 +349,7 @@ async def startgiveaway(ctx, platform: str, prize: str, num_winners: int, title:
|
|||||||
days = int(duration[:-1])
|
days = int(duration[:-1])
|
||||||
end_time = datetime.now() + timedelta(days=days)
|
end_time = datetime.now() + timedelta(days=days)
|
||||||
else:
|
else:
|
||||||
await ctx.send("Invalid duration. Please use 'm' for minutes or 'd' for days.")
|
await ctx.send("Ungültige Dauer. Bitte 'm' für Minuten oder 'd' für Tage verwenden.")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Neues Giveaway erstellen
|
# Neues Giveaway erstellen
|
||||||
@@ -350,17 +357,17 @@ async def startgiveaway(ctx, platform: str, prize: str, num_winners: int, title:
|
|||||||
giveaway_id = len(giveaways) + 1
|
giveaway_id = len(giveaways) + 1
|
||||||
giveaways[giveaway_id] = giveaway
|
giveaways[giveaway_id] = giveaway
|
||||||
|
|
||||||
button = Button(label="Participate", style=discord.ButtonStyle.green, custom_id=f"giveaway_{giveaway_id}")
|
button = Button(label="Teilnehmen", style=discord.ButtonStyle.green, custom_id=f"giveaway_{giveaway_id}")
|
||||||
view = View()
|
view = View()
|
||||||
view.add_item(button)
|
view.add_item(button)
|
||||||
unix_end_time = int(time.mktime(end_time.timetuple()))
|
unix_end_time = int(time.mktime(end_time.timetuple()))
|
||||||
|
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(
|
||||||
title=title,
|
title=title,
|
||||||
description=f"{subtitle}\n\nPrize: {prize}\nPlatform: {platform}\nNumber of winners: {num_winners}\nEnds <t:{unix_end_time}:R>",
|
description=f"{subtitle}\n\nPreis: {prize}\nPlattform: {platform}\nAnzahl der Gewinner: {num_winners}\nEndet <t:{unix_end_time}:R>",
|
||||||
color=0x00ff00
|
color=0x00ff00
|
||||||
)
|
)
|
||||||
embed.set_footer(text=f"Giveaway endet at {end_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
embed.set_footer(text=f"Giveaway endet am {end_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||||
await ctx.send(embed=embed, view=view)
|
await ctx.send(embed=embed, view=view)
|
||||||
|
|
||||||
check_giveaway.start(giveaway_id)
|
check_giveaway.start(giveaway_id)
|
||||||
@@ -384,7 +391,6 @@ async def setlocalpermission(ctx, permission_level: int):
|
|||||||
|
|
||||||
@tasks.loop(minutes=1)
|
@tasks.loop(minutes=1)
|
||||||
async def check_giveaway(giveaway_id):
|
async def check_giveaway(giveaway_id):
|
||||||
"""Checks every 1 minute to see if the giveaway has ended."""
|
|
||||||
giveaway = giveaways.get(giveaway_id)
|
giveaway = giveaways.get(giveaway_id)
|
||||||
|
|
||||||
if giveaway and giveaway.is_finished():
|
if giveaway and giveaway.is_finished():
|
||||||
@@ -393,18 +399,16 @@ async def check_giveaway(giveaway_id):
|
|||||||
winners = giveaway.pick_winners()
|
winners = giveaway.pick_winners()
|
||||||
if winners:
|
if winners:
|
||||||
winner_mentions = ", ".join([winner.mention for winner in winners])
|
winner_mentions = ", ".join([winner.mention for winner in winners])
|
||||||
await giveaway.ctx.send(f"🎉 Congratulations to the winners of the giveaway '{giveaway.title}'! The winners are: {winner_mentions}")
|
await giveaway.ctx.send(f"🎉 Glückwunsch an die Gewinner des Giveaways '{giveaway.title}'! Die Gewinner sind: {winner_mentions}")
|
||||||
|
|
||||||
# Jeden Gewinner benachrichtigen und zur Webseite schicken
|
# Gewinner speichern und benachrichtigen
|
||||||
for winner in winners:
|
for winner in winners:
|
||||||
user_data = load_user_data(winner.id, giveaway.ctx.guild.id)
|
user_data = load_user_data(winner.id, giveaway.guild_id)
|
||||||
# Gewinner in der Datenbank speichern (winner_dc_id)
|
update_winner_in_db(giveaway.guild_id, giveaway.prize_uuid, winner.id)
|
||||||
update_winner_in_db(giveaway.prize_uuid, winner.id)
|
await winner.send(f"🎁 Glückwunsch! Du hast das Giveaway '{giveaway.title}' gewonnen!\n"
|
||||||
# Nachricht an den Gewinner senden
|
f"Bitte hole deinen Preis unter folgendem Link ab: {GIVEAWAY_WEBSITE_URL}/{giveaway.guild_id}/{giveaway.prize_uuid}")
|
||||||
await winner.send(f"🎁 Congratulations! You won the giveaway '{giveaway.title}'!\n"
|
|
||||||
f"Please claim your prize using the following link: {GIVEAWAY_WEBSITE_URL}{giveaway.prize_uuid}")
|
|
||||||
else:
|
else:
|
||||||
await giveaway.ctx.send(f"The giveaway '{giveaway.title}' has ended, but there were no participants.")
|
await giveaway.ctx.send(f"Das Giveaway '{giveaway.title}' ist beendet, aber es gab keine Teilnehmer.")
|
||||||
|
|
||||||
del giveaways[giveaway_id]
|
del giveaways[giveaway_id]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user