diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..87c86b8 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,13 @@ +.git +.gitignore +*.service +README.md +.editorconfig +**/__pycache__ +.pyc +venv +Pipfile* +.vscode +.swp +*.tar +*nginx.conf diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..997883a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +# EditorConfig is awesome: https://EditorConfig.org + +root = true + +[*] +indent_style = tab +indent_size = 4 +end_of_line = crlf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +indent_style = space +indent_size = 2 +trim_trailing_whitespace = false diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c79d6bb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.8-buster +WORKDIR /usr/src/app +COPY . . +RUN pip install --no-cache-dir -r requirements.txt +CMD python ./run_chessbot.py diff --git a/chessbot/config.py b/chessbot/config.py index 31a5ec0..dd64794 100644 --- a/chessbot/config.py +++ b/chessbot/config.py @@ -1,13 +1,13 @@ - import chess.svg +from os import getenv -WEBHOOK_PORT = 7003 +WEBHOOK_PORT = int(getenv('WEBHOOK_PORT', '7003')) -BOT_INVITE_LINK = "https://discord.com/oauth2/authorize?client_id=366770566331629579&scope=bot&permissions=52288" -GITHUB_LINK = "https://github.com/qwertyquerty/ChessBot" +BOT_INVITE_LINK = getenv('BOT_INVITE_LINK', '') +GITHUB_LINK = getenv('GITHUB_LINK', "https://github.com/qwertyquerty/ChessBot") -BOTURL = "https://discordbots.org/bot/366770566331629579" -MOTD = "" +BOTURL = getenv('BOTURL', "https://discordbots.org/bot/366770566331629579") +MOTD = getenv('MOTD', '') COLOR_WHITE = True COLOR_BLACK = False @@ -73,7 +73,7 @@ BOTVOTEURL = "https://top.gg/bot/366770566331629579/vote" SERVERVOTEURL = "https://top.gg/servers/430504476458221570/vote" -PREFIX = "|" +PREFIX = getenv('PREFIX', "|") CHESSBOTSERVER = 430504476458221570 diff --git a/chessbot/db.py b/chessbot/db.py index e9872bd..0af9f11 100644 --- a/chessbot/db.py +++ b/chessbot/db.py @@ -11,7 +11,15 @@ import chess.variant import chess.pgn -client = MongoClient() +from os import getenv + +client = MongoClient( + host=getenv('DB_HOST', 'localhost'), + port=int(getenv('DB_PORT', '27017')), + username=getenv('DB_USER', 'chessbot'), + password=getenv('DB_PASSWORD', 'QsfEJLEURQHWoM+n7CpBNQOjUWgJIcXcpzWTb5m1iwM='), + authSource=getenv('DB_NAME', 'chess'), + authMechanism=getenv('DB_AUTH_MECHANISM', 'SCRAM-SHA-256')) db = client.chess users = db.users @@ -162,7 +170,7 @@ def __init__(self,d): ### STUFF THAT RELIES ON FETCHING GAMES THAT WE WILL LAZY LOAD WHEN NEEDED ### self._list_of_games = None self._badges = None - + self.votes = d["votes"] self.bio = d["bio"] self.flags = d["flags"] @@ -221,7 +229,7 @@ def unblacklist(self): self.set('flags', self.flags&~USER_FLAG_BLACKLISTED) rating_sync() return self - + def get_rank(self): rank_cur = db.users.find().sort("rating", -1) i = 0 @@ -229,28 +237,28 @@ def get_rank(self): for user in rank_cur: if user["id"] == self.id: return i - + i += 1 - + def update_glicko(self, glicko): self.collection.update_one({"_id": ObjectId(self._id)}, {"$set": { "rating": glicko.mu, "rating_deviation": glicko.phi, "rating_volatility": glicko.sigma }}) - + def render_rating(self): rendered_rating = int(round(self.rating, 0)) if self.rating_deviation >= GLICKO_PROVISIONAL_MIN_PHI: rendered_rating = f"{rendered_rating}?" - + return rendered_rating - + def list_of_games(self): # Lazy load in the list of games only when needed if self._list_of_games == None: self._list_of_games = list(games.find({"$and": [{"$or": [{"1":self.id}, {"2":self.id}]}, {"valid": True}]})) # Get all valid games the user is in - + return self._list_of_games def badges(self): @@ -272,7 +280,7 @@ def badges(self): if self.flags & USER_FLAG_TOURNAMENT_2ND: self._badges.append("tournament-second-place") if self.flags & USER_FLAG_PATRON: self._badges.append("patron") if self.flags & USER_FLAG_MASTER: self._badges.append("master") - + return self._badges def win_count(self):