2022-09-20 01:44:23 +02:00
|
|
|
import os
|
2022-09-24 22:34:00 +02:00
|
|
|
import sys
|
2022-09-24 23:19:14 +02:00
|
|
|
import signal
|
2022-09-20 01:44:23 +02:00
|
|
|
import interactions
|
2022-09-20 13:49:23 +02:00
|
|
|
import database
|
2022-09-20 01:44:23 +02:00
|
|
|
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
from steamid import SteamID
|
2022-10-01 09:50:28 +02:00
|
|
|
from loguru import logger
|
2022-09-20 01:44:23 +02:00
|
|
|
|
2022-09-24 23:19:14 +02:00
|
|
|
class GracefulDeath:
|
|
|
|
"""Catch signals to allow graceful shutdown."""
|
2022-10-01 09:50:28 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.receivedSignal=self.receivedSignalTermSignal=False
|
2022-09-24 23:19:14 +02:00
|
|
|
catchSignals = [
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
10,
|
|
|
|
12,
|
|
|
|
15,
|
2022-10-01 09:50:28 +02:00
|
|
|
]
|
2022-09-24 23:19:14 +02:00
|
|
|
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
|
|
|
|
|
2022-09-24 22:34:00 +02:00
|
|
|
def main(args):
|
2022-09-24 23:19:14 +02:00
|
|
|
sighandler = GracefulDeath()
|
2022-09-20 13:49:23 +02:00
|
|
|
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')
|
|
|
|
|
|
|
|
bot = interactions.Client(token=token)
|
|
|
|
|
|
|
|
mysql = database.create_connection(host, user, password, db)
|
2022-09-24 22:37:54 +02:00
|
|
|
try:
|
|
|
|
if args[0] == "init_database":
|
|
|
|
database.init_test_database(mysql)
|
2022-10-01 09:50:28 +02:00
|
|
|
logger.info(f"First init of database... Please disable this afterwards with the flag init_dabase")
|
2022-09-24 22:37:54 +02:00
|
|
|
except:
|
2022-10-01 09:50:28 +02:00
|
|
|
logger.info(f"Skipping database init...")
|
2022-09-24 22:37:54 +02:00
|
|
|
pass
|
2022-09-20 13:49:23 +02:00
|
|
|
|
|
|
|
@bot.command(
|
|
|
|
name="reserve_slot",
|
|
|
|
description="Claim your reserved slot on the DM Server",
|
|
|
|
scope=433600034983116810,
|
|
|
|
options=[
|
|
|
|
interactions.Option(
|
|
|
|
name="steamid",
|
|
|
|
description="Please provide your SteamID, from https://steamid.xyz/",
|
|
|
|
type=interactions.OptionType.STRING,
|
|
|
|
required=True,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
async def reserve_slot(ctx: interactions.CommandContext, steamid: str):
|
2022-09-24 22:20:42 +02:00
|
|
|
channel = ctx.channel.id
|
2022-09-24 23:19:14 +02:00
|
|
|
user = ctx.user
|
2022-09-24 22:20:42 +02:00
|
|
|
|
|
|
|
if channel != "433738529898627073":
|
|
|
|
await ctx.send(f"This command only works in the Channel <#433738529898627073>!", ephemeral=True)
|
2022-10-01 09:50:28 +02:00
|
|
|
logger.debug(f"Command was used in the wrong channel. ({channel})")
|
2022-09-24 22:20:42 +02:00
|
|
|
return
|
|
|
|
|
2022-09-24 23:19:14 +02:00
|
|
|
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)
|
2022-10-01 09:50:28 +02:00
|
|
|
logger.debug(f"User ({user.id}) has give a invalid SteamID. Could not convert!")
|
2022-09-24 23:19:14 +02:00
|
|
|
return
|
|
|
|
|
2022-09-24 22:20:42 +02:00
|
|
|
if steam_id.isValid() == False:
|
2022-09-24 23:19:14 +02:00
|
|
|
await ctx.send(f"You failed to give a valid SteamID as it seems, please head to <https://steamid.xyz/> and check.", ephemeral=True)
|
2022-10-01 09:50:28 +02:00
|
|
|
logger.debug(f"User ({user.id}) has given a invalid SteamID!")
|
2022-09-24 22:20:42 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
database.create_reserved_slot(mysql, user, steam_id.steam2(1))
|
|
|
|
await ctx.send(f"You just claimed a reserved slot for: `{steam_id.steam2(1)}`!")
|
2022-10-01 09:50:28 +02:00
|
|
|
logger.debug(f"{steam_id.steam2(1)} has just claimed a reserved slot.")
|
2022-09-24 22:20:42 +02:00
|
|
|
except Exception as err:
|
|
|
|
if str(err) != "Duplicate entrys":
|
|
|
|
await ctx.send(f"Something failed with the database, please try again later.", ephemeral=True)
|
2022-10-01 09:50:28 +02:00
|
|
|
logger.warning(f"Error while trying to access database.", err)
|
2022-09-24 22:20:42 +02:00
|
|
|
return
|
|
|
|
await ctx.send(f"You have already claimed a reserved slot!", ephemeral=True)
|
2022-10-01 09:50:28 +02:00
|
|
|
logger.debug(f"{steam_id.steam2(1)} or {user.id} is already in the database.")
|
2022-09-20 13:49:23 +02:00
|
|
|
|
|
|
|
bot.start()
|
|
|
|
|
2022-09-24 23:19:14 +02:00
|
|
|
while True:
|
2022-10-01 09:50:28 +02:00
|
|
|
logger.info("Trying graceful shutdown...")
|
2022-09-24 23:19:14 +02:00
|
|
|
if sighandler.receivedSignal:
|
|
|
|
bot.remove("reserve_slot", remove_commands=True)
|
|
|
|
exit()
|
2022-09-20 13:49:23 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-09-24 22:34:00 +02:00
|
|
|
main(sys.argv[1:])
|