first commit

This commit is contained in:
Philipp 2022-09-20 01:44:23 +02:00
commit c5b2844be3
Signed by: Spaenny
GPG Key ID: 9EBD8439AFBAB750
5 changed files with 268 additions and 0 deletions

161
.gitignore vendored Normal file
View File

@ -0,0 +1,161 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

18
compose.yml Normal file
View File

@ -0,0 +1,18 @@
services:
db:
container_name: db
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
app:
container_name: app
image: python:latest
depends_on:
- db
volumes:
- .:/code
entrypoint: ./code/docker-entrypoint.sh

8
docker-entrypoint.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
echo "Installing requirements..."
pip install -r /code/requirements.txt
echo "Starting discord bot..."
python -u /code/src/main.py

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
discord-py-interactions==4.3.1
steamid==1.0.2
python-dotenv==0.21.0
mysql-connector-python==8.0.30

77
src/main.py Normal file
View File

@ -0,0 +1,77 @@
import os
import interactions
import mysql.connector
from dotenv import load_dotenv
from steamid import SteamID
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)
db = mysql.connector.connect(
host=host,
user="root",
password=password,
database=db,
)
cursor = db.cursor()
def create_tables():
cursor.execute("CREATE TABLE discord_link ( admin_id int(10) UNSIGNED NOT NULL, discord_id bigint(20) UNSIGNED NOT NULL, steamid varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL);")
cursor.execute("CREATE TABLE sm_admins ( id int(10) UNSIGNED NOT NULL, authtype enum('steam', 'name', 'ip') COLLATE utf8mb4_unicode_ci NOT NULL, identity varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL, password varchar(65) COLLATE utf8mb4_unicode_ci DEFAULT NULL, flags varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, name varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL, immunity int(10) UNSIGNED NOT NULL);")
cursor.execute("ALTER TABLE discord_link ADD PRIMARY KEY (admin_id), ADD UNIQUE KEY admin_id (admin_id), ADD UNIQUE KEY discord_id (discord_id), ADD UNIQUE KEY steamid (steamid);")
cursor.execute("ALTER TABLE sm_admins ADD PRIMARY KEY (id), ADD UNIQUE KEY identity (identity), ADD UNIQUE KEY id (id);")
cursor.execute("ALTER TABLE sm_admins MODIFY id int(10) UNSIGNED NOT NULL AUTO_INCREMENT;")
cursor.execute("ALTER TABLE discord_link ADD CONSTRAINT discord_link_ibfk_1 FOREIGN KEY (admin_id) REFERENCES sm_admins (id);")
create_tables()
@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):
steam_id = SteamID(steamid)
user = ctx.user
if steam_id.isValid() == True:
#try:
sql_sm = "INSERT INTO sm_admins (authtype, identity, password, flags, name, immunity) VALUES (%s, %s, %s, %s, %s, %s)"
val_sm = ("steam", steam_id.steam2(1), "", "a", user.username, "5")
cursor.execute(sql_sm, val_sm)
db.commit()
id = cursor.lastrowid
sql_discord = "INSERT INTO discord_link (admin_id, discord_id, steamid) VALUES (%s, %s, %s)"
print(id, user.id, steam_id.steam2(1))
val_discord = (id, str(user.id), steam_id.steam2(1))
cursor.execute(sql_discord, val_discord)
db.commit()
await ctx.send(f"You just claimed a reserved slot for: `{steam_id.steam2(1)}`!")
#except:
await ctx.send(f"Something failed with the database, please try again later.")
else:
await ctx.send(f"You failed to give a valid SteamID as it seems, please head to <https://steamid.xyz/> and check.")
bot.start()