diff --git a/src/main.py b/src/main.py index 78a8a4a..20c17cc 100644 --- a/src/main.py +++ b/src/main.py @@ -1,12 +1,36 @@ import os import sys +import signal import interactions import database from dotenv import load_dotenv from steamid import SteamID +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) + + def handler(self, signum, frame): + self.lastSignal=signum + self.receivedSignal=True + if signum in [2, 3, 15]: + self.receivedSignalTermSignal=True + def main(args): + sighandler = GracefulDeath() load_dotenv() token = os.getenv('DISCORD_TOKEN') host = os.getenv('MYSQL_HOST') @@ -39,16 +63,21 @@ def main(args): ) async def reserve_slot(ctx: interactions.CommandContext, steamid: str): - steam_id = SteamID(steamid) - user = ctx.user channel = ctx.channel.id + user = ctx.user if channel != "433738529898627073": await ctx.send(f"This command only works in the Channel <#433738529898627073>!", ephemeral=True) return + 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) + 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) return try: @@ -62,7 +91,11 @@ def main(args): bot.start() - + while True: + print("Graceful shutdown...") + if sighandler.receivedSignal: + bot.remove("reserve_slot", remove_commands=True) + exit() if __name__ == '__main__': main(sys.argv[1:])