From 0204c1a270b17807b70cdd7a45e467cf6dcab381 Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 9 May 2022 18:21:03 +0100 Subject: [PATCH 01/30] Some base work for the API conversion --- koala/cogs/announce/cog.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/koala/cogs/announce/cog.py b/koala/cogs/announce/cog.py index e8c1f9ad..8de9cc1d 100644 --- a/koala/cogs/announce/cog.py +++ b/koala/cogs/announce/cog.py @@ -30,7 +30,7 @@ def announce_is_enabled(ctx): except PermissionError: result = False - return result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest) + await ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) class Announce(commands.Cog): @@ -55,15 +55,15 @@ def not_exceeded_limit(self, guild_id): guild_id) > ANNOUNCE_SEPARATION_DAYS * SECONDS_IN_A_DAY return True - def has_active_msg(self, guild_id): + def has_active_msg(self, guild_id, ctx): """ Check if a particular id has an active announcement pending announcement :param guild_id: The id of the guild of the command :return: Boolean of whether there is an active announcement or not """ - return guild_id in self.messages.keys() + await ctx.send(guild_id in self.messages) - def get_role_names(self, guild_id, roles): + def get_role_names(self, guild_id, roles, ctx): """ A function to get the names of all the roles the announcement will be sent to :param roles: The list of roles in the guild @@ -73,9 +73,9 @@ def get_role_names(self, guild_id, roles): temp = [] for role in self.roles[guild_id]: temp.append(discord.utils.get(roles, id=role).name) - return temp + await ctx.send(temp) - def get_receivers(self, guild_id, roles): + def get_receivers(self, guild_id, roles, ctx): """ A function to get the receivers of a particular announcement :param roles: The list of roles in the guild @@ -85,7 +85,7 @@ def get_receivers(self, guild_id, roles): temp = [] for role in self.roles[guild_id]: temp += discord.utils.get(roles, id=role).members - return list(set(temp)) + await ctx.send(list(set(temp))) def receiver_msg(self, guild): """ From 38c3294cee9e0d91a21f42b45fa6687b2eb1a7a8 Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 9 May 2022 18:24:12 +0100 Subject: [PATCH 02/30] More ctx stuff --- koala/cogs/announce/cog.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/koala/cogs/announce/cog.py b/koala/cogs/announce/cog.py index 8de9cc1d..03562546 100644 --- a/koala/cogs/announce/cog.py +++ b/koala/cogs/announce/cog.py @@ -45,7 +45,7 @@ def __init__(self, bot): insert_extension("Announce", 0, True, True) self.announce_database_manager = AnnounceDBManager() - def not_exceeded_limit(self, guild_id): + def not_exceeded_limit(self, guild_id, ctx): """ Check if enough days have passed for the user to use the announce function :return: @@ -53,7 +53,7 @@ def not_exceeded_limit(self, guild_id): if self.announce_database_manager.get_last_use_date(guild_id): return int(time.time()) - self.announce_database_manager.get_last_use_date( guild_id) > ANNOUNCE_SEPARATION_DAYS * SECONDS_IN_A_DAY - return True + await ctx.send(True) def has_active_msg(self, guild_id, ctx): """ @@ -87,7 +87,7 @@ def get_receivers(self, guild_id, roles, ctx): temp += discord.utils.get(roles, id=role).members await ctx.send(list(set(temp))) - def receiver_msg(self, guild): + def receiver_msg(self, guild, ctx): """ A function to create a string message about receivers :param guild: The guild of the bot @@ -95,9 +95,9 @@ def receiver_msg(self, guild): """ if not self.roles[guild.id]: return f"You are currently sending to Everyone and there are {str(len(guild.members))} receivers" - return f"You are currently sending to {self.get_role_names(guild.id, guild.roles)} and there are {str(len(self.get_receivers(guild.id, guild.roles)))} receivers " + await ctx.send(f"You are currently sending to {self.get_role_names(guild.id, guild.roles)} and there are {str(len(self.get_receivers(guild.id, guild.roles)))} receivers ") - def construct_embed(self, guild: discord.Guild): + def construct_embed(self, guild: discord.Guild, ctx): """ Constructing an embedded message from the information stored in the manager :param guild: The the guild @@ -109,7 +109,7 @@ def construct_embed(self, guild: discord.Guild): embed.set_author(name="Announcement from " + guild.name) if message.thumbnail != 'https://cdn.discordapp.com/': embed.set_thumbnail(url=message.thumbnail) - return embed + await ctx.send(embed) @commands.check(announce_is_enabled) @commands.group(name="announce") From 05a53195886cdc6bae9851a2b44c1ca9b6bf3d2e Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 23 May 2022 13:56:02 +0100 Subject: [PATCH 03/30] Remove awaits as not async functions --- koala/cogs/announce/cog.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/koala/cogs/announce/cog.py b/koala/cogs/announce/cog.py index 03562546..f418b13c 100644 --- a/koala/cogs/announce/cog.py +++ b/koala/cogs/announce/cog.py @@ -30,7 +30,7 @@ def announce_is_enabled(ctx): except PermissionError: result = False - await ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) + ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) class Announce(commands.Cog): @@ -53,7 +53,7 @@ def not_exceeded_limit(self, guild_id, ctx): if self.announce_database_manager.get_last_use_date(guild_id): return int(time.time()) - self.announce_database_manager.get_last_use_date( guild_id) > ANNOUNCE_SEPARATION_DAYS * SECONDS_IN_A_DAY - await ctx.send(True) + ctx.send(True) def has_active_msg(self, guild_id, ctx): """ @@ -61,7 +61,7 @@ def has_active_msg(self, guild_id, ctx): :param guild_id: The id of the guild of the command :return: Boolean of whether there is an active announcement or not """ - await ctx.send(guild_id in self.messages) + ctx.send(guild_id in self.messages) def get_role_names(self, guild_id, roles, ctx): """ @@ -73,7 +73,7 @@ def get_role_names(self, guild_id, roles, ctx): temp = [] for role in self.roles[guild_id]: temp.append(discord.utils.get(roles, id=role).name) - await ctx.send(temp) + ctx.send(temp) def get_receivers(self, guild_id, roles, ctx): """ @@ -85,7 +85,7 @@ def get_receivers(self, guild_id, roles, ctx): temp = [] for role in self.roles[guild_id]: temp += discord.utils.get(roles, id=role).members - await ctx.send(list(set(temp))) + ctx.send(list(set(temp))) def receiver_msg(self, guild, ctx): """ @@ -95,7 +95,7 @@ def receiver_msg(self, guild, ctx): """ if not self.roles[guild.id]: return f"You are currently sending to Everyone and there are {str(len(guild.members))} receivers" - await ctx.send(f"You are currently sending to {self.get_role_names(guild.id, guild.roles)} and there are {str(len(self.get_receivers(guild.id, guild.roles)))} receivers ") + ctx.send(f"You are currently sending to {self.get_role_names(guild.id, guild.roles)} and there are {str(len(self.get_receivers(guild.id, guild.roles)))} receivers ") def construct_embed(self, guild: discord.Guild, ctx): """ @@ -109,7 +109,7 @@ def construct_embed(self, guild: discord.Guild, ctx): embed.set_author(name="Announcement from " + guild.name) if message.thumbnail != 'https://cdn.discordapp.com/': embed.set_thumbnail(url=message.thumbnail) - await ctx.send(embed) + ctx.send(embed) @commands.check(announce_is_enabled) @commands.group(name="announce") From 0e95a28994c8e0d19a15b6c37ed79e4d61dfb9c8 Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 23 May 2022 14:03:53 +0100 Subject: [PATCH 04/30] Test file --- tests/cogs/announce/test_api.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/cogs/announce/test_api.py diff --git a/tests/cogs/announce/test_api.py b/tests/cogs/announce/test_api.py new file mode 100644 index 00000000..7a278e27 --- /dev/null +++ b/tests/cogs/announce/test_api.py @@ -0,0 +1,22 @@ +from http.client import BAD_REQUEST, CREATED, OK, UNPROCESSABLE_ENTITY + +from mock import mock +from koala.db import get_all_available_guild_extensions +from koala.rest.api import parse_request + +import koalabot +from koala.cogs.base.api import BaseEndpoint + +# Libs +import discord +from aiohttp import web +import pytest +import discord.ext.test as dpytest + + +@pytest.fixture +def api_client(bot: discord.ext.commands.Bot, aiohttp_client, loop ): + app = web.Application() + endpoint = BaseEndpoint(bot) + app = endpoint.register(app) + return loop.run_until_complete(aiohttp_client(app)) \ No newline at end of file From b60fd8d3495464f801adb0db64d6fd05315b70ea Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 23 May 2022 14:37:44 +0100 Subject: [PATCH 05/30] Move cog functionality to announce_message --- koala/cogs/announce/announce_message.py | 131 ++++++++++++++++++++ koala/cogs/announce/cog.py | 151 ++++++++++-------------- 2 files changed, 196 insertions(+), 86 deletions(-) diff --git a/koala/cogs/announce/announce_message.py b/koala/cogs/announce/announce_message.py index 3b15dd25..6a0e5033 100644 --- a/koala/cogs/announce/announce_message.py +++ b/koala/cogs/announce/announce_message.py @@ -1,3 +1,36 @@ +# Built-in/Generic Imports +import math +import time + +# Own modules +import koalabot +from koala.colours import KOALA_GREEN +from koala.db import insert_extension +from koala.utils import extract_id, wait_for_message +from .announce_message import AnnounceMessage +from .db import AnnounceDBManager +from .log import logger +from .utils import ANNOUNCE_SEPARATION_DAYS, SECONDS_IN_A_DAY, MAX_MESSAGE_LENGTH + +# Libs +import discord +from discord.ext import commands + +def announce_is_enabled(guild): + """ + A command used to check if the guild has enabled announce + e.g. @commands.check(announce_is_enabled) + + :param guild: The context of the message + :return: True if enabled or test, False otherwise + """ + try: + result = koalabot.check_guild_has_ext(guild, "Announce") + except PermissionError: + result = False + + return result + class AnnounceMessage: """ A class consisting the information about a announcement message @@ -29,3 +62,101 @@ def set_description(self, message): :return: """ self.description = message + +class Announce: + """ + Send DM announcements to certain roles and people. + """ + + def __init__(self, bot): + self.bot = bot + self.messages = {} + self.roles = {} + insert_extension("Announce", 0, True, True) + self.announce_database_manager = AnnounceDBManager() + + def not_exceeded_limit(self, guild_id): + """ + Check if enough days have passed for the user to use the announce function + :return: + """ + if self.announce_database_manager.get_last_use_date(guild_id): + last_use = self.announce_database_manager.get_last_use_date(guild_id) + if (time.time() - last_use) > ANNOUNCE_SEPARATION_DAYS * SECONDS_IN_A_DAY: + return True + else: + return False + + def has_active_message(self, guild_id): + """ + Check if a particular id has an active announcement pending announcement + :param guild_id: The id of the guild of the command + :return: Boolean of whether there is an active announcement or not + """ + if guild_id in self.messages: + return True + else: + return False + + def get_role_names(self, guild_id): + """ + Get the names of the roles that are currently being announced + :param guild_id: The id of the guild of the command + :return: A list of strings consisting the names of the roles + """ + if guild_id in self.roles: + return self.roles[guild_id] + else: + return [] + + def get_receivers(self, guild_id): + """ + A function to get the receivers of a particular announcement + :param roles: The list of roles in the guild + :param guild_id: The id of the guild + :return: All the receivers of the announcement + """ + receivers = [] + if guild_id in self.roles: + roles = self.roles[guild_id] + for role in roles: + for member in role.members: + receivers.append(member) + return receivers + + def receiver_msg(self, guild): + """ + A function to create a string message about receivers + :param guild: The guild of the bot + :return: A string message about receivers + """ + if not self.roles[guild.id]: + return f"You are currently sending to Everyone and there are {str(len(guild.members))} receivers" + else: + receivers = self.get_receivers(guild.id) + return f"You are currently sending to {str(len(self.roles[guild.id]))} roles and {str(len(receivers))} receivers" + + def construct_embed(self, guild: discord.Guild): + """ + Constructing an embedded message from the information stored in the manager + :param guild: The the guild + :return: An embedded message for the announcement + """ + message = self.messages[guild.id] + embed: discord.Embed = discord.Embed(title=message.title, + description=message.description, colour=KOALA_GREEN) + embed.set_author(name="Announcement from " + guild.name) + if message.thumbnail != 'https://cdn.discordapp.com/': + embed.set_thumbnail(url=message.thumbnail) + return embed + + @commands.check(announce_is_enabled) + @commands.group(name="announce") + async def announce(self, ctx): + """ + The main command for the announce cog + :param ctx: The context of the command + :return: + """ + if ctx.invoked_subcommand is None: + await ctx.send(f"{ctx.author.mention}, please use `{ctx.prefix}announce help` to see the help for this command") \ No newline at end of file diff --git a/koala/cogs/announce/cog.py b/koala/cogs/announce/cog.py index f418b13c..80380d1f 100644 --- a/koala/cogs/announce/cog.py +++ b/koala/cogs/announce/cog.py @@ -1,116 +1,95 @@ -# Built-in/Generic Imports -import math -import time - -# Libs -import discord -from discord.ext import commands - -# Own modules +# Own imports import koalabot -from koala.colours import KOALA_GREEN -from koala.db import insert_extension -from koala.utils import extract_id, wait_for_message -from .announce_message import AnnounceMessage -from .db import AnnounceDBManager -from .log import logger -from .utils import ANNOUNCE_SEPARATION_DAYS, SECONDS_IN_A_DAY, MAX_MESSAGE_LENGTH +from announce_message import AnnounceMessage, Announce, announce_is_enabled +def announce_is_enabled(guild): + return announce_is_enabled(guild) -def announce_is_enabled(ctx): +def not_exceeded_limit(self, guild_id, ctx): """ - A command used to check if the guild has enabled announce - e.g. @commands.check(announce_is_enabled) - + Check if the number of announcements in the guild is not exceeded + :param guild_id: The id of the guild :param ctx: The context of the message - :return: True if enabled or test, False otherwise + :return: True if the number of announcements is not exceeded, False otherwise """ try: - result = koalabot.check_guild_has_ext(ctx, "Announce") + result = self.Announce.not_exceeded_limit(guild_id, ctx) except PermissionError: result = False ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) - -class Announce(commands.Cog): +def has_active_msg(self, guild_id, ctx): """ - Send DM announcements to certain roles and people. + Check if there is an active announcement message + :param guild_id: The id of the guild + :param ctx: The context of the message + :return: True if there is an active announcement message, False otherwise """ + try: + result = self.Announce.has_active_msg(guild_id, ctx) + except PermissionError: + result = False - def __init__(self, bot): - self.bot = bot - self.messages = {} - self.roles = {} - insert_extension("Announce", 0, True, True) - self.announce_database_manager = AnnounceDBManager() + ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) - def not_exceeded_limit(self, guild_id, ctx): - """ - Check if enough days have passed for the user to use the announce function - :return: - """ - if self.announce_database_manager.get_last_use_date(guild_id): - return int(time.time()) - self.announce_database_manager.get_last_use_date( - guild_id) > ANNOUNCE_SEPARATION_DAYS * SECONDS_IN_A_DAY - ctx.send(True) +def get_role_names(self, guild_id, roles, ctx): + """ + Get the names of the roles + :param guild_id: The id of the guild + :param roles: The roles + :param ctx: The context of the message + :return: A string consisting the names of the roles + """ + try: + result = self.Announce.get_role_names(guild_id, roles, ctx) + except PermissionError: + result = False - def has_active_msg(self, guild_id, ctx): - """ - Check if a particular id has an active announcement pending announcement - :param guild_id: The id of the guild of the command - :return: Boolean of whether there is an active announcement or not - """ - ctx.send(guild_id in self.messages) + ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) - def get_role_names(self, guild_id, roles, ctx): - """ - A function to get the names of all the roles the announcement will be sent to - :param roles: The list of roles in the guild - :param guild_id: The id of the guild - :return: All the names of the roles that are tagged - """ - temp = [] - for role in self.roles[guild_id]: - temp.append(discord.utils.get(roles, id=role).name) - ctx.send(temp) +def get_receivers(self, guild_id, roles, ctx): + """ + Get the receivers of the announcement + :param guild_id: The id of the guild + :param roles: The roles + :param ctx: The context of the message + :return: A list of the receivers + """ + try: + result = self.Announce.get_receivers(guild_id, roles, ctx) + except PermissionError: + result = False - def get_receivers(self, guild_id, roles, ctx): - """ - A function to get the receivers of a particular announcement - :param roles: The list of roles in the guild - :param guild_id: The id of the guild - :return: All the receivers of the announcement - """ - temp = [] - for role in self.roles[guild_id]: - temp += discord.utils.get(roles, id=role).members - ctx.send(list(set(temp))) + ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) - def receiver_msg(self, guild, ctx): +def receiver_msg(self, guild, ctx): """ A function to create a string message about receivers :param guild: The guild of the bot :return: A string message about receivers """ - if not self.roles[guild.id]: - return f"You are currently sending to Everyone and there are {str(len(guild.members))} receivers" - ctx.send(f"You are currently sending to {self.get_role_names(guild.id, guild.roles)} and there are {str(len(self.get_receivers(guild.id, guild.roles)))} receivers ") + try: + result = self.Announce.receiver_msg(guild, ctx) + except PermissionError: + result = False - def construct_embed(self, guild: discord.Guild, ctx): - """ - Constructing an embedded message from the information stored in the manager - :param guild: The the guild - :return: An embedded message for the announcement - """ - message = self.messages[guild.id] - embed: discord.Embed = discord.Embed(title=message.title, - description=message.description, colour=KOALA_GREEN) - embed.set_author(name="Announcement from " + guild.name) - if message.thumbnail != 'https://cdn.discordapp.com/': - embed.set_thumbnail(url=message.thumbnail) - ctx.send(embed) + ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) + +def construct_embed(self, guild: discord.Guild, ctx): + """ + Construct an embed message + :param guild: The guild of the bot + :param ctx: The context of the message + :return: An embed message + """ + try: + result = self.Announce.construct_embed(guild, ctx) + except PermissionError: + result = False + ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) + @commands.check(announce_is_enabled) @commands.group(name="announce") async def announce(self, ctx): From 2932f03752fe7a4915cf1bf8bb2ef7e96b3af04b Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Sun, 26 Jun 2022 21:37:19 +0300 Subject: [PATCH 06/30] Base api.py for announce cog --- koala/cogs/announce/__init__.py | 10 +++++-- koala/cogs/announce/api.py | 50 +++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 koala/cogs/announce/api.py diff --git a/koala/cogs/announce/__init__.py b/koala/cogs/announce/__init__.py index bcc0d1f7..44441578 100644 --- a/koala/cogs/announce/__init__.py +++ b/koala/cogs/announce/__init__.py @@ -1,3 +1,7 @@ -from . import utils, db, log, models -from .cog import Announce, setup -from .announce_message import AnnounceMessage \ No newline at end of file +from . import cog +from . import api +from .cog import AnnounceCog + +def setup(bot): + cog.setup(bot) + api.setup(bot) diff --git a/koala/cogs/announce/api.py b/koala/cogs/announce/api.py new file mode 100644 index 00000000..cbd4f925 --- /dev/null +++ b/koala/cogs/announce/api.py @@ -0,0 +1,50 @@ +# Futures +# Built-in/Generic Imports +# Libs +from http.client import CREATED, OK, BAD_REQUEST +from aiohttp import web +import discord +from discord.ext.commands import Bot + +# Own modules +from . import core +from .log import logger +from koala.rest.api import parse_request, build_response +from koala.utils import convert_iso_datetime + +# Constants +ANNOUNCE_ENDPOINT = 'announce' +ACTIVITY_ENDPOINT = 'scheduled-activity' # GET +SET_ACTIVITY_ENDPOINT = 'activity' # PUT +SCHEDULE_ACTIVITY_ENDPOINT = 'scheduled-activity' # POST + +class AnnounceEndpoint: + """ + The API endpoints for AnnounceCog + """ + def __init__(self, bot): + self._bot = bot + + def register(self, app): + """ + Register the routes for the given application + todo: review aiohttp 'views' and see if they are a better idea + :param app: The aiohttp.web.Application (likely of the sub app) + :return: app + """ + app.add_routes([web.get('/{endpoint}'.format(endpoint=ACTIVITY_ENDPOINT), self.get_activities), + web.put('/{endpoint}'.format(endpoint=SET_ACTIVITY_ENDPOINT), self.put_set_activity), + web.post('/{endpoint}'.format(endpoint=SCHEDULE_ACTIVITY_ENDPOINT), self.post_schedule_activity), + web.get('/{endpoint}'.format(endpoint=ANNOUNCE_ENDPOINT), self.get_announce)]) + return app + +def setup(bot: Bot): + """ + Load this cog to the KoalaBot. + :param bot: the bot client for KoalaBot + """ + sub_app = web.Application() + endpoint = BaseEndpoint(bot) + endpoint.register(sub_app) + getattr(bot, "koala_web_app").add_subapp('/{extension}'.format(extension=BASE_ENDPOINT), sub_app) + logger.info("Announce API is ready.") \ No newline at end of file From 684b4a625ac07d58fd7b669456d5f6c0d9edee1a Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Sun, 3 Jul 2022 21:40:58 +0300 Subject: [PATCH 07/30] Fix indenting problems and add a test file --- koala/cogs/announce/api.py | 9 ++++++--- koala/cogs/announce/test_annoucement.py | 12 ++++++++++++ tests/cogs/announce/test_api.py | 5 +++-- 3 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 koala/cogs/announce/test_annoucement.py diff --git a/koala/cogs/announce/api.py b/koala/cogs/announce/api.py index cbd4f925..670a6ea5 100644 --- a/koala/cogs/announce/api.py +++ b/koala/cogs/announce/api.py @@ -26,7 +26,7 @@ def __init__(self, bot): self._bot = bot def register(self, app): - """ + """ Register the routes for the given application todo: review aiohttp 'views' and see if they are a better idea :param app: The aiohttp.web.Application (likely of the sub app) @@ -38,13 +38,16 @@ def register(self, app): web.get('/{endpoint}'.format(endpoint=ANNOUNCE_ENDPOINT), self.get_announce)]) return app +def getActivityType(activity_type): + return discord.ActivityType[activity_type] + def setup(bot: Bot): """ Load this cog to the KoalaBot. :param bot: the bot client for KoalaBot """ sub_app = web.Application() - endpoint = BaseEndpoint(bot) + endpoint = AnnounceEndpoint(bot) endpoint.register(sub_app) - getattr(bot, "koala_web_app").add_subapp('/{extension}'.format(extension=BASE_ENDPOINT), sub_app) + getattr(bot, "koala_web_app").add_subapp('/{extension}'.format(extension=ANNOUNCE_ENDPOINT), sub_app) logger.info("Announce API is ready.") \ No newline at end of file diff --git a/koala/cogs/announce/test_annoucement.py b/koala/cogs/announce/test_annoucement.py new file mode 100644 index 00000000..0ee7f382 --- /dev/null +++ b/koala/cogs/announce/test_annoucement.py @@ -0,0 +1,12 @@ +import datetime + +import discord +import mock +import pytest +from discord.ext import commands +import discord.ext.test as dpytest +from sqlalchemy import null + +import koalabot +from koala.cogs.announce import AnnounceCog + diff --git a/tests/cogs/announce/test_api.py b/tests/cogs/announce/test_api.py index 7a278e27..a4289e00 100644 --- a/tests/cogs/announce/test_api.py +++ b/tests/cogs/announce/test_api.py @@ -1,11 +1,12 @@ from http.client import BAD_REQUEST, CREATED, OK, UNPROCESSABLE_ENTITY from mock import mock +from koala.cogs.announce.api import AnnounceEndpoint from koala.db import get_all_available_guild_extensions from koala.rest.api import parse_request import koalabot -from koala.cogs.base.api import BaseEndpoint +from koala.cogs.base.api import AnnounceEndpoint # Libs import discord @@ -17,6 +18,6 @@ @pytest.fixture def api_client(bot: discord.ext.commands.Bot, aiohttp_client, loop ): app = web.Application() - endpoint = BaseEndpoint(bot) + endpoint = AnnounceEndpoint(bot) app = endpoint.register(app) return loop.run_until_complete(aiohttp_client(app)) \ No newline at end of file From bb228dc1e2989364a52215576cf156902f83ef3e Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Sun, 3 Jul 2022 21:44:54 +0300 Subject: [PATCH 08/30] Import commands --- koala/cogs/announce/cog.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/koala/cogs/announce/cog.py b/koala/cogs/announce/cog.py index 80380d1f..dfb867e9 100644 --- a/koala/cogs/announce/cog.py +++ b/koala/cogs/announce/cog.py @@ -2,6 +2,9 @@ import koalabot from announce_message import AnnounceMessage, Announce, announce_is_enabled +from discord.ext import commands, tasks +from discord.ext.commands import BadArgument + def announce_is_enabled(guild): return announce_is_enabled(guild) From b78ba2eb7c096583a7dae8b72c9a42bc56a069ed Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Sun, 10 Jul 2022 15:21:02 +0300 Subject: [PATCH 09/30] Add missing imports --- koala/cogs/announce/cog.py | 40 +++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/koala/cogs/announce/cog.py b/koala/cogs/announce/cog.py index dfb867e9..1324eb4e 100644 --- a/koala/cogs/announce/cog.py +++ b/koala/cogs/announce/cog.py @@ -1,13 +1,24 @@ # Own imports +import math +import time + +import discord + import koalabot from announce_message import AnnounceMessage, Announce, announce_is_enabled from discord.ext import commands, tasks from discord.ext.commands import BadArgument +from koala.cogs.announce.utils import MAX_MESSAGE_LENGTH, ANNOUNCE_SEPARATION_DAYS, SECONDS_IN_A_DAY +from koala.log import logger +from koala.utils import wait_for_message, extract_id + + def announce_is_enabled(guild): return announce_is_enabled(guild) + def not_exceeded_limit(self, guild_id, ctx): """ Check if the number of announcements in the guild is not exceeded @@ -22,6 +33,7 @@ def not_exceeded_limit(self, guild_id, ctx): ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) + def has_active_msg(self, guild_id, ctx): """ Check if there is an active announcement message @@ -36,6 +48,7 @@ def has_active_msg(self, guild_id, ctx): ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) + def get_role_names(self, guild_id, roles, ctx): """ Get the names of the roles @@ -51,6 +64,7 @@ def get_role_names(self, guild_id, roles, ctx): ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) + def get_receivers(self, guild_id, roles, ctx): """ Get the receivers of the announcement @@ -66,18 +80,20 @@ def get_receivers(self, guild_id, roles, ctx): ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) + def receiver_msg(self, guild, ctx): - """ + """ A function to create a string message about receivers :param guild: The guild of the bot :return: A string message about receivers """ - try: - result = self.Announce.receiver_msg(guild, ctx) - except PermissionError: - result = False + try: + result = self.Announce.receiver_msg(guild, ctx) + except PermissionError: + result = False + + ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) - ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) def construct_embed(self, guild: discord.Guild, ctx): """ @@ -92,7 +108,7 @@ def construct_embed(self, guild: discord.Guild, ctx): result = False ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) - + @commands.check(announce_is_enabled) @commands.group(name="announce") async def announce(self, ctx): @@ -187,7 +203,8 @@ async def add_role(self, ctx): :return: """ if self.has_active_msg(ctx.guild.id): - await ctx.send("Please enter the roles you want to tag separated by space, I'll wait for 60 seconds, no rush.") + await ctx.send( + "Please enter the roles you want to tag separated by space, I'll wait for 60 seconds, no rush.") message, channel = await wait_for_message(self.bot, ctx) if not message: await channel.send("Okay, I'll cancel the command.") @@ -210,7 +227,8 @@ async def remove_role(self, ctx): :return: """ if self.has_active_msg(ctx.guild.id): - await ctx.send("Please enter the roles you want to remove separated by space, I'll wait for 60 seconds, no rush.") + await ctx.send( + "Please enter the roles you want to remove separated by space, I'll wait for 60 seconds, no rush.") message, channel = await wait_for_message(self.bot, ctx) if not message: await channel.send("Okay, I'll cancel the command.") @@ -252,13 +270,13 @@ async def send(self, ctx): try: await receiver.send(embed=embed) except (discord.Forbidden, AttributeError, discord.HTTPException) as e: - logger.error(f'User {receiver.id} cannot recieve dms') + logger.error(f'User {receiver.id} cannot receive dms') else: for receiver in ctx.guild.members: try: await receiver.send(embed=embed) except (discord.Forbidden, AttributeError, discord.HTTPException) as e: - logger.error(f'User {receiver.id} cannot recieve dms') + logger.error(f'User {receiver.id} cannot receive dms') self.messages.pop(ctx.guild.id) self.roles.pop(ctx.guild.id) From ebb21858fe70479a2999fba03d6d143b949dfd11 Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Sun, 10 Jul 2022 15:36:52 +0300 Subject: [PATCH 10/30] Add some API paths --- koala/cogs/announce/announce_message.py | 10 ++++-- koala/cogs/announce/api.py | 47 ++++++++++++++++++------- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/koala/cogs/announce/announce_message.py b/koala/cogs/announce/announce_message.py index 6a0e5033..364303ec 100644 --- a/koala/cogs/announce/announce_message.py +++ b/koala/cogs/announce/announce_message.py @@ -16,6 +16,7 @@ import discord from discord.ext import commands + def announce_is_enabled(guild): """ A command used to check if the guild has enabled announce @@ -31,6 +32,7 @@ def announce_is_enabled(guild): return result + class AnnounceMessage: """ A class consisting the information about a announcement message @@ -63,6 +65,7 @@ def set_description(self, message): """ self.description = message + class Announce: """ Send DM announcements to certain roles and people. @@ -86,7 +89,7 @@ def not_exceeded_limit(self, guild_id): return True else: return False - + def has_active_message(self, guild_id): """ Check if a particular id has an active announcement pending announcement @@ -131,7 +134,7 @@ def receiver_msg(self, guild): :return: A string message about receivers """ if not self.roles[guild.id]: - return f"You are currently sending to Everyone and there are {str(len(guild.members))} receivers" + return f"You are currently sending to Everyone and there are {str(len(guild.members))} receivers" else: receivers = self.get_receivers(guild.id) return f"You are currently sending to {str(len(self.roles[guild.id]))} roles and {str(len(receivers))} receivers" @@ -159,4 +162,5 @@ async def announce(self, ctx): :return: """ if ctx.invoked_subcommand is None: - await ctx.send(f"{ctx.author.mention}, please use `{ctx.prefix}announce help` to see the help for this command") \ No newline at end of file + await ctx.send( + f"{ctx.author.mention}, please use `{ctx.prefix}announce help` to see the help for this command") diff --git a/koala/cogs/announce/api.py b/koala/cogs/announce/api.py index 670a6ea5..399af050 100644 --- a/koala/cogs/announce/api.py +++ b/koala/cogs/announce/api.py @@ -1,27 +1,26 @@ # Futures # Built-in/Generic Imports # Libs -from http.client import CREATED, OK, BAD_REQUEST from aiohttp import web -import discord from discord.ext.commands import Bot +from koala.rest.api import parse_request +from . import cog # Own modules -from . import core from .log import logger -from koala.rest.api import parse_request, build_response -from koala.utils import convert_iso_datetime # Constants ANNOUNCE_ENDPOINT = 'announce' -ACTIVITY_ENDPOINT = 'scheduled-activity' # GET -SET_ACTIVITY_ENDPOINT = 'activity' # PUT -SCHEDULE_ACTIVITY_ENDPOINT = 'scheduled-activity' # POST +ACTIVITY_ENDPOINT = 'scheduled-activity' # GET +SET_ACTIVITY_ENDPOINT = 'activity' # PUT +SCHEDULE_ACTIVITY_ENDPOINT = 'scheduled-activity' # POST + class AnnounceEndpoint: """ The API endpoints for AnnounceCog """ + def __init__(self, bot): self._bot = bot @@ -34,12 +33,36 @@ def register(self, app): """ app.add_routes([web.get('/{endpoint}'.format(endpoint=ACTIVITY_ENDPOINT), self.get_activities), web.put('/{endpoint}'.format(endpoint=SET_ACTIVITY_ENDPOINT), self.put_set_activity), - web.post('/{endpoint}'.format(endpoint=SCHEDULE_ACTIVITY_ENDPOINT), self.post_schedule_activity), + web.post('/{endpoint}'.format(endpoint=SCHEDULE_ACTIVITY_ENDPOINT), + self.post_schedule_activity), web.get('/{endpoint}'.format(endpoint=ANNOUNCE_ENDPOINT), self.get_announce)]) return app -def getActivityType(activity_type): - return discord.ActivityType[activity_type] + +@parse_request +async def announce_is_enabled(guild): + return await cog.announce_is_enabled(guild) + + +@parse_request +async def enough_days_passed(self, guild_id, ctx): + return await cog.not_exceeded_limit(self, guild_id, ctx) + + +@parse_request +async def has_active_message(self, guild_id, ctx): + return await cog.has_active_msg(self, guild_id, ctx) + + +@parse_request +async def get_names_of_roles(self, guild_id, roles, ctx): + return await cog.get_role_names(self, guild_id, roles, ctx) + + +@parse_request() +async def get_receivers_of_announcement(self, guild_id, roles, ctx): + return await cog.get_receivers(self, guild_id, roles, ctx) + def setup(bot: Bot): """ @@ -50,4 +73,4 @@ def setup(bot: Bot): endpoint = AnnounceEndpoint(bot) endpoint.register(sub_app) getattr(bot, "koala_web_app").add_subapp('/{extension}'.format(extension=ANNOUNCE_ENDPOINT), sub_app) - logger.info("Announce API is ready.") \ No newline at end of file + logger.info("Announce API is ready.") From 4639702dcd5327dc91cf0146c6871de405d39bce Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 11 Jul 2022 20:07:19 +0300 Subject: [PATCH 11/30] Add more API routes --- koala/cogs/announce/api.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/koala/cogs/announce/api.py b/koala/cogs/announce/api.py index 399af050..e16d8931 100644 --- a/koala/cogs/announce/api.py +++ b/koala/cogs/announce/api.py @@ -63,6 +63,14 @@ async def get_names_of_roles(self, guild_id, roles, ctx): async def get_receivers_of_announcement(self, guild_id, roles, ctx): return await cog.get_receivers(self, guild_id, roles, ctx) +@parse_request() +async def receiver_msg_create(self, guild, ctx): + return await cog.receiver_msg_create(self, guild, ctx) + +@parse_request() +async def construct_embed_message(self, guild, ctx): + return await cog.construct_embed_message(self, guild, ctx) + def setup(bot: Bot): """ From ac140664f05a5fedc3e357e99f22b84bada28cf1 Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Sun, 17 Jul 2022 13:02:06 +0300 Subject: [PATCH 12/30] Remove duplicate endpoint --- koala/cogs/announce/api.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/koala/cogs/announce/api.py b/koala/cogs/announce/api.py index e16d8931..90c445ed 100644 --- a/koala/cogs/announce/api.py +++ b/koala/cogs/announce/api.py @@ -13,7 +13,6 @@ ANNOUNCE_ENDPOINT = 'announce' ACTIVITY_ENDPOINT = 'scheduled-activity' # GET SET_ACTIVITY_ENDPOINT = 'activity' # PUT -SCHEDULE_ACTIVITY_ENDPOINT = 'scheduled-activity' # POST class AnnounceEndpoint: @@ -33,7 +32,7 @@ def register(self, app): """ app.add_routes([web.get('/{endpoint}'.format(endpoint=ACTIVITY_ENDPOINT), self.get_activities), web.put('/{endpoint}'.format(endpoint=SET_ACTIVITY_ENDPOINT), self.put_set_activity), - web.post('/{endpoint}'.format(endpoint=SCHEDULE_ACTIVITY_ENDPOINT), + web.put('/{endpoint}'.format(endpoint=ACTIVITY_ENDPOINT), self.post_schedule_activity), web.get('/{endpoint}'.format(endpoint=ANNOUNCE_ENDPOINT), self.get_announce)]) return app From 9b92096753554e0e599c4a993255e87ba1f18c14 Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 12 Sep 2022 16:22:34 +0100 Subject: [PATCH 13/30] Remove a unneeded file --- koala/cogs/announce/test_annoucement.py | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 koala/cogs/announce/test_annoucement.py diff --git a/koala/cogs/announce/test_annoucement.py b/koala/cogs/announce/test_annoucement.py deleted file mode 100644 index 0ee7f382..00000000 --- a/koala/cogs/announce/test_annoucement.py +++ /dev/null @@ -1,12 +0,0 @@ -import datetime - -import discord -import mock -import pytest -from discord.ext import commands -import discord.ext.test as dpytest -from sqlalchemy import null - -import koalabot -from koala.cogs.announce import AnnounceCog - From 7dadc186489b8812795efea0b8fd402c0217c509 Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Sat, 15 Oct 2022 15:04:35 +0100 Subject: [PATCH 14/30] Fix import formatting --- koala/cogs/announce/__init__.py | 4 ++-- koala/cogs/announce/announce_message.py | 12 +++++++----- koala/cogs/announce/api.py | 1 + koala/cogs/announce/cog.py | 11 +++++------ koala/cogs/announce/db.py | 1 + koala/cogs/announce/models.py | 2 +- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/koala/cogs/announce/__init__.py b/koala/cogs/announce/__init__.py index 44441578..f19adba6 100644 --- a/koala/cogs/announce/__init__.py +++ b/koala/cogs/announce/__init__.py @@ -1,7 +1,7 @@ -from . import cog -from . import api +from . import api, cog from .cog import AnnounceCog + def setup(bot): cog.setup(bot) api.setup(bot) diff --git a/koala/cogs/announce/announce_message.py b/koala/cogs/announce/announce_message.py index 364303ec..c8e75d9b 100644 --- a/koala/cogs/announce/announce_message.py +++ b/koala/cogs/announce/announce_message.py @@ -2,19 +2,21 @@ import math import time +# Libs +import discord +from discord.ext import commands + # Own modules import koalabot from koala.colours import KOALA_GREEN from koala.db import insert_extension from koala.utils import extract_id, wait_for_message + from .announce_message import AnnounceMessage from .db import AnnounceDBManager from .log import logger -from .utils import ANNOUNCE_SEPARATION_DAYS, SECONDS_IN_A_DAY, MAX_MESSAGE_LENGTH - -# Libs -import discord -from discord.ext import commands +from .utils import (ANNOUNCE_SEPARATION_DAYS, MAX_MESSAGE_LENGTH, + SECONDS_IN_A_DAY) def announce_is_enabled(guild): diff --git a/koala/cogs/announce/api.py b/koala/cogs/announce/api.py index 90c445ed..0526c834 100644 --- a/koala/cogs/announce/api.py +++ b/koala/cogs/announce/api.py @@ -5,6 +5,7 @@ from discord.ext.commands import Bot from koala.rest.api import parse_request + from . import cog # Own modules from .log import logger diff --git a/koala/cogs/announce/cog.py b/koala/cogs/announce/cog.py index 1324eb4e..8d8c6a50 100644 --- a/koala/cogs/announce/cog.py +++ b/koala/cogs/announce/cog.py @@ -3,16 +3,15 @@ import time import discord - -import koalabot -from announce_message import AnnounceMessage, Announce, announce_is_enabled - +from announce_message import Announce, AnnounceMessage, announce_is_enabled from discord.ext import commands, tasks from discord.ext.commands import BadArgument -from koala.cogs.announce.utils import MAX_MESSAGE_LENGTH, ANNOUNCE_SEPARATION_DAYS, SECONDS_IN_A_DAY +import koalabot +from koala.cogs.announce.utils import (ANNOUNCE_SEPARATION_DAYS, + MAX_MESSAGE_LENGTH, SECONDS_IN_A_DAY) from koala.log import logger -from koala.utils import wait_for_message, extract_id +from koala.utils import extract_id, wait_for_message def announce_is_enabled(guild): diff --git a/koala/cogs/announce/db.py b/koala/cogs/announce/db.py index 99394df4..39e09336 100644 --- a/koala/cogs/announce/db.py +++ b/koala/cogs/announce/db.py @@ -3,6 +3,7 @@ # Own modules from koala.db import session_manager + from .models import GuildUsage # Libs diff --git a/koala/cogs/announce/models.py b/koala/cogs/announce/models.py index b032c739..f31c2b53 100644 --- a/koala/cogs/announce/models.py +++ b/koala/cogs/announce/models.py @@ -1,4 +1,4 @@ -from sqlalchemy import Column, Integer, ForeignKey +from sqlalchemy import Column, ForeignKey, Integer from koala.db import setup from koala.models import mapper_registry From 3f2032f4eb4764c39d3fb297c0c06710d5e85e0f Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 16:31:51 +0100 Subject: [PATCH 15/30] Try fix imports and add API paths --- koala/cogs/announce/announce_message.py | 7 +------ koala/cogs/announce/api.py | 16 +++++++--------- koala/cogs/announce/cog.py | 5 ++--- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/koala/cogs/announce/announce_message.py b/koala/cogs/announce/announce_message.py index c8e75d9b..84a97409 100644 --- a/koala/cogs/announce/announce_message.py +++ b/koala/cogs/announce/announce_message.py @@ -1,5 +1,4 @@ # Built-in/Generic Imports -import math import time # Libs @@ -10,13 +9,9 @@ import koalabot from koala.colours import KOALA_GREEN from koala.db import insert_extension -from koala.utils import extract_id, wait_for_message -from .announce_message import AnnounceMessage from .db import AnnounceDBManager -from .log import logger -from .utils import (ANNOUNCE_SEPARATION_DAYS, MAX_MESSAGE_LENGTH, - SECONDS_IN_A_DAY) +from .utils import ANNOUNCE_SEPARATION_DAYS, SECONDS_IN_A_DAY def announce_is_enabled(guild): diff --git a/koala/cogs/announce/api.py b/koala/cogs/announce/api.py index 0526c834..ff08ac97 100644 --- a/koala/cogs/announce/api.py +++ b/koala/cogs/announce/api.py @@ -12,8 +12,9 @@ # Constants ANNOUNCE_ENDPOINT = 'announce' -ACTIVITY_ENDPOINT = 'scheduled-activity' # GET -SET_ACTIVITY_ENDPOINT = 'activity' # PUT + +ANNOUNCE_IS_ENABLED= 'announce_is_enabled' # GET +ANNOUNCE_ENOUGH_DAYS_PASSED = 'announce_enough_days_passed' class AnnounceEndpoint: @@ -31,21 +32,18 @@ def register(self, app): :param app: The aiohttp.web.Application (likely of the sub app) :return: app """ - app.add_routes([web.get('/{endpoint}'.format(endpoint=ACTIVITY_ENDPOINT), self.get_activities), - web.put('/{endpoint}'.format(endpoint=SET_ACTIVITY_ENDPOINT), self.put_set_activity), - web.put('/{endpoint}'.format(endpoint=ACTIVITY_ENDPOINT), - self.post_schedule_activity), - web.get('/{endpoint}'.format(endpoint=ANNOUNCE_ENDPOINT), self.get_announce)]) + app.add_routes([web.get('/{endpoint}'.format(endpoint=ANNOUNCE_IS_ENABLED), self.get_announce_is_enabled), + web.get('/{endpoint}'.format(endpoint=ANNOUNCE_ENOUGH_DAYS_PASSED), self.get_enough_days_passed)]) return app @parse_request -async def announce_is_enabled(guild): +async def get_announce_is_enabled(guild): return await cog.announce_is_enabled(guild) @parse_request -async def enough_days_passed(self, guild_id, ctx): +async def get_enough_days_passed(self, guild_id, ctx): return await cog.not_exceeded_limit(self, guild_id, ctx) diff --git a/koala/cogs/announce/cog.py b/koala/cogs/announce/cog.py index 8d8c6a50..fbe2d038 100644 --- a/koala/cogs/announce/cog.py +++ b/koala/cogs/announce/cog.py @@ -3,9 +3,8 @@ import time import discord -from announce_message import Announce, AnnounceMessage, announce_is_enabled -from discord.ext import commands, tasks -from discord.ext.commands import BadArgument +from announce_message import Announce, AnnounceMessage +from discord.ext import commands import koalabot from koala.cogs.announce.utils import (ANNOUNCE_SEPARATION_DAYS, From 9305ce0554fab461f0c4317e5eaf27dac2a30f8e Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 16:33:37 +0100 Subject: [PATCH 16/30] Add a missing .dot for import --- koala/cogs/announce/cog.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/koala/cogs/announce/cog.py b/koala/cogs/announce/cog.py index fbe2d038..a9436d4a 100644 --- a/koala/cogs/announce/cog.py +++ b/koala/cogs/announce/cog.py @@ -3,7 +3,6 @@ import time import discord -from announce_message import Announce, AnnounceMessage from discord.ext import commands import koalabot @@ -12,6 +11,8 @@ from koala.log import logger from koala.utils import extract_id, wait_for_message +from .announce_message import Announce, AnnounceMessage + def announce_is_enabled(guild): return announce_is_enabled(guild) From 4b501b99b9af93889757440867aa9a21873746a4 Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 16:36:15 +0100 Subject: [PATCH 17/30] Remove a unneeded import --- koala/cogs/announce/__init__.py | 1 - tests/cogs/announce/test_api.py | 17 +++++++---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/koala/cogs/announce/__init__.py b/koala/cogs/announce/__init__.py index f19adba6..435dad6c 100644 --- a/koala/cogs/announce/__init__.py +++ b/koala/cogs/announce/__init__.py @@ -1,5 +1,4 @@ from . import api, cog -from .cog import AnnounceCog def setup(bot): diff --git a/tests/cogs/announce/test_api.py b/tests/cogs/announce/test_api.py index a4289e00..ec8982ba 100644 --- a/tests/cogs/announce/test_api.py +++ b/tests/cogs/announce/test_api.py @@ -1,18 +1,15 @@ from http.client import BAD_REQUEST, CREATED, OK, UNPROCESSABLE_ENTITY -from mock import mock -from koala.cogs.announce.api import AnnounceEndpoint -from koala.db import get_all_available_guild_extensions -from koala.rest.api import parse_request - -import koalabot -from koala.cogs.base.api import AnnounceEndpoint - # Libs import discord -from aiohttp import web import pytest -import discord.ext.test as dpytest +from aiohttp import web + +import koalabot +from koala.cogs.announce.api import AnnounceEndpoint +from koala.cogs.base.api import AnnounceEndpoint +from koala.db import get_all_available_guild_extensions +from koala.rest.api import parse_request @pytest.fixture From ea320ec5cf2c8231df5842aa9f4e66fad7e29ca4 Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 16:42:19 +0100 Subject: [PATCH 18/30] Add import Announce to init --- koala/cogs/announce/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/koala/cogs/announce/__init__.py b/koala/cogs/announce/__init__.py index 435dad6c..4bcce1fb 100644 --- a/koala/cogs/announce/__init__.py +++ b/koala/cogs/announce/__init__.py @@ -1,4 +1,5 @@ from . import api, cog +from .cog import Announce def setup(bot): From 6fda91cc0b8a13e6007c6b0b88c30f7eb8799baf Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 16:43:55 +0100 Subject: [PATCH 19/30] Add missing setup to init --- koala/cogs/announce/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/koala/cogs/announce/__init__.py b/koala/cogs/announce/__init__.py index 4bcce1fb..180d574f 100644 --- a/koala/cogs/announce/__init__.py +++ b/koala/cogs/announce/__init__.py @@ -1,5 +1,5 @@ from . import api, cog -from .cog import Announce +from .cog import Announce, setup def setup(bot): From 8aa0dd3d9ca290f869ad02431adac8aae6504a37 Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 16:48:58 +0100 Subject: [PATCH 20/30] Remove unneeded import --- tests/cogs/announce/test_api.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/cogs/announce/test_api.py b/tests/cogs/announce/test_api.py index ec8982ba..4695b4ae 100644 --- a/tests/cogs/announce/test_api.py +++ b/tests/cogs/announce/test_api.py @@ -7,7 +7,6 @@ import koalabot from koala.cogs.announce.api import AnnounceEndpoint -from koala.cogs.base.api import AnnounceEndpoint from koala.db import get_all_available_guild_extensions from koala.rest.api import parse_request From edccd469baa318fbfdb9c68e716df3340ce53f7c Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 17:02:48 +0100 Subject: [PATCH 21/30] Reformat the cog to original --- koala/cogs/announce/announce_message.py | 132 ------------------- koala/cogs/announce/api.py | 31 +---- koala/cogs/announce/cog.py | 165 ++++++++++++------------ 3 files changed, 85 insertions(+), 243 deletions(-) diff --git a/koala/cogs/announce/announce_message.py b/koala/cogs/announce/announce_message.py index 84a97409..3b15dd25 100644 --- a/koala/cogs/announce/announce_message.py +++ b/koala/cogs/announce/announce_message.py @@ -1,35 +1,3 @@ -# Built-in/Generic Imports -import time - -# Libs -import discord -from discord.ext import commands - -# Own modules -import koalabot -from koala.colours import KOALA_GREEN -from koala.db import insert_extension - -from .db import AnnounceDBManager -from .utils import ANNOUNCE_SEPARATION_DAYS, SECONDS_IN_A_DAY - - -def announce_is_enabled(guild): - """ - A command used to check if the guild has enabled announce - e.g. @commands.check(announce_is_enabled) - - :param guild: The context of the message - :return: True if enabled or test, False otherwise - """ - try: - result = koalabot.check_guild_has_ext(guild, "Announce") - except PermissionError: - result = False - - return result - - class AnnounceMessage: """ A class consisting the information about a announcement message @@ -61,103 +29,3 @@ def set_description(self, message): :return: """ self.description = message - - -class Announce: - """ - Send DM announcements to certain roles and people. - """ - - def __init__(self, bot): - self.bot = bot - self.messages = {} - self.roles = {} - insert_extension("Announce", 0, True, True) - self.announce_database_manager = AnnounceDBManager() - - def not_exceeded_limit(self, guild_id): - """ - Check if enough days have passed for the user to use the announce function - :return: - """ - if self.announce_database_manager.get_last_use_date(guild_id): - last_use = self.announce_database_manager.get_last_use_date(guild_id) - if (time.time() - last_use) > ANNOUNCE_SEPARATION_DAYS * SECONDS_IN_A_DAY: - return True - else: - return False - - def has_active_message(self, guild_id): - """ - Check if a particular id has an active announcement pending announcement - :param guild_id: The id of the guild of the command - :return: Boolean of whether there is an active announcement or not - """ - if guild_id in self.messages: - return True - else: - return False - - def get_role_names(self, guild_id): - """ - Get the names of the roles that are currently being announced - :param guild_id: The id of the guild of the command - :return: A list of strings consisting the names of the roles - """ - if guild_id in self.roles: - return self.roles[guild_id] - else: - return [] - - def get_receivers(self, guild_id): - """ - A function to get the receivers of a particular announcement - :param roles: The list of roles in the guild - :param guild_id: The id of the guild - :return: All the receivers of the announcement - """ - receivers = [] - if guild_id in self.roles: - roles = self.roles[guild_id] - for role in roles: - for member in role.members: - receivers.append(member) - return receivers - - def receiver_msg(self, guild): - """ - A function to create a string message about receivers - :param guild: The guild of the bot - :return: A string message about receivers - """ - if not self.roles[guild.id]: - return f"You are currently sending to Everyone and there are {str(len(guild.members))} receivers" - else: - receivers = self.get_receivers(guild.id) - return f"You are currently sending to {str(len(self.roles[guild.id]))} roles and {str(len(receivers))} receivers" - - def construct_embed(self, guild: discord.Guild): - """ - Constructing an embedded message from the information stored in the manager - :param guild: The the guild - :return: An embedded message for the announcement - """ - message = self.messages[guild.id] - embed: discord.Embed = discord.Embed(title=message.title, - description=message.description, colour=KOALA_GREEN) - embed.set_author(name="Announcement from " + guild.name) - if message.thumbnail != 'https://cdn.discordapp.com/': - embed.set_thumbnail(url=message.thumbnail) - return embed - - @commands.check(announce_is_enabled) - @commands.group(name="announce") - async def announce(self, ctx): - """ - The main command for the announce cog - :param ctx: The context of the command - :return: - """ - if ctx.invoked_subcommand is None: - await ctx.send( - f"{ctx.author.mention}, please use `{ctx.prefix}announce help` to see the help for this command") diff --git a/koala/cogs/announce/api.py b/koala/cogs/announce/api.py index ff08ac97..db34619d 100644 --- a/koala/cogs/announce/api.py +++ b/koala/cogs/announce/api.py @@ -32,8 +32,7 @@ def register(self, app): :param app: The aiohttp.web.Application (likely of the sub app) :return: app """ - app.add_routes([web.get('/{endpoint}'.format(endpoint=ANNOUNCE_IS_ENABLED), self.get_announce_is_enabled), - web.get('/{endpoint}'.format(endpoint=ANNOUNCE_ENOUGH_DAYS_PASSED), self.get_enough_days_passed)]) + app.add_routes([web.get('/{endpoint}'.format(endpoint=ANNOUNCE_IS_ENABLED), self.get_announce_is_enabled)]) return app @@ -42,34 +41,6 @@ async def get_announce_is_enabled(guild): return await cog.announce_is_enabled(guild) -@parse_request -async def get_enough_days_passed(self, guild_id, ctx): - return await cog.not_exceeded_limit(self, guild_id, ctx) - - -@parse_request -async def has_active_message(self, guild_id, ctx): - return await cog.has_active_msg(self, guild_id, ctx) - - -@parse_request -async def get_names_of_roles(self, guild_id, roles, ctx): - return await cog.get_role_names(self, guild_id, roles, ctx) - - -@parse_request() -async def get_receivers_of_announcement(self, guild_id, roles, ctx): - return await cog.get_receivers(self, guild_id, roles, ctx) - -@parse_request() -async def receiver_msg_create(self, guild, ctx): - return await cog.receiver_msg_create(self, guild, ctx) - -@parse_request() -async def construct_embed_message(self, guild, ctx): - return await cog.construct_embed_message(self, guild, ctx) - - def setup(bot: Bot): """ Load this cog to the KoalaBot. diff --git a/koala/cogs/announce/cog.py b/koala/cogs/announce/cog.py index a9436d4a..178a073f 100644 --- a/koala/cogs/announce/cog.py +++ b/koala/cogs/announce/cog.py @@ -1,112 +1,117 @@ -# Own imports +# Built-in/Generic Imports import math import time +# Libs import discord from discord.ext import commands +# Own modules import koalabot -from koala.cogs.announce.utils import (ANNOUNCE_SEPARATION_DAYS, - MAX_MESSAGE_LENGTH, SECONDS_IN_A_DAY) -from koala.log import logger +from koala.colours import KOALA_GREEN +from koala.db import insert_extension from koala.utils import extract_id, wait_for_message -from .announce_message import Announce, AnnounceMessage +from .announce_message import AnnounceMessage +from .db import AnnounceDBManager +from .log import logger +from .utils import (ANNOUNCE_SEPARATION_DAYS, MAX_MESSAGE_LENGTH, + SECONDS_IN_A_DAY) -def announce_is_enabled(guild): - return announce_is_enabled(guild) - - -def not_exceeded_limit(self, guild_id, ctx): - """ - Check if the number of announcements in the guild is not exceeded - :param guild_id: The id of the guild - :param ctx: The context of the message - :return: True if the number of announcements is not exceeded, False otherwise +def announce_is_enabled(ctx): """ - try: - result = self.Announce.not_exceeded_limit(guild_id, ctx) - except PermissionError: - result = False - - ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) - + A command used to check if the guild has enabled announce + e.g. @commands.check(announce_is_enabled) -def has_active_msg(self, guild_id, ctx): - """ - Check if there is an active announcement message - :param guild_id: The id of the guild :param ctx: The context of the message - :return: True if there is an active announcement message, False otherwise + :return: True if enabled or test, False otherwise """ try: - result = self.Announce.has_active_msg(guild_id, ctx) + result = koalabot.check_guild_has_ext(ctx, "Announce") except PermissionError: result = False - ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) + return result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest) -def get_role_names(self, guild_id, roles, ctx): +class Announce(commands.Cog): """ - Get the names of the roles - :param guild_id: The id of the guild - :param roles: The roles - :param ctx: The context of the message - :return: A string consisting the names of the roles + Send DM announcements to certain roles and people. """ - try: - result = self.Announce.get_role_names(guild_id, roles, ctx) - except PermissionError: - result = False - ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) + def __init__(self, bot): + self.bot = bot + self.messages = {} + self.roles = {} + insert_extension("Announce", 0, True, True) + self.announce_database_manager = AnnounceDBManager() + def not_exceeded_limit(self, guild_id): + """ + Check if enough days have passed for the user to use the announce function + :return: + """ + if self.announce_database_manager.get_last_use_date(guild_id): + return int(time.time()) - self.announce_database_manager.get_last_use_date( + guild_id) > ANNOUNCE_SEPARATION_DAYS * SECONDS_IN_A_DAY + return True -def get_receivers(self, guild_id, roles, ctx): - """ - Get the receivers of the announcement - :param guild_id: The id of the guild - :param roles: The roles - :param ctx: The context of the message - :return: A list of the receivers - """ - try: - result = self.Announce.get_receivers(guild_id, roles, ctx) - except PermissionError: - result = False + def has_active_msg(self, guild_id): + """ + Check if a particular id has an active announcement pending announcement + :param guild_id: The id of the guild of the command + :return: Boolean of whether there is an active announcement or not + """ + return guild_id in self.messages.keys() - ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) + def get_role_names(self, guild_id, roles): + """ + A function to get the names of all the roles the announcement will be sent to + :param roles: The list of roles in the guild + :param guild_id: The id of the guild + :return: All the names of the roles that are tagged + """ + temp = [] + for role in self.roles[guild_id]: + temp.append(discord.utils.get(roles, id=role).name) + return temp + def get_receivers(self, guild_id, roles): + """ + A function to get the receivers of a particular announcement + :param roles: The list of roles in the guild + :param guild_id: The id of the guild + :return: All the receivers of the announcement + """ + temp = [] + for role in self.roles[guild_id]: + temp += discord.utils.get(roles, id=role).members + return list(set(temp)) -def receiver_msg(self, guild, ctx): - """ + def receiver_msg(self, guild): + """ A function to create a string message about receivers :param guild: The guild of the bot :return: A string message about receivers """ - try: - result = self.Announce.receiver_msg(guild, ctx) - except PermissionError: - result = False + if not self.roles[guild.id]: + return f"You are currently sending to Everyone and there are {str(len(guild.members))} receivers" + return f"You are currently sending to {self.get_role_names(guild.id, guild.roles)} and there are {str(len(self.get_receivers(guild.id, guild.roles)))} receivers " - ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) - - -def construct_embed(self, guild: discord.Guild, ctx): - """ - Construct an embed message - :param guild: The guild of the bot - :param ctx: The context of the message - :return: An embed message - """ - try: - result = self.Announce.construct_embed(guild, ctx) - except PermissionError: - result = False - - ctx.send(result or (str(ctx.guild) == koalabot.TEST_USER and koalabot.is_dpytest)) + def construct_embed(self, guild: discord.Guild): + """ + Constructing an embedded message from the information stored in the manager + :param guild: The the guild + :return: An embedded message for the announcement + """ + message = self.messages[guild.id] + embed: discord.Embed = discord.Embed(title=message.title, + description=message.description, colour=KOALA_GREEN) + embed.set_author(name="Announcement from " + guild.name) + if message.thumbnail != 'https://cdn.discordapp.com/': + embed.set_thumbnail(url=message.thumbnail) + return embed @commands.check(announce_is_enabled) @commands.group(name="announce") @@ -202,8 +207,7 @@ async def add_role(self, ctx): :return: """ if self.has_active_msg(ctx.guild.id): - await ctx.send( - "Please enter the roles you want to tag separated by space, I'll wait for 60 seconds, no rush.") + await ctx.send("Please enter the roles you want to tag separated by space, I'll wait for 60 seconds, no rush.") message, channel = await wait_for_message(self.bot, ctx) if not message: await channel.send("Okay, I'll cancel the command.") @@ -226,8 +230,7 @@ async def remove_role(self, ctx): :return: """ if self.has_active_msg(ctx.guild.id): - await ctx.send( - "Please enter the roles you want to remove separated by space, I'll wait for 60 seconds, no rush.") + await ctx.send("Please enter the roles you want to remove separated by space, I'll wait for 60 seconds, no rush.") message, channel = await wait_for_message(self.bot, ctx) if not message: await channel.send("Okay, I'll cancel the command.") @@ -269,13 +272,13 @@ async def send(self, ctx): try: await receiver.send(embed=embed) except (discord.Forbidden, AttributeError, discord.HTTPException) as e: - logger.error(f'User {receiver.id} cannot receive dms') + logger.error(f'User {receiver.id} cannot recieve dms') else: for receiver in ctx.guild.members: try: await receiver.send(embed=embed) except (discord.Forbidden, AttributeError, discord.HTTPException) as e: - logger.error(f'User {receiver.id} cannot receive dms') + logger.error(f'User {receiver.id} cannot recieve dms') self.messages.pop(ctx.guild.id) self.roles.pop(ctx.guild.id) From 44c916edec2c4883c396e684e853eaaeb2331d7f Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 17:11:43 +0100 Subject: [PATCH 22/30] Try re-adding other API modules --- koala/cogs/announce/api.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/koala/cogs/announce/api.py b/koala/cogs/announce/api.py index db34619d..c04c128c 100644 --- a/koala/cogs/announce/api.py +++ b/koala/cogs/announce/api.py @@ -1,20 +1,21 @@ # Futures # Built-in/Generic Imports # Libs +from http.client import CREATED, OK, BAD_REQUEST from aiohttp import web +import discord from discord.ext.commands import Bot -from koala.rest.api import parse_request - -from . import cog # Own modules +from . import cog from .log import logger +from koala.rest.api import parse_request, build_response +from koala.utils import convert_iso_datetime # Constants ANNOUNCE_ENDPOINT = 'announce' ANNOUNCE_IS_ENABLED= 'announce_is_enabled' # GET -ANNOUNCE_ENOUGH_DAYS_PASSED = 'announce_enough_days_passed' class AnnounceEndpoint: From f3022bd78da8e64658c015e75cb759636d98040e Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 17:16:13 +0100 Subject: [PATCH 23/30] Fix API route by identation --- koala/cogs/announce/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/koala/cogs/announce/api.py b/koala/cogs/announce/api.py index c04c128c..243941e0 100644 --- a/koala/cogs/announce/api.py +++ b/koala/cogs/announce/api.py @@ -37,9 +37,9 @@ def register(self, app): return app -@parse_request -async def get_announce_is_enabled(guild): - return await cog.announce_is_enabled(guild) + @parse_request + async def get_announce_is_enabled(guild): + return await cog.announce_is_enabled(guild) def setup(bot: Bot): From 03c09dfccb3b779cb9bb58c7c0c890b1b823064c Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 17:25:36 +0100 Subject: [PATCH 24/30] Revert init file to see if it works --- koala/cogs/announce/__init__.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/koala/cogs/announce/__init__.py b/koala/cogs/announce/__init__.py index 180d574f..bcc0d1f7 100644 --- a/koala/cogs/announce/__init__.py +++ b/koala/cogs/announce/__init__.py @@ -1,7 +1,3 @@ -from . import api, cog +from . import utils, db, log, models from .cog import Announce, setup - - -def setup(bot): - cog.setup(bot) - api.setup(bot) +from .announce_message import AnnounceMessage \ No newline at end of file From 2a5b64401266e83dc324444757b3638400c1fb36 Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 17:30:48 +0100 Subject: [PATCH 25/30] Try add api as a import to init --- koala/cogs/announce/__init__.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/koala/cogs/announce/__init__.py b/koala/cogs/announce/__init__.py index bcc0d1f7..fa6b23c1 100644 --- a/koala/cogs/announce/__init__.py +++ b/koala/cogs/announce/__init__.py @@ -1,3 +1,10 @@ -from . import utils, db, log, models +from koala.cogs.announce import api + +from . import db, log, models, utils +from .announce_message import AnnounceMessage from .cog import Announce, setup -from .announce_message import AnnounceMessage \ No newline at end of file + + +def setup(bot): + cog.setup(bot) + api.setup(bot) \ No newline at end of file From bd620aa9eeec9e343c136860c77fa41bcc35326f Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 17:31:57 +0100 Subject: [PATCH 26/30] Improve the import for cog and api init --- koala/cogs/announce/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/koala/cogs/announce/__init__.py b/koala/cogs/announce/__init__.py index fa6b23c1..e414fc07 100644 --- a/koala/cogs/announce/__init__.py +++ b/koala/cogs/announce/__init__.py @@ -1,6 +1,4 @@ -from koala.cogs.announce import api - -from . import db, log, models, utils +from . import api, cog, db, log, models, utils from .announce_message import AnnounceMessage from .cog import Announce, setup From 0d2ef60c070c01e177c05351e462f0cc5fd0ec9b Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 17:41:08 +0100 Subject: [PATCH 27/30] Add a conftest --- tests/cogs/announce/conftest.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/cogs/announce/conftest.py diff --git a/tests/cogs/announce/conftest.py b/tests/cogs/announce/conftest.py new file mode 100644 index 00000000..bc7cd0da --- /dev/null +++ b/tests/cogs/announce/conftest.py @@ -0,0 +1,13 @@ +import pytest +from aiohttp import web +from discord.ext.commands import Bot + +from koala.cogs.announce.api import AnnounceEndpoint + + +@pytest.fixture(autouse=True) +def setup_attributes(bot: Bot): + app = web.Application() + endpoint = AnnounceEndpoint(bot) + endpoint.register(app) + setattr(bot, "koala_web_app", app) \ No newline at end of file From 2bb83c61ccc3d8f6a2953d4ea5a7ca553b312e11 Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 18:01:14 +0100 Subject: [PATCH 28/30] Test removing setup import --- koala/cogs/announce/__init__.py | 2 +- koala/cogs/announce/api.py | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/koala/cogs/announce/__init__.py b/koala/cogs/announce/__init__.py index e414fc07..9a9043cf 100644 --- a/koala/cogs/announce/__init__.py +++ b/koala/cogs/announce/__init__.py @@ -1,6 +1,6 @@ from . import api, cog, db, log, models, utils from .announce_message import AnnounceMessage -from .cog import Announce, setup +from .cog import Announce def setup(bot): diff --git a/koala/cogs/announce/api.py b/koala/cogs/announce/api.py index 243941e0..e755ef55 100644 --- a/koala/cogs/announce/api.py +++ b/koala/cogs/announce/api.py @@ -1,16 +1,18 @@ # Futures # Built-in/Generic Imports # Libs -from http.client import CREATED, OK, BAD_REQUEST -from aiohttp import web +from http.client import BAD_REQUEST, CREATED, OK + import discord +from aiohttp import web from discord.ext.commands import Bot +from koala.rest.api import build_response, parse_request +from koala.utils import convert_iso_datetime + # Own modules from . import cog from .log import logger -from koala.rest.api import parse_request, build_response -from koala.utils import convert_iso_datetime # Constants ANNOUNCE_ENDPOINT = 'announce' From 6611177d03f86301c90613e3aa981a6efbcc43bb Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Mon, 17 Oct 2022 18:19:32 +0100 Subject: [PATCH 29/30] Remove load all cogs from test_core --- tests/cogs/base/test_core.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/cogs/base/test_core.py b/tests/cogs/base/test_core.py index e550ee14..ceca35d6 100644 --- a/tests/cogs/base/test_core.py +++ b/tests/cogs/base/test_core.py @@ -208,7 +208,6 @@ async def test_list_enabled_extensions(bot: commands.Bot): @mock.patch("koalabot.ENABLED_COGS", ["announce"]) @pytest.mark.asyncio async def test_get_extensions(bot: commands.Bot): - koalabot.load_all_cogs() guild: discord.Guild = dpytest.get_config().guilds[0] resp = core.get_all_available_guild_extensions(guild.id) print(resp) From 5e9b48bcb0a9711a0a24d784dc29b6003f880f5d Mon Sep 17 00:00:00 2001 From: Otto Hooper Date: Sat, 22 Oct 2022 16:48:56 +0100 Subject: [PATCH 30/30] Try testing announce_cog_enabled --- koala/cogs/announce/api.py | 4 ++-- tests/cogs/announce/test_api.py | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/koala/cogs/announce/api.py b/koala/cogs/announce/api.py index e755ef55..66045672 100644 --- a/koala/cogs/announce/api.py +++ b/koala/cogs/announce/api.py @@ -17,7 +17,7 @@ # Constants ANNOUNCE_ENDPOINT = 'announce' -ANNOUNCE_IS_ENABLED= 'announce_is_enabled' # GET +ANNOUNCE_STATUS= 'announce_status' # GET class AnnounceEndpoint: @@ -35,7 +35,7 @@ def register(self, app): :param app: The aiohttp.web.Application (likely of the sub app) :return: app """ - app.add_routes([web.get('/{endpoint}'.format(endpoint=ANNOUNCE_IS_ENABLED), self.get_announce_is_enabled)]) + app.add_routes([web.get('/{endpoint}'.format(endpoint=ANNOUNCE_STATUS), self.get_announce_is_enabled)]) return app diff --git a/tests/cogs/announce/test_api.py b/tests/cogs/announce/test_api.py index 4695b4ae..70609596 100644 --- a/tests/cogs/announce/test_api.py +++ b/tests/cogs/announce/test_api.py @@ -16,4 +16,10 @@ def api_client(bot: discord.ext.commands.Bot, aiohttp_client, loop ): app = web.Application() endpoint = AnnounceEndpoint(bot) app = endpoint.register(app) - return loop.run_until_complete(aiohttp_client(app)) \ No newline at end of file + return loop.run_until_complete(aiohttp_client(app)) + +# Check if announce cog is enabled +async def test_is_announce_cog_enabled(api_client): + resp = await api_client.get('/announce_status?guild=1') + assert resp.status == OK + assert await resp.json() == True \ No newline at end of file