From 5ec82be54821bee7888731da99b6180bd53f11bc Mon Sep 17 00:00:00 2001 From: Philipp Date: Tue, 8 Oct 2024 00:15:01 +0200 Subject: [PATCH] remove graceful stop, update to discord-py-interaction 5.0.0 We had to remove the graceful stop because it somehow blocked the bot from starting. --- example.env | 1 + src/main.py | 135 +++++++++++++++++++--------------------------------- 2 files changed, 49 insertions(+), 87 deletions(-) diff --git a/example.env b/example.env index 51b88da..c6330a2 100644 --- a/example.env +++ b/example.env @@ -3,3 +3,4 @@ MYSQL_USER=root MYSQL_PASSWORD=password MYSQL_DATABASE=database DISCORD_TOKEN=token +DISCORD_GUILD=433600034983116810 diff --git a/src/main.py b/src/main.py index c71af9a..c8777ef 100644 --- a/src/main.py +++ b/src/main.py @@ -8,101 +8,62 @@ from dotenv import load_dotenv from steamid import SteamID from loguru import logger -class GracefulDeath: - """Catch signals to allow graceful shutdown.""" - def __init__(self): - self.receivedSignal=self.receivedSignalTermSignal=False - catchSignals = [ - 1, - 2, - 3, - 10, - 12, - 15, - ] - for signum in catchSignals: - signal.signal(signum, self.handler) +load_dotenv() +token = os.getenv('DISCORD_TOKEN') +host = os.getenv('MYSQL_HOST') +user = os.getenv('MYSQL_USER') +password = os.getenv('MYSQL_PASSWORD') +db = os.getenv('MYSQL_DATABASE') +guild_id = os.getenv('DISCORD_GUILD') - def handler(self, signum): - self.lastSignal=signum - self.receivedSignal=True - if signum in [2, 3, 15]: - self.receivedSignalTermSignal=True +bot = Client() -def main(args): - sighandler = GracefulDeath() - load_dotenv() - token = os.getenv('DISCORD_TOKEN') - host = os.getenv('MYSQL_HOST') - user = os.getenv('MYSQL_USER') - password = os.getenv('MYSQL_PASSWORD') - db = os.getenv('MYSQL_DATABASE') +con = Database(host, user, password, db) - bot = Client(intents=Intents.DEFAULT) +try: + if args[0] == "init_database": + con.init_test_database() + logger.info(f"First init of database... Please disable this afterwards with the flag init_dabase") +except: + logger.info(f"Skipping database init...") - con = Database(host, user, password, db) +@listen() +async def on_ready(): + logger.info(f"We logged in as {bot.app.name}.") + +@slash_command(name="reserve_slot", description="Claim your reserved slot on the DM Server", scopes=[guild_id]) +@slash_option(name="steamid", description="Please provide your SteamID, from https://steamid.xyz/", required=True, opt_type=OptionType.STRING) +async def reserve_slot(ctx: SlashContext, steamid: str): + channel = ctx.channel.id + user = ctx.user + + if channel != 433738529898627073 and channel != 441985560144379912: + await ctx.send(f"This command only works in the Channel <#433738529898627073>!", ephemeral=True) + logger.debug(f"Command was used in the wrong channel. ({channel})") + return try: - if args[0] == "init_database": - con.init_test_database() - logger.info(f"First init of database... Please disable this afterwards with the flag init_dabase") + steam_id = SteamID(steamid) except: - logger.info(f"Skipping database init...") - pass + await ctx.send(f"You failed to give a valid SteamID as it seems, please head to and check.", ephemeral=True) + logger.debug(f"User {user.username} ({user.id}) has give a invalid SteamID. Could not convert! {steamid}") + return - @slash_command( - name="reserve_slot", - description="Claim your reserved slot on the DM Server", - scopes=[433600034983116810], - ) + if steam_id.isValid() == False: + await ctx.send(f"You failed to give a valid SteamID as it seems, please head to and check.", ephemeral=True) + logger.debug(f"User {user.username} ({user.id}) has given a invalid SteamID! Value: {steamid}") + return - @slash_option( - name="steamid", - description="Please provide your SteamID, from https://steamid.xyz/", - required=True, - opt_type=OptionType.STRING, - ) - - async def reserve_slot(ctx: SlashContext, steamid: str): - channel = ctx.channel.id - user = ctx.user - - if channel != "433738529898627073" and channel != "441985560144379912": - await ctx.send(f"This command only works in the Channel <#433738529898627073>!", ephemeral=True) - logger.debug(f"Command was used in the wrong channel. ({channel})") + try: + con.create_reserved_slot(user, steam_id.steam2(1)) + await ctx.send(f"You just claimed a reserved slot for: `{steam_id.steam2(1)}`!") + logger.debug(f"{user.id} with SteamID: {steam_id.steam2(1)} has just claimed a reserved slot.") + except Exception as err: + if str(err) != "Duplicate entrys": + await ctx.send(f"Something failed with the database, please try again later.") + logger.warning(f"Error while trying to access database. {err}") return + await ctx.send(f"You have already claimed a reserved slot!", ephemeral=True) + logger.debug(f"{steam_id.steam2(1)} or User {user.username} ({user.id}) is already in the database.") - try: - steam_id = SteamID(steamid) - except: - await ctx.send(f"You failed to give a valid SteamID as it seems, please head to and check.", ephemeral=True) - logger.debug(f"User {user.username} ({user.id}) has give a invalid SteamID. Could not convert! {steamid}") - return - - if steam_id.isValid() == False: - await ctx.send(f"You failed to give a valid SteamID as it seems, please head to and check.", ephemeral=True) - logger.debug(f"User {user.username} ({user.id}) has given a invalid SteamID! Value: {steamid}") - return - - try: - con.create_reserved_slot(user, steam_id.steam2(1)) - await ctx.send(f"You just claimed a reserved slot for: `{steam_id.steam2(1)}`!") - logger.debug(f"{user.id} with SteamID: {steam_id.steam2(1)} has just claimed a reserved slot.") - except Exception as err: - if str(err) != "Duplicate entrys": - await ctx.send(f"Something failed with the database, please try again later.") - logger.warning(f"Error while trying to access database. {err}") - return - await ctx.send(f"You have already claimed a reserved slot!", ephemeral=True) - logger.debug(f"{steam_id.steam2(1)} or User {user.username} ({user.id}) is already in the database.") - - bot.start(token) - - while True: - logger.info("Trying graceful shutdown...") - if sighandler.receivedSignal: - bot.remove("reserve_slot", remove_commands=True) - exit() - -if __name__ == '__main__': - main(sys.argv[1:]) +bot.start(token)