tldr: refactoring

This commit is contained in:
Romain J 2019-12-16 18:12:10 +01:00
commit f42b2194cd
2881 changed files with 568359 additions and 388 deletions

View file

@ -153,15 +153,29 @@ class Logs(commands.Cog):
await self.webhook.send(embed=e)
@commands.Cog.listener()
async def on_guild_join(self, guild):
async def on_guild_join(self, guild: discord.guild):
e = discord.Embed(colour=0x53dda4, title='New Guild') # green colour
await self.send_guild_stats(e, guild)
@commands.Cog.listener()
async def on_guild_remove(self, guild):
async def on_guild_remove(self, guild: discord.guild):
e = discord.Embed(colour=0xdd5f53, title='Left Guild') # red colour
await self.send_guild_stats(e, guild)
@commands.Cog.listener()
async def on_message(self, message: discord.message):
if message.guild is None:
e = discord.Embed(colour=0x0a97f5, title='New DM') # blue colour
e.set_author(
name=message.author,
icon_url=message.author.avatar_url_as(format='png')
)
e.description = message.content
if len(message.attachments) > 0:
e.set_image(url=message.attachments[0].url)
e.set_footer(text=f"User ID: {message.author.id}")
await self.webhook.send(embed=e)
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
await self.register_command(ctx)
@ -246,12 +260,14 @@ class Logs(commands.Cog):
minutes = delta.total_seconds() / 60
total = sum(self.bot.socket_stats.values())
cpm = total / minutes
await ctx.send(f'{total} socket events observed ({cpm:.2f}/minute):\n{self.bot.socket_stats}')
await ctx.send(
f'{total} socket events observed ({cpm:.2f}/minute):\n{self.bot.socket_stats}')
@commands.command(name='uptime')
async def _uptime(self, ctx):
"""Tells you how long the bot has been up for."""
uptime = humanize.naturaltime(datetime.datetime.utcnow() - self.bot.uptime)
uptime = humanize.naturaltime(
datetime.datetime.utcnow() - self.bot.uptime)
await ctx.send(f'Uptime: **{uptime}**')

View file

@ -22,12 +22,9 @@ class Polls(commands.Cog):
.query(Poll) \
.filter(Poll.message_id == pld.message_id)
print("-------------------------25---------------------------")
if poll.count() != 0:
print("-------------------------27---------------------------")
if poll.count() > 0:
poll = poll.one()
emotes = utils_emotes.get(poll.available_choices)
if pld.emoji.name in emotes:
return poll
@ -52,6 +49,8 @@ class Polls(commands.Cog):
pass
choice = utils_emotes.get_index(pld.emoji.name)
print(choice)
response = self.bot.database.session.query(Poll) \
.filter(
Responses.poll_id == poll.id,
@ -59,18 +58,20 @@ class Polls(commands.Cog):
Responses.choice == choice
)
print(pld.user_id, poll.id, choice)
if response.count() != 0:
print("--pre delete--")
response = response.one()
self.bot.database.session.delete(response)
print("--post delete--")
else:
print("--pre add--")
response = Responses(
user=pld.user_id,
poll_id=poll.id,
choice=choice
)
self.bot.database.session.add(response)
print("--post add--")
self.bot.database.session.commit()
"""---------------------------------------------------------------------"""

View file

@ -1,4 +1,4 @@
import json
import configparser
class Config:
@ -7,11 +7,8 @@ class Config:
def __init__(self, name):
self.name = name
try:
with open(self.name, 'r') as f:
self._db = json.load(f)
except FileNotFoundError:
self._db = {}
self._db: configparser.ConfigParser = configparser.ConfigParser()
self._db.read(self.name)
def __contains__(self, item):
return item in self._db
@ -19,9 +16,8 @@ class Config:
def __getitem__(self, item):
return self._db[str(item)]
def get(self, key, *args):
"""Retrieves a config entry."""
return self._db.get(str(key), *args)
def all(self) -> list:
return self._db.sections()
def all(self) -> dict:
return self._db
def get(self, *args, **kwargs) -> str:
return self._db.get(*args, **kwargs)

View file

@ -1,10 +1,16 @@
from .config import Config
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, session
class Database:
def __init__(self, config):
self.engine = create_engine(config.postgresql, echo=True)
def __init__(self, config: Config):
conf_postgresql = config["postgresql"]
postgresql = 'postgresql://{}:{}@{}/{}'.format(
conf_postgresql.get("Username"), conf_postgresql.get("Password"),
conf_postgresql.get("Host"), conf_postgresql.get("DBName"))
self.engine = create_engine(postgresql, echo=False)
Session = sessionmaker()
Session.configure(bind=self.engine)

View file

@ -1,5 +1,5 @@
import gettext
import config
from .config import Config
from cogs.utils.database import Database
from .models.lang import Lang
@ -22,7 +22,7 @@ class Texts:
@staticmethod
def get_locale(ctx):
database = Database(config)
database = Database(Config("./configs/config.cfg"))
if ctx is not None:
current = database.session\

View file

@ -1,3 +1,6 @@
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
from .lang import Lang
from .warn import Warn
from .poll import Poll, Responses
# from .poll import Poll, Responses

View file

@ -1,6 +1,5 @@
from sqlalchemy.ext.declarative import declarative_base
from . import Base
from sqlalchemy import Column, String
Base = declarative_base()
class Lang(Base):

View file

@ -1,10 +1,8 @@
import datetime
from sqlalchemy.ext.declarative import declarative_base
from . import Base
from sqlalchemy import Column, Integer, String, BIGINT, TIMESTAMP
Base = declarative_base()
class Warn(Base):
__tablename__ = 'warns'