From 27e0fafcb73167ea25a531150a6f15a86366b872 Mon Sep 17 00:00:00 2001 From: kevin caires Date: Thu, 23 Jun 2022 10:11:05 -0300 Subject: [PATCH] INIT project. --- environment/template | 11 ++++++++ manage.py | 30 +++++++++++++++++++++ requirements/common.txt | 2 ++ settings/__init__.py | 0 settings/config.py | 60 +++++++++++++++++++++++++++++++++++++++++ utils/__init__.py | 0 utils/commands.py | 7 +++++ utils/database.py | 58 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 168 insertions(+) create mode 100644 environment/template create mode 100644 manage.py create mode 100644 requirements/common.txt create mode 100644 settings/__init__.py create mode 100644 settings/config.py create mode 100644 utils/__init__.py create mode 100644 utils/commands.py create mode 100644 utils/database.py diff --git a/environment/template b/environment/template new file mode 100644 index 0000000..fc61001 --- /dev/null +++ b/environment/template @@ -0,0 +1,11 @@ +######################## +# ENVIRONMENT TEMPLATE # +######################## + +export DISCORD_BOT_TOKEN=<> +export MYSQL_HOST=<> +export MYSQL_PORT=<> +export MYSQL_DATABASE=<> +export MYSQL_USER=<> +export MYSQL_PASSWORD=<> +export LOGLEVEL=<> diff --git a/manage.py b/manage.py new file mode 100644 index 0000000..a1f8906 --- /dev/null +++ b/manage.py @@ -0,0 +1,30 @@ +""" +Bot management. +""" +import logging +from argparse import ArgumentParser + +from settings.config import DISCORD_BOT_TOKEN +from utils.commands import client +from utils.database import DataBase + +logger = logging.getLogger(__name__) + + +manage = ArgumentParser( + description='Neeble discord bot.' +) +manage.add_argument( + 'command', + type=str, + required=True, +) +command = manage.parse_args() + +if command.command == 'run': + try: + client.run(DISCORD_BOT_TOKEN) + except Exception as ex: + logger.error(ex.args[0]) +elif command.command == 'migrate': + DataBase.migrate() diff --git a/requirements/common.txt b/requirements/common.txt new file mode 100644 index 0000000..ad1cffc --- /dev/null +++ b/requirements/common.txt @@ -0,0 +1,2 @@ +discord.py==1.7.3 +mysqlclient==2.1.1 diff --git a/settings/__init__.py b/settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/settings/config.py b/settings/config.py new file mode 100644 index 0000000..2b25b9d --- /dev/null +++ b/settings/config.py @@ -0,0 +1,60 @@ +""" +BOT configuration. +""" +import logging.config as logger +import os + +# Discord token. +DISCORD_BOT_TOKEN = os.environ.get('DISCORD_BOT_TOKEN') + +# Mysql configuration. +MYSQL_CONFIG = { + 'host': os.environ.get('MYSQL_HOST', 'localhost'), + 'port': int(os.environ.get('MYSQL_PORT', '3306')), + 'db': os.environ.get('MYSQL_DATABASE', 'neeble'), + 'user': os.environ.get('MYSQL_USER', 'neeble'), + 'password': os.environ.get('MYSQL_PASSWORD', 'neeble'), +} + +# Define the log level +LOGLEVEL = os.environ.get('LOGLEVEL', 'info').upper() + +# Configuração do logging. +LOGGING_CONFIG = { + 'version': 1, + 'disable_existing_loggers': False, + 'formatters': { + 'default': { + 'format': '[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s', + }, + }, + 'handlers': { + 'console': { + 'level': LOGLEVEL, + 'class': 'logging.StreamHandler', + 'formatter': 'default', + }, + 'logfile': { + 'level': LOGLEVEL, + 'class': 'logging.handlers.RotatingFileHandler', + 'filename': '/tmp/neeble.log', + 'maxBytes': 50000, + 'backupCount': 9, + 'formatter': 'default', + }, + }, + 'loggers': { + '': { + 'level': LOGLEVEL, + 'handlers': ['console', 'logfile'], + 'propagate': False, + }, + 'neeble': { + 'level': LOGLEVEL, + 'handlers': ['console', 'logfile'], + 'propagate': False, + }, + }, +} + +logger.dictConfig(LOGGING_CONFIG) diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/commands.py b/utils/commands.py new file mode 100644 index 0000000..239c615 --- /dev/null +++ b/utils/commands.py @@ -0,0 +1,7 @@ +""" +Bot commands. +""" +import discord +from discord.ext import commands + +client = commands.Bot(command_prefix='~') diff --git a/utils/database.py b/utils/database.py new file mode 100644 index 0000000..68d51ab --- /dev/null +++ b/utils/database.py @@ -0,0 +1,58 @@ +""" +Database utils module. +""" +import logging +import MySQLdb +from settings.config import MYSQL_CONFIG + +logger = logging.getLogger(__name__) + + +class Cursor: + """ + Context manage for database handler. + """ + def __init__(self, config: dict) -> None: + """ + Constructor. + """ + self.configuration = config + + def __enter__(self) -> 'cursor': + """ + Context manager. + """ + self.conn = MySQLdb.connect(**self.configuration, charset='utf-8') + self.cursor = self.conn.cursor() + + return self.cursor + + def __exit__(self, exc_type, exc_value, exc_trace) -> None: + """ + Exit from context manager. + """ + self.conn.commit() + self.cursor.close() + self.conn.close() + + +class DataBase: + """ + Database handler. + """ + @staticmethod + def migrate() -> None: + """ + Create tables. + """ + _sql = ''' + create if not exists neeble_quotes( + user varchar(200) not null, + quote varchar(1000) not null unique + ); + ''' + try: + with Cursor(MYSQL_CONFIG) as cursor: + cursor.execute(_sql) + except Exception as ex: + logger.error(ex.args[0])