From 15324b3428e80d2e7a37ba84443983684e834ffc Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Wed, 30 Sep 2020 21:29:11 +0200 Subject: Update the Hacktoberfest channel constant I've updated the Hacktoberfest channel ID to the ID of the new channel just created for the 2020 edition of the event. --- bot/exts/halloween/hacktoberstats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index db5e37f2..92429c1b 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -18,7 +18,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,) +HACKTOBER_WHITELIST = WHITELISTED_CHANNELS + (Channels.hacktoberfest_2020,) class HacktoberStats(commands.Cog): -- cgit v1.2.3 From 45c204ada302e4c5f7f9866ff26b607391d94ff3 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Wed, 30 Sep 2020 22:28:03 +0100 Subject: Allow hacktoberfest commands in September and November --- bot/exts/halloween/hacktoberstats.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 92429c1b..42754f4b 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -29,7 +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() - @in_month(Month.OCTOBER) + @in_month(Month.SEPTEMBER, Month.OCTOBER, Month.NOVEMBER) @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: @@ -57,7 +57,7 @@ class HacktoberStats(commands.Cog): await self.get_stats(ctx, github_username) - @in_month(Month.OCTOBER) + @in_month(Month.SEPTEMBER, Month.OCTOBER, Month.NOVEMBER) @hacktoberstats_group.command(name="link") @override_in_channel(HACKTOBER_WHITELIST) async def link_user(self, ctx: commands.Context, github_username: str = None) -> None: @@ -92,7 +92,7 @@ class HacktoberStats(commands.Cog): logging.info(f"{author_id} tried to link a GitHub account but didn't provide a username") await ctx.send(f"{author_mention}, a GitHub username is required to link your account") - @in_month(Month.OCTOBER) + @in_month(Month.SEPTEMBER, Month.OCTOBER, Month.NOVEMBER) @hacktoberstats_group.command(name="unlink") @override_in_channel(HACKTOBER_WHITELIST) async def unlink_user(self, ctx: commands.Context) -> None: -- cgit v1.2.3 From a279a11e85a46b4731dc97d177415fbe35e8384f Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Wed, 30 Sep 2020 22:28:26 +0100 Subject: Update Hacktoberfest image URL --- bot/exts/halloween/hacktoberstats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 42754f4b..08a83a55 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -196,7 +196,7 @@ class HacktoberStats(commands.Cog): stats_embed.set_author( name="Hacktoberfest", url="https://hacktoberfest.digitalocean.com", - icon_url="https://hacktoberfest.digitalocean.com/pretty_logo.png" + icon_url="https://avatars1.githubusercontent.com/u/35706162?s=200&v=4" ) stats_embed.add_field( name="Top 5 Repositories:", -- cgit v1.2.3 From 047902d599bb87b7227a259fe63fa3a3f34b1d71 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Wed, 30 Sep 2020 22:34:22 +0100 Subject: Update t-shirt references to t-shirts and trees! --- bot/exts/halloween/hacktoberstats.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 08a83a55..9dd0e1a4 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -175,11 +175,11 @@ class HacktoberStats(commands.Cog): n = pr_stats['n_prs'] if n >= PRS_FOR_SHIRT: - shirtstr = f"**{github_username} has earned a tshirt!**" + shirtstr = f"**{github_username} has earned a T-shirt or a tree!**" elif n == PRS_FOR_SHIRT - 1: - shirtstr = f"**{github_username} is 1 PR away from a tshirt!**" + shirtstr = f"**{github_username} is 1 PR away from a T-shirt or a tree!**" else: - shirtstr = f"**{github_username} is {PRS_FOR_SHIRT - n} PRs away from a tshirt!**" + shirtstr = f"**{github_username} is {PRS_FOR_SHIRT - n} PRs away from a T-shirt or a tree!**" stats_embed = discord.Embed( title=f"{github_username}'s Hacktoberfest", -- cgit v1.2.3 From ba12301b69aa347b705ed61e8250d948b514d23b Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 30 Sep 2020 23:33:03 -0400 Subject: Authenticate GitHub API requests for Hacktoberfest stats. Also changed the user agent from `Discord Python Hacktoberbot` to `Python Discord Hacktoberbot`. --- bot/exts/halloween/hacktoberstats.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 9dd0e1a4..3347dd8f 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -10,7 +10,7 @@ import aiohttp import discord from discord.ext import commands -from bot.constants import Channels, Month, WHITELISTED_CHANNELS +from bot.constants import Channels, Month, Tokens, WHITELISTED_CHANNELS from bot.utils.decorators import in_month, override_in_channel from bot.utils.persist import make_persistent @@ -20,6 +20,10 @@ 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_2020,) +REQUEST_HEADERS = {"User-Agent": "Python Discord Hacktoberbot"} +if GITHUB_TOKEN := Tokens.github: + REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}" + class HacktoberStats(commands.Cog): """Hacktoberfest statistics Cog.""" @@ -242,9 +246,8 @@ class HacktoberStats(commands.Cog): f"&per_page={per_page}" ) - headers = {"user-agent": "Discord Python Hacktoberbot"} async with aiohttp.ClientSession() as session: - async with session.get(query_url, headers=headers) as resp: + async with session.get(query_url, headers=REQUEST_HEADERS) as resp: jsonresp = await resp.json() if "message" in jsonresp.keys(): -- cgit v1.2.3 From b0fe81808b32650e7dd6a7a8a86d5fc36c27c2fe Mon Sep 17 00:00:00 2001 From: Den4200 Date: Wed, 30 Sep 2020 23:34:45 -0400 Subject: Check the GitHub user exists before searching for their PRs. Ensures an error does not occur when a GitHub user does not exist. --- bot/exts/halloween/hacktoberstats.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 3347dd8f..a2261caf 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -247,6 +247,11 @@ class HacktoberStats(commands.Cog): ) async with aiohttp.ClientSession() as session: + async with session.get(f"https://api.github.com/users/{github_username}", headers=REQUEST_HEADERS) as resp: + if resp.status == 404: + logging.debug(f"No GitHub user found named '{github_username}'") + return + async with session.get(query_url, headers=REQUEST_HEADERS) as resp: jsonresp = await resp.json() -- cgit v1.2.3 From deee8cfebc69e63ddc0060bc888e7f00b90efe6e Mon Sep 17 00:00:00 2001 From: Den4200 Date: Thu, 1 Oct 2020 00:15:14 -0400 Subject: Removed unnecessary GitHub API request and checked response message instead. This check is to see if a GitHub user is non-existent. We do not want to log this as an error. --- bot/exts/halloween/hacktoberstats.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index a2261caf..ed1755e3 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -24,6 +24,11 @@ REQUEST_HEADERS = {"User-Agent": "Python Discord Hacktoberbot"} if GITHUB_TOKEN := Tokens.github: REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}" +GITHUB_NONEXISTENT_USER_MESSAGE = ( + "The listed users cannot be searched either because the users do not exist " + "or you do not have permission to view the users." +) + class HacktoberStats(commands.Cog): """Hacktoberfest statistics Cog.""" @@ -247,19 +252,21 @@ class HacktoberStats(commands.Cog): ) async with aiohttp.ClientSession() as session: - async with session.get(f"https://api.github.com/users/{github_username}", headers=REQUEST_HEADERS) as resp: - if resp.status == 404: - logging.debug(f"No GitHub user found named '{github_username}'") - return - async with session.get(query_url, headers=REQUEST_HEADERS) as resp: jsonresp = await resp.json() if "message" in jsonresp.keys(): # One of the parameters is invalid, short circuit for now api_message = jsonresp["errors"][0]["message"] - logging.error(f"GitHub API request for '{github_username}' failed with message: {api_message}") + + # Ignore logging non-existent users or users we do not have permission to see + if api_message == GITHUB_NONEXISTENT_USER_MESSAGE: + logging.debug(f"No GitHub user found named '{github_username}'") + else: + logging.error(f"GitHub API request for '{github_username}' failed with message: {api_message}") + return + else: if jsonresp["total_count"] == 0: # Short circuit if there aren't any PRs -- cgit v1.2.3