diff --git a/.env-dist b/.env-dist new file mode 100644 index 0000000..6f26407 --- /dev/null +++ b/.env-dist @@ -0,0 +1,14 @@ +COMPOSE_FILE=docker-compose.yml +COMPOSE_DIR=. + +MONGO_INITDB_ROOT_USERNAME=root +MONGO_INITDB_ROOT_PASSWORD=SetYourOwnPassword +MONGO_INITDB_DATABASE=pollmaster + +# official discord bot token +DISCORD_BOT_TOKEN= +# owner user id +OWNER_ID= + +# Sets the invite client id +INVITE_CLIENT_ID= \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9a0602d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM alpine:3.10.2 + +WORKDIR '/tmp' + +COPY ./pm /data/pm + +RUN apk update && apk add bash nodejs npm python3 python3-dev freetype-dev subversion +RUN apk --no-cache --update-cache add gcc gfortran build-base wget libpng-dev openblas-dev + +RUN pip3 install --upgrade pip + +WORKDIR '/data/pm' + +RUN pip install pykg-config + +RUN pip install -r requirements.txt + +CMD python3 pollmaster.py \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..1b58d1b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,42 @@ +# Use root/example as user/password credentials +version: '3.1' + +services: + # Database + mongo: + image: mongo:latest + restart: always + volumes: + - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d + environment: + - MONGO_INITDB_ROOT_USERNAME + - MONGO_INITDB_ROOT_PASSWORD + - MONGO_INITDB_DATABASE + + # App - Container, where the main application runs + app: + restart: always + volumes: + - ./pm/cogs/poll_controls.py:/data/pm/cogs/poll_controls.py + - ./pm/pollmaster.py:/data/pm/pollmaster.py + links: + - mongo + build: + context: . + dockerfile: Dockerfile + environment: + - DISCORD_BOT_TOKEN + - MONGO_INITDB_ROOT_USERNAME + - MONGO_INITDB_ROOT_PASSWORD + - OWNER_ID + - INVITE_CLIENT_ID + + # Admin Interface for Database + mongo-express: + image: mongo-express + restart: always + ports: + - 8081:8081 + environment: + ME_CONFIG_MONGODB_ADMINUSERNAME: $MONGO_INITDB_ROOT_USERNAME + ME_CONFIG_MONGODB_ADMINPASSWORD: $MONGO_INITDB_ROOT_PASSWORD \ No newline at end of file diff --git a/.gitignore b/pm/.gitignore similarity index 94% rename from .gitignore rename to pm/.gitignore index 90a0880..37f7b4f 100644 --- a/.gitignore +++ b/pm/.gitignore @@ -1,5 +1,5 @@ # IDE Folders -\.idea/ +../.idea/ # PY VENV venv/ diff --git a/LICENSE b/pm/LICENSE similarity index 100% rename from LICENSE rename to pm/LICENSE diff --git a/changelog.md b/pm/changelog.md similarity index 100% rename from changelog.md rename to pm/changelog.md diff --git a/cogs/__init__.py b/pm/cogs/__init__.py similarity index 100% rename from cogs/__init__.py rename to pm/cogs/__init__.py diff --git a/cogs/admin.py b/pm/cogs/admin.py similarity index 100% rename from cogs/admin.py rename to pm/cogs/admin.py diff --git a/cogs/config.py b/pm/cogs/config.py similarity index 100% rename from cogs/config.py rename to pm/cogs/config.py diff --git a/cogs/db_api.py b/pm/cogs/db_api.py similarity index 100% rename from cogs/db_api.py rename to pm/cogs/db_api.py diff --git a/cogs/help.py b/pm/cogs/help.py similarity index 100% rename from cogs/help.py rename to pm/cogs/help.py diff --git a/cogs/poll_controls.py b/pm/cogs/poll_controls.py similarity index 99% rename from cogs/poll_controls.py rename to pm/cogs/poll_controls.py index b6a71ca..122bbb4 100644 --- a/cogs/poll_controls.py +++ b/pm/cogs/poll_controls.py @@ -1,4 +1,5 @@ import argparse +import os import asyncio import datetime import logging @@ -8,6 +9,8 @@ import shlex import discord import pytz +import time + from discord.ext import commands from utils.misc import CustomFormatter @@ -47,7 +50,9 @@ class PollControls(commands.Cog): while True: try: if not hasattr(self.bot, 'db'): - await asyncio.sleep(30) + """await asyncio.sleep(30)""" + logger.info("DB not available yet") + time.sleep(30) continue query = self.bot.db.polls.find({'active': False, 'activation': {"$not": re.compile("0")}}) diff --git a/essentials/__init__.py b/pm/essentials/__init__.py similarity index 100% rename from essentials/__init__.py rename to pm/essentials/__init__.py diff --git a/essentials/exceptions.py b/pm/essentials/exceptions.py similarity index 100% rename from essentials/exceptions.py rename to pm/essentials/exceptions.py diff --git a/essentials/messagecache.py b/pm/essentials/messagecache.py similarity index 100% rename from essentials/messagecache.py rename to pm/essentials/messagecache.py diff --git a/essentials/multi_server.py b/pm/essentials/multi_server.py similarity index 100% rename from essentials/multi_server.py rename to pm/essentials/multi_server.py diff --git a/essentials/settings.py b/pm/essentials/settings.py similarity index 81% rename from essentials/settings.py rename to pm/essentials/settings.py index 424dc04..08da506 100644 --- a/essentials/settings.py +++ b/pm/essentials/settings.py @@ -1,4 +1,5 @@ import discord +import os from essentials.secrets import SECRETS @@ -9,11 +10,11 @@ class Settings: self.title_icon = "https://i.imgur.com/vtLsAl8.jpg" #PM self.author_icon = "https://i.imgur.com/TYbBtwB.jpg" #tag self.report_icon = "https://i.imgur.com/YksGRLN.png" #report - self.owner_id = 117687652278468610 - self.msg_errors = False + self.owner_id = os.environ.get('OWNER_ID') + self.msg_errors = True self.log_errors = True self.invite_link = \ - 'https://discordapp.com/api/oauth2/authorize?client_id=444831720659877889&permissions=126016&scope=bot' + 'https://discordapp.com/api/oauth2/authorize?client_id=%s&permissions=126016&scope=bot' % (os.environ.get('INVITE_CLIENT_ID')) self.load_secrets() diff --git a/models/__init__.py b/pm/models/__init__.py similarity index 100% rename from models/__init__.py rename to pm/models/__init__.py diff --git a/pm/models/__pycache__/__init__.cpython-36.pyc b/pm/models/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..88307b0 Binary files /dev/null and b/pm/models/__pycache__/__init__.cpython-36.pyc differ diff --git a/pm/models/__pycache__/poll.cpython-36.pyc b/pm/models/__pycache__/poll.cpython-36.pyc new file mode 100644 index 0000000..a82c806 Binary files /dev/null and b/pm/models/__pycache__/poll.cpython-36.pyc differ diff --git a/models/poll.py b/pm/models/poll.py similarity index 100% rename from models/poll.py rename to pm/models/poll.py diff --git a/pollmaster.py b/pm/pollmaster.py similarity index 93% rename from pollmaster.py rename to pm/pollmaster.py index 169d90e..8be41d4 100644 --- a/pollmaster.py +++ b/pm/pollmaster.py @@ -43,17 +43,9 @@ ch.setFormatter(formatter) logger.addHandler(fh) logger.addHandler(ch) -extensions = ['cogs.config', 'cogs.poll_controls', 'cogs.help', 'cogs.db_api', 'cogs.admin'] -for ext in extensions: - bot.load_extension(ext) - - @bot.event async def on_ready(): bot.owner = await bot.fetch_user(SETTINGS.owner_id) - - mongo = AsyncIOMotorClient(SETTINGS.mongo_db) - bot.db = mongo.pollmaster bot.session = aiohttp.ClientSession() print(bot.db) @@ -71,6 +63,7 @@ async def on_ready(): except: print("Problem verifying servers.") + logger.error("Problem verifying servers.") # cache prefixes bot.pre = {entry['_id']: entry['prefix'] async for entry in bot.db.config.find({}, {'_id', 'prefix'})} @@ -82,6 +75,7 @@ async def on_ready(): await bot.change_presence(status=discord.Status.online, activity=game) print("Servers verified. Bot running.") + logger.info("Servers verified. Bot running.") @bot.event async def on_command_error(ctx, e): @@ -136,4 +130,14 @@ async def on_guild_join(server): ) bot.pre[str(server.id)] = 'pm!' +logger.info("Connecting to Mongo DB: " + SETTINGS.mongo_db) +mongo = AsyncIOMotorClient(SETTINGS.mongo_db) +bot.db = mongo.pollmaster +print(bot.db) + +extensions = ['cogs.config', 'cogs.poll_controls', 'cogs.help', 'cogs.db_api', 'cogs.admin'] +for ext in extensions: + logger.info("Loading extension " + ext) + bot.load_extension(ext) + bot.run(SETTINGS.bot_token) diff --git a/readme.md b/pm/readme.md similarity index 100% rename from readme.md rename to pm/readme.md diff --git a/requirements.txt b/pm/requirements.txt similarity index 100% rename from requirements.txt rename to pm/requirements.txt diff --git a/utils/__init__.py b/pm/utils/__init__.py similarity index 100% rename from utils/__init__.py rename to pm/utils/__init__.py diff --git a/utils/misc.py b/pm/utils/misc.py similarity index 100% rename from utils/misc.py rename to pm/utils/misc.py diff --git a/utils/paginator.py b/pm/utils/paginator.py similarity index 100% rename from utils/paginator.py rename to pm/utils/paginator.py diff --git a/utils/poll_name_generator.py b/pm/utils/poll_name_generator.py similarity index 100% rename from utils/poll_name_generator.py rename to pm/utils/poll_name_generator.py