Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Refactor to enable unit testing #285

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file added cogs/controllers/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion cogs/currency.py → cogs/controllers/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cogs/games.py → cogs/controllers/games.py
Original file line number Diff line number Diff line change
Expand Up @@ -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*)$')

Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions cogs/helpers.py → cogs/controllers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion cogs/info.py → cogs/controllers/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
59 changes: 59 additions & 0 deletions cogs/controllers/memes.py
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.

# 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
6 changes: 3 additions & 3 deletions cogs/mod.py → cogs/controllers/mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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`")
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cogs/quotes.py → cogs/controllers/quotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"['()`]")
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion cogs/reminder.py → cogs/controllers/reminder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import datetime

# Other utilities
from .utils.paginator import Pages
from ..utils.paginator import Pages

# For remindme functionality
import re
Expand Down
2 changes: 1 addition & 1 deletion cogs/score.py → cogs/controllers/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
File renamed without changes.
29 changes: 8 additions & 21 deletions cogs/memes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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()

Expand Down Expand Up @@ -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))


Expand Down