fix order to make more sense, GracefulShutdown?
This commit is contained in:
parent
6835bf5358
commit
9d30aea5a8
1 changed files with 37 additions and 4 deletions
41
src/main.py
41
src/main.py
|
@ -1,12 +1,36 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import signal
|
||||||
import interactions
|
import interactions
|
||||||
import database
|
import database
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from steamid import SteamID
|
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):
|
def main(args):
|
||||||
|
sighandler = GracefulDeath()
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
token = os.getenv('DISCORD_TOKEN')
|
token = os.getenv('DISCORD_TOKEN')
|
||||||
host = os.getenv('MYSQL_HOST')
|
host = os.getenv('MYSQL_HOST')
|
||||||
|
@ -39,16 +63,21 @@ def main(args):
|
||||||
)
|
)
|
||||||
|
|
||||||
async def reserve_slot(ctx: interactions.CommandContext, steamid: str):
|
async def reserve_slot(ctx: interactions.CommandContext, steamid: str):
|
||||||
steam_id = SteamID(steamid)
|
|
||||||
user = ctx.user
|
|
||||||
channel = ctx.channel.id
|
channel = ctx.channel.id
|
||||||
|
user = ctx.user
|
||||||
|
|
||||||
if channel != "433738529898627073":
|
if channel != "433738529898627073":
|
||||||
await ctx.send(f"This command only works in the Channel <#433738529898627073>!", ephemeral=True)
|
await ctx.send(f"This command only works in the Channel <#433738529898627073>!", ephemeral=True)
|
||||||
return
|
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:
|
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
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -62,7 +91,11 @@ def main(args):
|
||||||
|
|
||||||
bot.start()
|
bot.start()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
print("Graceful shutdown...")
|
||||||
|
if sighandler.receivedSignal:
|
||||||
|
bot.remove("reserve_slot", remove_commands=True)
|
||||||
|
exit()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main(sys.argv[1:])
|
main(sys.argv[1:])
|
||||||
|
|
Loading…
Reference in a new issue