fix order to make more sense, GracefulShutdown?

This commit is contained in:
Philipp 2022-09-24 23:19:14 +02:00
parent 6835bf5358
commit 9d30aea5a8
Signed by: Spaenny
GPG Key ID: 9EBD8439AFBAB750
1 changed files with 37 additions and 4 deletions

View File

@ -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 <https://steamid.xyz/> 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 <https://steamid.xyz/ and check.", ephemeral=True)
await ctx.send(f"You failed to give a valid SteamID as it seems, please head to <https://steamid.xyz/> 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:])