From 9a6e68f7b0cbc4696f596fe7ddbeb0736763d56c Mon Sep 17 00:00:00 2001 From: Numerlor Date: Tue, 1 Oct 2019 19:53:35 +0200 Subject: Add #event-hacktoberfest-2019 to bot command whitelist --- bot/constants.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/constants.py b/bot/constants.py index dbf35754..53821cfa 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -52,6 +52,7 @@ class Channels(NamedTuple): python_discussion = 267624335836053506 show_your_projects = int(environ.get("CHANNEL_SHOW_YOUR_PROJECTS", 303934982764625920)) show_your_projects_discussion = 360148304664723466 + hacktoberfest_2019 = 628184417646411776 class Client(NamedTuple): @@ -125,7 +126,7 @@ STAFF_ROLES = Roles.helpers, Roles.moderator, Roles.admin, Roles.owner WHITELISTED_CHANNELS = ( Channels.bot, Channels.seasonalbot_commands, Channels.off_topic_0, Channels.off_topic_1, Channels.off_topic_2, - Channels.devtest, + Channels.devtest, Channels.hacktoberfest_2019, ) # Bot replies -- cgit v1.2.3 From 5d41b70019f211a7f9d84385100a05d800c95922 Mon Sep 17 00:00:00 2001 From: Numerlor Date: Tue, 1 Oct 2019 20:03:16 +0200 Subject: Add +14h to start UTC offset and -11 to end UTC offset to date_range in query for Hacktoberfest user pull requests --- bot/seasons/halloween/hacktoberstats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/seasons/halloween/hacktoberstats.py b/bot/seasons/halloween/hacktoberstats.py index 20797037..7fbd9b62 100644 --- a/bot/seasons/halloween/hacktoberstats.py +++ b/bot/seasons/halloween/hacktoberstats.py @@ -220,7 +220,7 @@ class HacktoberStats(commands.Cog): not_label = "invalid" action_type = "pr" is_query = f"public+author:{github_username}" - date_range = f"{CURRENT_YEAR}-10-01..{CURRENT_YEAR}-10-31" + date_range = f"{CURRENT_YEAR}-10-01T00:00:00%2B14:00..{CURRENT_YEAR}-10-31T00:00:00-11:00" per_page = "300" query_url = ( f"{base_url}" -- cgit v1.2.3 From 65fd7c840c595fa408ef38c6fd8627d881986007 Mon Sep 17 00:00:00 2001 From: Numerlor Date: Tue, 1 Oct 2019 21:37:31 +0200 Subject: remove hacktoberfest channel from global whitelist --- bot/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/constants.py b/bot/constants.py index 53821cfa..0d4321c8 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -126,7 +126,7 @@ STAFF_ROLES = Roles.helpers, Roles.moderator, Roles.admin, Roles.owner WHITELISTED_CHANNELS = ( Channels.bot, Channels.seasonalbot_commands, Channels.off_topic_0, Channels.off_topic_1, Channels.off_topic_2, - Channels.devtest, Channels.hacktoberfest_2019, + Channels.devtest, ) # Bot replies -- cgit v1.2.3 From 2aed38d7b6383db9cd07be8e5595dc1cb86e029b Mon Sep 17 00:00:00 2001 From: Numerlor Date: Tue, 1 Oct 2019 21:38:04 +0200 Subject: allow hacktoberstats commands to be used in the hacktober channel --- bot/seasons/halloween/hacktoberstats.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'bot') diff --git a/bot/seasons/halloween/hacktoberstats.py b/bot/seasons/halloween/hacktoberstats.py index 7fbd9b62..625e7324 100644 --- a/bot/seasons/halloween/hacktoberstats.py +++ b/bot/seasons/halloween/hacktoberstats.py @@ -10,8 +10,11 @@ import aiohttp import discord from discord.ext import commands +from bot.constants import Channels, STAFF_ROLES, WHITELISTED_CHANNELS +from bot.decorators import in_channel_check from bot.utils.persist import make_persistent + log = logging.getLogger(__name__) CURRENT_YEAR = datetime.now().year # Used to construct GH API query @@ -26,6 +29,7 @@ class HacktoberStats(commands.Cog): self.link_json = make_persistent(Path("bot", "resources", "halloween", "github_links.json")) self.linked_accounts = self.load_linked_users() + @commands.check(in_channel_check(*(*WHITELISTED_CHANNELS, Channels.hacktoberfest_2019), bypass_roles=STAFF_ROLES)) @commands.group(name="hacktoberstats", aliases=("hackstats",), invoke_without_command=True) async def hacktoberstats_group(self, ctx: commands.Context, github_username: str = None) -> None: """ -- cgit v1.2.3 From 9b19f550716c1ae06c11115c2f31b33031ebe897 Mon Sep 17 00:00:00 2001 From: Numerlor Date: Tue, 1 Oct 2019 23:07:58 +0200 Subject: fix typo in header --- bot/seasons/halloween/hacktoberstats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/seasons/halloween/hacktoberstats.py b/bot/seasons/halloween/hacktoberstats.py index 625e7324..3e9bd508 100644 --- a/bot/seasons/halloween/hacktoberstats.py +++ b/bot/seasons/halloween/hacktoberstats.py @@ -235,7 +235,7 @@ class HacktoberStats(commands.Cog): f"&per_page={per_page}" ) - headers = {"user-agent": "Discord Python Hactoberbot"} + headers = {"user-agent": "Discord Python Hacktoberbot"} async with aiohttp.ClientSession() as session: async with session.get(query_url, headers=headers) as resp: jsonresp = await resp.json() -- cgit v1.2.3 From ab0cefb02910e0c16c42e30bcc84d14de62be06b Mon Sep 17 00:00:00 2001 From: Numerlor Date: Wed, 2 Oct 2019 00:13:18 +0200 Subject: rewrite override_in_channel to accept an optional new channel whitelist add checks for the new whitelist to in_channel_check --- bot/decorators.py | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'bot') diff --git a/bot/decorators.py b/bot/decorators.py index dbaad4a2..d7ce29cb 100644 --- a/bot/decorators.py +++ b/bot/decorators.py @@ -64,12 +64,13 @@ def without_role(*role_ids: int) -> bool: def in_channel_check(*channels: int, bypass_roles: typing.Container[int] = None) -> typing.Callable[[Context], bool]: - """Checks that the message is in a whitelisted channel or optionally has a bypass role.""" + """Checks that the message is in a whitelisted channel or optionally has a bypass role. + + If `in_channel_override` is present, check if it contains channels and use them in place of global whitelist""" def predicate(ctx: Context) -> bool: if not ctx.guild: log.debug(f"{ctx.author} tried to use the '{ctx.command.name}' command from a DM.") return True - if ctx.channel.id in channels: log.debug( f"{ctx.author} tried to call the '{ctx.command.name}' command " @@ -78,11 +79,24 @@ def in_channel_check(*channels: int, bypass_roles: typing.Container[int] = None) return True if hasattr(ctx.command.callback, "in_channel_override"): - log.debug( - f"{ctx.author} called the '{ctx.command.name}' command " - f"and the command was whitelisted to bypass the in_channel check." - ) - return True + override = ctx.command.callback.in_channel_override + if override is None: + log.debug( + f"{ctx.author} called the '{ctx.command.name}' command " + f"and the command was whitelisted to bypass the in_channel check." + ) + return True + else: + if ctx.channel.id in override: + log.debug( + f"{ctx.author} tried to call the '{ctx.command.name}' command " + f"and the command was used in an overridden whitelisted channel." + ) + return True + channels_str = ', '.join(f"<#{c_id}>" for c_id in override) + raise InChannelCheckFailure( + f"Sorry, but you may only use this command within {channels_str}." + ) if bypass_roles and any(r.id in bypass_roles for r in ctx.author.roles): log.debug( @@ -107,14 +121,19 @@ def in_channel_check(*channels: int, bypass_roles: typing.Container[int] = None) in_channel = commands.check(in_channel_check) -def override_in_channel(func: typing.Callable) -> typing.Callable: +def override_in_channel(channels: typing.Tuple[int] = None) -> typing.Callable: """ Set command callback attribute for detection in `in_channel_check`. + Override global whitelist if channels are specified. + This decorator has to go before (below) below the `command` decorator. """ - func.in_channel_override = True - return func + def inner(func: typing.Callable): + func.in_channel_override = channels + return func + + return inner def locked() -> typing.Union[typing.Callable, None]: -- cgit v1.2.3 From 3662734b9434e0aa10f11bbe37f3dece8b311984 Mon Sep 17 00:00:00 2001 From: Numerlor Date: Wed, 2 Oct 2019 00:14:05 +0200 Subject: Use rewritten override_in_channel to create a new whitelist for the command --- bot/seasons/halloween/hacktoberstats.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'bot') diff --git a/bot/seasons/halloween/hacktoberstats.py b/bot/seasons/halloween/hacktoberstats.py index 3e9bd508..035eafbc 100644 --- a/bot/seasons/halloween/hacktoberstats.py +++ b/bot/seasons/halloween/hacktoberstats.py @@ -10,8 +10,8 @@ import aiohttp import discord from discord.ext import commands -from bot.constants import Channels, STAFF_ROLES, WHITELISTED_CHANNELS -from bot.decorators import in_channel_check +from bot.constants import Channels, WHITELISTED_CHANNELS +from bot.decorators import override_in_channel from bot.utils.persist import make_persistent @@ -19,6 +19,7 @@ log = logging.getLogger(__name__) CURRENT_YEAR = datetime.now().year # Used to construct GH API query PRS_FOR_SHIRT = 4 # Minimum number of PRs before a shirt is awarded +HACKTOBER_WHITELIST = WHITELISTED_CHANNELS + (Channels.hacktoberfest_2019,) class HacktoberStats(commands.Cog): @@ -29,8 +30,8 @@ class HacktoberStats(commands.Cog): self.link_json = make_persistent(Path("bot", "resources", "halloween", "github_links.json")) self.linked_accounts = self.load_linked_users() - @commands.check(in_channel_check(*(*WHITELISTED_CHANNELS, Channels.hacktoberfest_2019), bypass_roles=STAFF_ROLES)) @commands.group(name="hacktoberstats", aliases=("hackstats",), invoke_without_command=True) + @override_in_channel(HACKTOBER_WHITELIST) async def hacktoberstats_group(self, ctx: commands.Context, github_username: str = None) -> None: """ Display an embed for a user's Hacktoberfest contributions. -- cgit v1.2.3 From 619ee9fd2106d0778e90ae782be1c66abe516cba Mon Sep 17 00:00:00 2001 From: Numerlor Date: Wed, 2 Oct 2019 00:14:21 +0200 Subject: call the rewritten override_in_channel --- bot/seasons/christmas/adventofcode.py | 2 +- bot/seasons/evergreen/issues.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/seasons/christmas/adventofcode.py b/bot/seasons/christmas/adventofcode.py index 6609387e..513c1020 100644 --- a/bot/seasons/christmas/adventofcode.py +++ b/bot/seasons/christmas/adventofcode.py @@ -126,7 +126,7 @@ class AdventOfCode(commands.Cog): self.status_task = asyncio.ensure_future(self.bot.loop.create_task(status_coro)) @commands.group(name="adventofcode", aliases=("aoc",), invoke_without_command=True) - @override_in_channel + @override_in_channel() async def adventofcode_group(self, ctx: commands.Context) -> None: """All of the Advent of Code commands.""" await ctx.send_help(ctx.command) diff --git a/bot/seasons/evergreen/issues.py b/bot/seasons/evergreen/issues.py index 0ba74d9c..438ab475 100644 --- a/bot/seasons/evergreen/issues.py +++ b/bot/seasons/evergreen/issues.py @@ -16,7 +16,7 @@ class Issues(commands.Cog): self.bot = bot @commands.command(aliases=("issues",)) - @override_in_channel + @override_in_channel() async def issue( self, ctx: commands.Context, number: int, repository: str = "seasonalbot", user: str = "python-discord" ) -> None: -- cgit v1.2.3 From 1c7a7371bde7a31f5fbda2730b0e81761ed08be4 Mon Sep 17 00:00:00 2001 From: Numerlor Date: Wed, 2 Oct 2019 00:17:35 +0200 Subject: add return type hint to override_in_channel inner function --- bot/decorators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/decorators.py b/bot/decorators.py index d7ce29cb..8748fd83 100644 --- a/bot/decorators.py +++ b/bot/decorators.py @@ -129,7 +129,7 @@ def override_in_channel(channels: typing.Tuple[int] = None) -> typing.Callable: This decorator has to go before (below) below the `command` decorator. """ - def inner(func: typing.Callable): + def inner(func: typing.Callable) -> typing.Callable: func.in_channel_override = channels return func -- cgit v1.2.3 From 2f349294d94eac5eec172674e868dcf281e69687 Mon Sep 17 00:00:00 2001 From: Numerlor Date: Wed, 2 Oct 2019 00:31:09 +0200 Subject: fix in_channel_check docstring style --- bot/decorators.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/decorators.py b/bot/decorators.py index 8748fd83..32c017d7 100644 --- a/bot/decorators.py +++ b/bot/decorators.py @@ -64,9 +64,11 @@ def without_role(*role_ids: int) -> bool: def in_channel_check(*channels: int, bypass_roles: typing.Container[int] = None) -> typing.Callable[[Context], bool]: - """Checks that the message is in a whitelisted channel or optionally has a bypass role. + """ + Checks that the message is in a whitelisted channel or optionally has a bypass role. - If `in_channel_override` is present, check if it contains channels and use them in place of global whitelist""" + If `in_channel_override` is present, check if it contains channels and use them in place of global whitelist + """ def predicate(ctx: Context) -> bool: if not ctx.guild: log.debug(f"{ctx.author} tried to use the '{ctx.command.name}' command from a DM.") -- cgit v1.2.3 From 678e2dca0ef182ea51982547e78473d20457b46c Mon Sep 17 00:00:00 2001 From: Numerlor Date: Wed, 2 Oct 2019 14:03:26 +0200 Subject: add logging when an overriden in_channel_check fails --- bot/decorators.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'bot') diff --git a/bot/decorators.py b/bot/decorators.py index 32c017d7..471ea3be 100644 --- a/bot/decorators.py +++ b/bot/decorators.py @@ -95,6 +95,11 @@ def in_channel_check(*channels: int, bypass_roles: typing.Container[int] = None) f"and the command was used in an overridden whitelisted channel." ) return True + + log.debug( + f"{ctx.author} tried to call the '{ctx.command.name}' command. " + f"The overridden in_channel check failed." + ) channels_str = ', '.join(f"<#{c_id}>" for c_id in override) raise InChannelCheckFailure( f"Sorry, but you may only use this command within {channels_str}." -- cgit v1.2.3 From 0e01bf0fb84fc91f691cd4f721180c4be0a5dd70 Mon Sep 17 00:00:00 2001 From: Numerlor Date: Wed, 2 Oct 2019 14:04:19 +0200 Subject: fix grammar in in_channel_override docstring --- bot/decorators.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/decorators.py b/bot/decorators.py index 471ea3be..2c042b56 100644 --- a/bot/decorators.py +++ b/bot/decorators.py @@ -67,7 +67,8 @@ def in_channel_check(*channels: int, bypass_roles: typing.Container[int] = None) """ Checks that the message is in a whitelisted channel or optionally has a bypass role. - If `in_channel_override` is present, check if it contains channels and use them in place of global whitelist + If `in_channel_override` is present, check if it contains channels + and use them in place of the global whitelist. """ def predicate(ctx: Context) -> bool: if not ctx.guild: -- cgit v1.2.3