Skip to content
This repository was archived by the owner on Mar 25, 2023. It is now read-only.

Dockerization 🐳 #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.git
.gitignore
*.service
README.md
.editorconfig
**/__pycache__
.pyc
venv
Pipfile*
.vscode
.swp
*.tar
*nginx.conf
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
14 changes: 7 additions & 7 deletions chessbot/config.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -73,7 +73,7 @@
BOTVOTEURL = "https://top.gg/bot/366770566331629579/vote"
SERVERVOTEURL = "https://top.gg/servers/430504476458221570/vote"

PREFIX = "|"
PREFIX = getenv('PREFIX', "|")

CHESSBOTSERVER = 430504476458221570

Expand Down
28 changes: 18 additions & 10 deletions chessbot/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -221,36 +229,36 @@ 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

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):
Expand All @@ -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):
Expand Down