INIT project.

pull/3/head
kevin caires 3 years ago
commit 27e0fafcb7

@ -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=<>

@ -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()

@ -0,0 +1,2 @@
discord.py==1.7.3
mysqlclient==2.1.1

@ -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)

@ -0,0 +1,7 @@
"""
Bot commands.
"""
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='~')

@ -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])
Loading…
Cancel
Save