From 8ff3609e0756c44387c8b6896192baa128b9b953 Mon Sep 17 00:00:00 2001 From: Valerian Clerc Date: Thu, 26 Mar 2020 01:41:18 +0100 Subject: [PATCH] Change module structure + refactor memes --- Main.py | 20 ++++----- cogs/controllers/__init__.py | 0 cogs/{ => controllers}/currency.py | 2 +- cogs/{ => controllers}/games.py | 6 +-- cogs/{ => controllers}/helpers.py | 10 ++--- cogs/{ => controllers}/images.py | 0 cogs/{ => controllers}/info.py | 2 +- cogs/controllers/memes.py | 59 +++++++++++++++++++++++++++ cogs/{ => controllers}/mod.py | 6 +-- cogs/{ => controllers}/quotes.py | 4 +- cogs/{ => controllers}/reminder.py | 2 +- cogs/{ => controllers}/score.py | 2 +- cogs/{ => controllers}/subscribers.py | 0 cogs/memes.py | 29 ++++--------- 14 files changed, 94 insertions(+), 48 deletions(-) create mode 100644 cogs/controllers/__init__.py rename cogs/{ => controllers}/currency.py (99%) rename cogs/{ => controllers}/games.py (97%) rename cogs/{ => controllers}/helpers.py (98%) rename cogs/{ => controllers}/images.py (100%) rename cogs/{ => controllers}/info.py (98%) create mode 100644 cogs/controllers/memes.py rename cogs/{ => controllers}/mod.py (91%) rename cogs/{ => controllers}/quotes.py (99%) rename cogs/{ => controllers}/reminder.py (99%) rename cogs/{ => controllers}/score.py (99%) rename cogs/{ => controllers}/subscribers.py (100%) diff --git a/Main.py b/Main.py index 997258f4..ddaeabec 100644 --- a/Main.py +++ b/Main.py @@ -33,17 +33,17 @@ # List the extensions (modules) that should be loaded on startup. startup = [ - "cogs.currency", - "cogs.helpers", - "cogs.images", - "cogs.info", "cogs.memes", - "cogs.mod", - "cogs.quotes", - "cogs.reminder", - "cogs.score", - "cogs.subscribers", - "cogs.games", + "cogs.controllers.currency", + "cogs.controllers.helpers", + "cogs.controllers.images", + "cogs.controllers.info", + "cogs.controllers.mod", + "cogs.controllers.quotes", + "cogs.controllers.reminder", + "cogs.controllers.score", + "cogs.controllers.subscribers", + "cogs.controllers.games", ] # TODO: SHOULD BE DB diff --git a/cogs/controllers/__init__.py b/cogs/controllers/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/cogs/currency.py b/cogs/controllers/currency.py similarity index 99% rename from cogs/currency.py rename to cogs/controllers/currency.py index f87766e2..765a6240 100644 --- a/cogs/currency.py +++ b/cogs/controllers/currency.py @@ -30,7 +30,7 @@ # For tables from tabulate import tabulate -from .utils.paginator import Pages +from ..utils.paginator import Pages # For general currency shenanigans from decimal import Decimal, InvalidOperation diff --git a/cogs/games.py b/cogs/controllers/games.py similarity index 97% rename from cogs/games.py rename to cogs/controllers/games.py index 0b12d92f..9173a4eb 100644 --- a/cogs/games.py +++ b/cogs/controllers/games.py @@ -23,8 +23,8 @@ # Other utilities import re -from .utils.dice_roll import dice_roll -from .utils.clamp_default import clamp_default +from ..utils.dice_roll import dice_roll +from ..utils.clamp_default import clamp_default ROLL_PATTERN = re.compile(r'^(\d*)d(\d*)([+-]?\d*)$') @@ -42,7 +42,7 @@ async def roll(self, ctx, arg: str = '', mpr: str = ''): to each roll rather than the sum of all rolls. All parameters are optional. Defaults to rolling one 20-sided die. - + Dice can have 1 to 100 sides Rolls 1 to 10000 dice at once Modifier can be any int between -100 and +100 diff --git a/cogs/helpers.py b/cogs/controllers/helpers.py similarity index 98% rename from cogs/helpers.py rename to cogs/controllers/helpers.py index 842fbae7..21934531 100644 --- a/cogs/helpers.py +++ b/cogs/controllers/helpers.py @@ -38,8 +38,8 @@ import time import datetime import random -from .utils.paginator import Pages -from .utils.requests import fetch +from ..utils.paginator import Pages +from ..utils.requests import fetch MCGILL_EXAM_URL = "https://www.mcgill.ca/exams/dates" @@ -329,8 +329,8 @@ async def keydates(self, ctx): em = discord.Embed(title='McGill Important Dates {0} {1}'.format( term, str(current_year)), - description=MCGILL_KEY_DATES_URL, - colour=0xDA291C) + description=MCGILL_KEY_DATES_URL, + colour=0xDA291C) for i in range(len(headers)): if i == 2: @@ -525,7 +525,7 @@ async def food_spot(self, ctx, *args): message = "**{}**".format(" ".join(args)) channel = utils.get(self.bot.get_guild( self.bot.config.server_id).text_channels, - name=self.bot.config.food_spotting_channel) + name=self.bot.config.food_spotting_channel) username = ctx.message.author pfp = ctx.message.author.avatar_url embed = discord.Embed() diff --git a/cogs/images.py b/cogs/controllers/images.py similarity index 100% rename from cogs/images.py rename to cogs/controllers/images.py diff --git a/cogs/info.py b/cogs/controllers/info.py similarity index 98% rename from cogs/info.py rename to cogs/controllers/info.py index b9beade7..05228f3d 100644 --- a/cogs/info.py +++ b/cogs/controllers/info.py @@ -22,7 +22,7 @@ import subprocess from discord.ext import commands -from .utils.paginator import Pages +from ..utils.paginator import Pages class Info(commands.Cog): diff --git a/cogs/controllers/memes.py b/cogs/controllers/memes.py new file mode 100644 index 00000000..1e5d738b --- /dev/null +++ b/cogs/controllers/memes.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) idoneam (2016-2019) +# +# This file is part of Canary +# +# Canary is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Canary is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Canary. If not, see . + +# Utilities +import random +from ..utils.auto_incorrect import auto_incorrect + + +class MemesController(): + def __init__(self, logger): + self.logger = logger + + def bac(self, author: str, content: str, input_str: str): + self.logger.info(f'input string: {input_str}') + msg = auto_incorrect(input_str) + self.logger.info('?bac invoked: Author: {}, Message: {}'.format( + author, content)) + return msg + + def mix(self, author: str, content: str, input_str: str): + msg = "".join([(c.upper() if random.randint(0, 1) else c.lower()) + for c in input_str]) + self.logger.info('?mix invoked: Author: {}, Message: {}'.format( + author, content)) + return msg + + def pyramid(self, num: int = 2, emoji: str = "👢"): + """Draws a pyramid of boots, default is 2 unless user specifies an integer number of levels of boots between -8 and 8. Also accepts any other emoji, word or multiword (in quotes) string.""" + def pyramidy(n, m): + return "{spaces}{emojis}".format(spaces=" " * ((m - n) * 3), + emojis=(emoji + " ") * n) + + if (num > 0): + num = max(min(num, 8), 1) # Above 8, herre gets angry + msg = "\n".join(pyramidy(ln, num) for ln in range(1, num + 1)) + else: + num = min(max(num, -8), -1) # Below -8, herre gets angry + msg = "\n".join( + pyramidy(ln, abs(num)) + for ln in reversed(range(1, + abs(num) + 1))) + + return msg diff --git a/cogs/mod.py b/cogs/controllers/mod.py similarity index 91% rename from cogs/mod.py rename to cogs/controllers/mod.py index 71fc22e2..cb4c991c 100644 --- a/cogs/mod.py +++ b/cogs/controllers/mod.py @@ -21,7 +21,7 @@ from discord import utils from discord.ext import commands -from .utils.checks import is_moderator +from ..utils.checks import is_moderator class Mod(commands.Cog): @@ -36,7 +36,7 @@ async def answer(self, ctx, *args): ) # to work regardless of whether the person uses apostrophes channel_to_send = utils.get(self.bot.get_guild( self.bot.config.server_id).text_channels, - name=self.bot.config.reception_channel) + name=self.bot.config.reception_channel) msg = '{} 📣 {}'.format(str(ctx.author.name), message) await channel_to_send.send(content=msg) await ctx.send("`Message sent`") @@ -53,7 +53,7 @@ async def pm(self, ctx, user: discord.User, *, message): format(message, self.bot.config.command_prefix[0])) channel_to_forward = utils.get(self.bot.get_guild( self.bot.config.server_id).text_channels, - name=self.bot.config.reception_channel) + name=self.bot.config.reception_channel) msg = '🐦 ({}) to {}: {}'.format(ctx.author.name, dest.name, message) await channel_to_forward.send(msg) await ctx.message.delete() diff --git a/cogs/quotes.py b/cogs/controllers/quotes.py similarity index 99% rename from cogs/quotes.py rename to cogs/controllers/quotes.py index d812f4c5..3fb1227b 100644 --- a/cogs/quotes.py +++ b/cogs/controllers/quotes.py @@ -31,7 +31,7 @@ # Other utils import random -from .utils.paginator import Pages +from ..utils.paginator import Pages GEN_SPACE_SYMBOLS = re.compile(r"[,“”\".?!]") GEN_BLANK_SYMBOLS = re.compile(r"['()`]") @@ -232,7 +232,7 @@ def check(reaction, user): pfp = author.avatar_url if author else DEFAULT_AVATAR embed = discord.Embed(colour=discord.Colour(random.randint( 0, 16777215)), - description=quote) + description=quote) img_urls_found = re.findall(IMAGE_REGEX, quote) diff --git a/cogs/reminder.py b/cogs/controllers/reminder.py similarity index 99% rename from cogs/reminder.py rename to cogs/controllers/reminder.py index d8083628..2259087b 100644 --- a/cogs/reminder.py +++ b/cogs/controllers/reminder.py @@ -27,7 +27,7 @@ import datetime # Other utilities -from .utils.paginator import Pages +from ..utils.paginator import Pages # For remindme functionality import re diff --git a/cogs/score.py b/cogs/controllers/score.py similarity index 99% rename from cogs/score.py rename to cogs/controllers/score.py index e9561904..017ae324 100644 --- a/cogs/score.py +++ b/cogs/controllers/score.py @@ -24,7 +24,7 @@ # for DB import sqlite3 from tabulate import tabulate -from .utils.paginator import Pages +from ..utils.paginator import Pages class Score(commands.Cog): diff --git a/cogs/subscribers.py b/cogs/controllers/subscribers.py similarity index 100% rename from cogs/subscribers.py rename to cogs/controllers/subscribers.py diff --git a/cogs/memes.py b/cogs/memes.py index 6682815e..c3fdcdfb 100644 --- a/cogs/memes.py +++ b/cogs/memes.py @@ -20,6 +20,8 @@ # discord-py requirements from discord.ext import commands +from .controllers.memes import MemesController + # Other utilities import random from .utils.auto_incorrect import auto_incorrect @@ -28,6 +30,7 @@ class Memes(commands.Cog): def __init__(self, bot): self.bot = bot + self.controller = MemesController(bot.logger) @commands.command() async def bac(self, ctx, *, input_str: str = None): @@ -36,9 +39,8 @@ async def bac(self, ctx, *, input_str: str = None): """ if input_str is None: await ctx.send() - msg = auto_incorrect(input_str) - self.bot.logger.info('?bac invoked: Author: {}, Message: {}'.format( - ctx.message.author, ctx.message.content)) + msg = self.controller.bac( + ctx.message.author, ctx.message.content, input_str) await ctx.send(msg) await ctx.message.delete() @@ -141,30 +143,15 @@ async def mix(self, ctx, *, input_str: str = None): disappears after.""" if input_str is None: await ctx.send() - msg = "".join([(c.upper() if random.randint(0, 1) else c.lower()) - for c in input_str]) - self.bot.logger.info('?mix invoked: Author: {}, Message: {}'.format( - ctx.message.author, ctx.message.content)) + msg = self.controller.mix( + ctx.message.author, ctx.message.content, input_str) await ctx.send(msg) await ctx.message.delete() @commands.command(aliases=['boot']) async def pyramid(self, ctx, num: int = 2, emoji: str = "👢"): """Draws a pyramid of boots, default is 2 unless user specifies an integer number of levels of boots between -8 and 8. Also accepts any other emoji, word or multiword (in quotes) string.""" - def pyramidy(n, m): - return "{spaces}{emojis}".format(spaces=" " * ((m - n) * 3), - emojis=(emoji + " ") * n) - - if (num > 0): - num = max(min(num, 8), 1) # Above 8, herre gets angry - msg = "\n".join(pyramidy(ln, num) for ln in range(1, num + 1)) - else: - num = min(max(num, -8), -1) # Below -8, herre gets angry - msg = "\n".join( - pyramidy(ln, abs(num)) - for ln in reversed(range(1, - abs(num) + 1))) - + msg = self.controller.pyramid(num, emoji) await ctx.send("**\n{}**".format(msg))