From 4b871609190ce5ecb9eb47c5e1f7e3a42effd989 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Sat, 13 Jun 2020 23:06:37 +0800 Subject: Add initial caesarcipher command --- bot/exts/evergreen/fun.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 67a4bae5..5f4c5b3e 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -87,6 +87,40 @@ class Fun(Cog): converted_text = f">>> {converted_text.lstrip('> ')}" await ctx.send(content=converted_text, embed=embed) + @commands.command(name="caesarcipher", aliases=("caesar",)) + async def caesarcipher_command(self, ctx: Context, offset: int, *, text: str) -> None: + """ + Given an integer `offset`, encrypt the given `text`. + + A positive `offset` will cause the letters to shift right, + while a negative `offset` will cause the letters to shift left. + + Also accepts a valid discord Message ID or link. + """ + + def cipher_func(text: str) -> str: + """Implements a lazy Caesar cipher algorithm.""" + for char in text: + if not char.isascii() or not char.isalpha() or char.isspace(): + yield char + continue + case_start = 65 if char.isupper() else 97 + yield chr((ord(char) - case_start + offset) % 26 + case_start) + + def conversion_func(text: str) -> str: + """Encrypts the given string using the Caesar cipher.""" + return "".join(cipher_func(text)) + + text, embed = await Fun._get_text_and_embed(ctx, text) + # Convert embed if it exists + if embed is not None: + embed = Fun._convert_embed(conversion_func, embed) + converted_text = conversion_func(text) + # Don't put >>> if only embed present + if converted_text: + converted_text = f">>> {converted_text.lstrip('> ')}" + await ctx.send(content=converted_text, embed=embed) + @staticmethod async def _get_text_and_embed(ctx: Context, text: str) -> Tuple[str, Union[Embed, None]]: """ -- cgit v1.2.3 From 3e58b35174ea4aa686f1347c41ecd056df74f6ee Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Sun, 14 Jun 2020 11:20:59 +0800 Subject: Make caesarcipher a command group --- bot/exts/evergreen/fun.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 5f4c5b3e..2b1abaa4 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -87,19 +87,32 @@ class Fun(Cog): converted_text = f">>> {converted_text.lstrip('> ')}" await ctx.send(content=converted_text, embed=embed) - @commands.command(name="caesarcipher", aliases=("caesar",)) - async def caesarcipher_command(self, ctx: Context, offset: int, *, text: str) -> None: + @commands.group(name="caesarcipher", aliases=("caesar",)) + async def caesarcipher_group(self, ctx: Context) -> None: """ - Given an integer `offset`, encrypt the given `text`. + Translates a message using the Caesar Cipher. + + See `info` and `translate` subcommands. + """ + if ctx.invoked_subcommand is None: + await self.bot.get_cog("Help").new_help(ctx, "caesarcipher") + + @caesarcipher_group.command(name="info") + async def caesarcipher_info(self, ctx: Context) -> None: + """Information about the Caesar Cipher.""" + + @caesarcipher_group.command(name="translate") + async def caesarcipher_translate(self, ctx: Context, offset: int, *, text: str) -> None: + """ + Given an integer `offset`, translate the given `text`. A positive `offset` will cause the letters to shift right, while a negative `offset` will cause the letters to shift left. Also accepts a valid discord Message ID or link. """ - def cipher_func(text: str) -> str: - """Implements a lazy Caesar cipher algorithm.""" + """Implements a lazy Caesar Cipher algorithm.""" for char in text: if not char.isascii() or not char.isalpha() or char.isspace(): yield char @@ -108,7 +121,7 @@ class Fun(Cog): yield chr((ord(char) - case_start + offset) % 26 + case_start) def conversion_func(text: str) -> str: - """Encrypts the given string using the Caesar cipher.""" + """Encrypts the given string using the Caesar Cipher.""" return "".join(cipher_func(text)) text, embed = await Fun._get_text_and_embed(ctx, text) -- cgit v1.2.3 From baa20503e716a9ed9ab8be5f82c3456c7b4cfbb5 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Sun, 14 Jun 2020 14:20:09 +0800 Subject: Add basic caesar cipher information embed --- bot/exts/evergreen/fun.py | 13 ++++++++++++- bot/resources/evergreen/caesar_info.json | 4 ++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 bot/resources/evergreen/caesar_info.json (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 2b1abaa4..f3e3bfcc 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -1,4 +1,5 @@ import functools +import json import logging import random from typing import Callable, Tuple, Union @@ -8,7 +9,7 @@ from discord.ext import commands from discord.ext.commands import Bot, Cog, Context, MessageConverter from bot import utils -from bot.constants import Emojis +from bot.constants import Colours, Emojis log = logging.getLogger(__name__) @@ -100,6 +101,16 @@ class Fun(Cog): @caesarcipher_group.command(name="info") async def caesarcipher_info(self, ctx: Context) -> None: """Information about the Caesar Cipher.""" + with open("bot\\resources\\evergreen\\caesar_info.json", "r") as f: + data = json.load(f) + + embed = Embed( + title=data["title"], + description="".join(data["description"]), + colour=Colours.dark_green, + ) + + await ctx.send(embed=embed) @caesarcipher_group.command(name="translate") async def caesarcipher_translate(self, ctx: Context, offset: int, *, text: str) -> None: diff --git a/bot/resources/evergreen/caesar_info.json b/bot/resources/evergreen/caesar_info.json new file mode 100644 index 00000000..ae3a4656 --- /dev/null +++ b/bot/resources/evergreen/caesar_info.json @@ -0,0 +1,4 @@ +{ + "title": "Caesar Cipher", + "description": "**Information**\nThe Caesar Cipher, named after hte Roman General Julius Caesar is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter given a specific position offset in the alphabet, with the letters wrapping around both sides.\n\n**Examples**\n1) `Hello World` <=> `Khoor Zruog` where letters are shifted forwards by `3.`\n2) `Julius Caesar` <=> `Yjaxjh Rpthpg` where letters are shifted backwards by `11.`" +} -- cgit v1.2.3 From 3260a16d78351fec2bab9ba6124e4a655fbd2e26 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Thu, 25 Jun 2020 23:29:06 +0800 Subject: Refactor translate subcommand to encrypt and decrypt --- bot/exts/evergreen/fun.py | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index f3e3bfcc..75b0e2b0 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -93,7 +93,7 @@ class Fun(Cog): """ Translates a message using the Caesar Cipher. - See `info` and `translate` subcommands. + See `decrpyt`, `encrypt`, and `info` subcommands. """ if ctx.invoked_subcommand is None: await self.bot.get_cog("Help").new_help(ctx, "caesarcipher") @@ -112,17 +112,17 @@ class Fun(Cog): await ctx.send(embed=embed) - @caesarcipher_group.command(name="translate") - async def caesarcipher_translate(self, ctx: Context, offset: int, *, text: str) -> None: + @staticmethod + async def _caesar_cipher(ctx: Context, offset: int, text: str) -> None: """ - Given an integer `offset`, translate the given `text`. + Given an integer `offset`, translates and sends the given `text`. - A positive `offset` will cause the letters to shift right, - while a negative `offset` will cause the letters to shift left. + A positive `offset` will cause the letters to shift right, while + a negative `offset` will cause the letters to shift left. - Also accepts a valid discord Message ID or link. + Also accepts a valid Discord Message ID or link. """ - def cipher_func(text: str) -> str: + def caesar_func(text: str) -> str: """Implements a lazy Caesar Cipher algorithm.""" for char in text: if not char.isascii() or not char.isalpha() or char.isspace(): @@ -133,7 +133,7 @@ class Fun(Cog): def conversion_func(text: str) -> str: """Encrypts the given string using the Caesar Cipher.""" - return "".join(cipher_func(text)) + return "".join(caesar_func(text)) text, embed = await Fun._get_text_and_embed(ctx, text) # Convert embed if it exists @@ -145,6 +145,28 @@ class Fun(Cog): converted_text = f">>> {converted_text.lstrip('> ')}" await ctx.send(content=converted_text, embed=embed) + @caesarcipher_group.command(name="encrypt", aliases=("rightshift", "rshift")) + async def caesarcipher_encrypt(self, ctx: Context, offset: int, *, text: str) -> None: + """ + Given a positive integer `offset`, encrypt the given `text`. + + Performs a right shift of the letters in the message. + + Also accepts a valid Discord Message ID or link. + """ + await self._caesar_cipher(ctx, offset, text) + + @caesarcipher_group.command(name="decrypt", aliases=("leftshift", "lshift")) + async def caesarcipher_decrypt(self, ctx: Context, offset: int, *, text: str) -> None: + """ + Given a positive integer `offset`, decrypt the given `text`. + + Performs a left shift of the letters in the message. + + Also accepts a valid Discord Message ID or link. + """ + await self._caesar_cipher(ctx, -offset, text) + @staticmethod async def _get_text_and_embed(ctx: Context, text: str) -> Tuple[str, Union[Embed, None]]: """ -- cgit v1.2.3 From f1a79fb65a98c4c483b3e77dfb1c910c8702165b Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Thu, 25 Jun 2020 23:47:30 +0800 Subject: Add offset validation --- bot/exts/evergreen/fun.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 75b0e2b0..364ce74b 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -154,7 +154,10 @@ class Fun(Cog): Also accepts a valid Discord Message ID or link. """ - await self._caesar_cipher(ctx, offset, text) + if offset < 0: + await ctx.send(":no_entry: Cannot use a negative offset.") + else: + await self._caesar_cipher(ctx, offset, text) @caesarcipher_group.command(name="decrypt", aliases=("leftshift", "lshift")) async def caesarcipher_decrypt(self, ctx: Context, offset: int, *, text: str) -> None: @@ -165,7 +168,10 @@ class Fun(Cog): Also accepts a valid Discord Message ID or link. """ - await self._caesar_cipher(ctx, -offset, text) + if offset < 0: + await ctx.send(":no_entry: Cannot use a negative offset.") + else: + await self._caesar_cipher(ctx, -offset, text) @staticmethod async def _get_text_and_embed(ctx: Context, text: str) -> Tuple[str, Union[Embed, None]]: -- cgit v1.2.3 From d6539562aef0ae4be88ddb023f5ff49206a90cf0 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Fri, 26 Jun 2020 12:11:00 +0800 Subject: Load embed data in __init__ and use Embed.from_dict --- bot/exts/evergreen/fun.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 364ce74b..4fc06b49 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -33,6 +33,9 @@ class Fun(Cog): def __init__(self, bot: Bot) -> None: self.bot = bot + with open("bot\\resources\\evergreen\\caesar_info.json", "r") as f: + self._caesar_cipher_embed = json.load(f) + @commands.command() async def roll(self, ctx: Context, num_rolls: int = 1) -> None: """Outputs a number of random dice emotes (up to 6).""" @@ -101,14 +104,8 @@ class Fun(Cog): @caesarcipher_group.command(name="info") async def caesarcipher_info(self, ctx: Context) -> None: """Information about the Caesar Cipher.""" - with open("bot\\resources\\evergreen\\caesar_info.json", "r") as f: - data = json.load(f) - - embed = Embed( - title=data["title"], - description="".join(data["description"]), - colour=Colours.dark_green, - ) + embed = Embed.from_dict(self._caesar_cipher_embed) + embed.colour = Colours.dark_green await ctx.send(embed=embed) -- cgit v1.2.3 From 3fe9a22fc9cf57d284d17949563596e05ea5d033 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Fri, 26 Jun 2020 12:21:46 +0800 Subject: Use Context.invoke instead of invoking the Help cog --- bot/exts/evergreen/fun.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 4fc06b49..090714aa 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -99,7 +99,7 @@ class Fun(Cog): See `decrpyt`, `encrypt`, and `info` subcommands. """ if ctx.invoked_subcommand is None: - await self.bot.get_cog("Help").new_help(ctx, "caesarcipher") + await ctx.invoke(self.bot.get_command("help"), "caesarcipher") @caesarcipher_group.command(name="info") async def caesarcipher_info(self, ctx: Context) -> None: -- cgit v1.2.3 From 3b27b96374488f2c2c34f51e718fc42fa5b7c1a6 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Fri, 26 Jun 2020 14:50:44 +0800 Subject: Utilize Union[Message, str] for converting messages --- bot/exts/evergreen/fun.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 090714aa..d1f87b6a 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -110,7 +110,7 @@ class Fun(Cog): await ctx.send(embed=embed) @staticmethod - async def _caesar_cipher(ctx: Context, offset: int, text: str) -> None: + async def _caesar_cipher(ctx: Context, offset: int, msg: Union[Message, str]) -> None: """ Given an integer `offset`, translates and sends the given `text`. @@ -132,18 +132,23 @@ class Fun(Cog): """Encrypts the given string using the Caesar Cipher.""" return "".join(caesar_func(text)) - text, embed = await Fun._get_text_and_embed(ctx, text) - # Convert embed if it exists + is_message = isinstance(msg, Message) + + text = msg.content if is_message else msg + embed = msg.embeds[0] if is_message and msg.embeds else None + if embed is not None: embed = Fun._convert_embed(conversion_func, embed) + converted_text = conversion_func(text) - # Don't put >>> if only embed present + if converted_text: converted_text = f">>> {converted_text.lstrip('> ')}" + await ctx.send(content=converted_text, embed=embed) @caesarcipher_group.command(name="encrypt", aliases=("rightshift", "rshift")) - async def caesarcipher_encrypt(self, ctx: Context, offset: int, *, text: str) -> None: + async def caesarcipher_encrypt(self, ctx: Context, offset: int, *, msg: Union[Message, str]) -> None: """ Given a positive integer `offset`, encrypt the given `text`. @@ -154,10 +159,10 @@ class Fun(Cog): if offset < 0: await ctx.send(":no_entry: Cannot use a negative offset.") else: - await self._caesar_cipher(ctx, offset, text) + await self._caesar_cipher(ctx, offset, msg) @caesarcipher_group.command(name="decrypt", aliases=("leftshift", "lshift")) - async def caesarcipher_decrypt(self, ctx: Context, offset: int, *, text: str) -> None: + async def caesarcipher_decrypt(self, ctx: Context, offset: int, *, msg: Union[Message, str]) -> None: """ Given a positive integer `offset`, decrypt the given `text`. @@ -168,7 +173,7 @@ class Fun(Cog): if offset < 0: await ctx.send(":no_entry: Cannot use a negative offset.") else: - await self._caesar_cipher(ctx, -offset, text) + await self._caesar_cipher(ctx, -offset, msg) @staticmethod async def _get_text_and_embed(ctx: Context, text: str) -> Tuple[str, Union[Embed, None]]: -- cgit v1.2.3 From 093a54c9acd546435c517497af409d55e0512412 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Sat, 27 Jun 2020 15:17:18 +0800 Subject: Use pathlib and fix grammar in the information embed --- bot/exts/evergreen/fun.py | 3 ++- bot/resources/evergreen/caesar_info.json | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index d1f87b6a..601ae45b 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -2,6 +2,7 @@ import functools import json import logging import random +from pathlib import Path from typing import Callable, Tuple, Union from discord import Embed, Message @@ -33,7 +34,7 @@ class Fun(Cog): def __init__(self, bot: Bot) -> None: self.bot = bot - with open("bot\\resources\\evergreen\\caesar_info.json", "r") as f: + with Path("bot/resources/evergreen/caesar_info.json").open("r") as f: self._caesar_cipher_embed = json.load(f) @commands.command() diff --git a/bot/resources/evergreen/caesar_info.json b/bot/resources/evergreen/caesar_info.json index ae3a4656..8229c4f3 100644 --- a/bot/resources/evergreen/caesar_info.json +++ b/bot/resources/evergreen/caesar_info.json @@ -1,4 +1,4 @@ { "title": "Caesar Cipher", - "description": "**Information**\nThe Caesar Cipher, named after hte Roman General Julius Caesar is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter given a specific position offset in the alphabet, with the letters wrapping around both sides.\n\n**Examples**\n1) `Hello World` <=> `Khoor Zruog` where letters are shifted forwards by `3.`\n2) `Julius Caesar` <=> `Yjaxjh Rpthpg` where letters are shifted backwards by `11.`" + "description": "**Information**\nThe Caesar Cipher, named after the Roman General Julius Caesar, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter given a specific position offset in the alphabet, with the letters wrapping around both sides.\n\n**Examples**\n1) `Hello World` <=> `Khoor Zruog` where letters are shifted forwards by `3`.\n2) `Julius Caesar` <=> `Yjaxjh Rpthpg` where letters are shifted backwards by `11`." } -- cgit v1.2.3 From c59a1630817503ed8507a0b364e037077645af35 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Sat, 27 Jun 2020 15:29:03 +0800 Subject: Add UTF-8 encoding when loading embed information --- bot/exts/evergreen/fun.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 601ae45b..f9bd8812 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -34,7 +34,7 @@ class Fun(Cog): def __init__(self, bot: Bot) -> None: self.bot = bot - with Path("bot/resources/evergreen/caesar_info.json").open("r") as f: + with Path("bot/resources/evergreen/caesar_info.json").open("r", encoding="UTF-8") as f: self._caesar_cipher_embed = json.load(f) @commands.command() -- cgit v1.2.3 From e8a94313f93f12b52c76cebf4f425206475b89c1 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Sun, 28 Jun 2020 13:51:01 +0800 Subject: Add 'cc' alias for caesarcipher --- bot/exts/evergreen/fun.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index f9bd8812..0abc0138 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -92,7 +92,7 @@ class Fun(Cog): converted_text = f">>> {converted_text.lstrip('> ')}" await ctx.send(content=converted_text, embed=embed) - @commands.group(name="caesarcipher", aliases=("caesar",)) + @commands.group(name="caesarcipher", aliases=("caesar", "cc",)) async def caesarcipher_group(self, ctx: Context) -> None: """ Translates a message using the Caesar Cipher. -- cgit v1.2.3 From 1d9ee64755736b715896e90c9b359b490f08e547 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Sun, 28 Jun 2020 13:53:54 +0800 Subject: Fix return type annotation --- bot/exts/evergreen/fun.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 0abc0138..d376c44e 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -3,7 +3,7 @@ import json import logging import random from pathlib import Path -from typing import Callable, Tuple, Union +from typing import Callable, Iterable, Tuple, Union from discord import Embed, Message from discord.ext import commands @@ -120,7 +120,7 @@ class Fun(Cog): Also accepts a valid Discord Message ID or link. """ - def caesar_func(text: str) -> str: + def caesar_func(text: str) -> Iterable[str]: """Implements a lazy Caesar Cipher algorithm.""" for char in text: if not char.isascii() or not char.isalpha() or char.isspace(): -- cgit v1.2.3 From b02b4e84caddb595d39b87571de6181f7446c528 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Tue, 30 Jun 2020 20:21:50 +0800 Subject: Change text to msg in command docstrings --- bot/exts/evergreen/fun.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index d376c44e..220881d4 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -113,7 +113,7 @@ class Fun(Cog): @staticmethod async def _caesar_cipher(ctx: Context, offset: int, msg: Union[Message, str]) -> None: """ - Given an integer `offset`, translates and sends the given `text`. + Given an integer `offset`, translates and sends the given `msg`. A positive `offset` will cause the letters to shift right, while a negative `offset` will cause the letters to shift left. @@ -151,7 +151,7 @@ class Fun(Cog): @caesarcipher_group.command(name="encrypt", aliases=("rightshift", "rshift")) async def caesarcipher_encrypt(self, ctx: Context, offset: int, *, msg: Union[Message, str]) -> None: """ - Given a positive integer `offset`, encrypt the given `text`. + Given a positive integer `offset`, encrypt the given `msg`. Performs a right shift of the letters in the message. @@ -165,7 +165,7 @@ class Fun(Cog): @caesarcipher_group.command(name="decrypt", aliases=("leftshift", "lshift")) async def caesarcipher_decrypt(self, ctx: Context, offset: int, *, msg: Union[Message, str]) -> None: """ - Given a positive integer `offset`, decrypt the given `text`. + Given a positive integer `offset`, decrypt the given `msg`. Performs a left shift of the letters in the message. -- cgit v1.2.3 From 52f99c69f882fdc253de594002d404a5b454a971 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Thu, 2 Jul 2020 19:21:56 +0800 Subject: Separate the formula for the actual offset --- bot/exts/evergreen/fun.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 220881d4..5f5c3718 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -126,8 +126,11 @@ class Fun(Cog): if not char.isascii() or not char.isalpha() or char.isspace(): yield char continue + case_start = 65 if char.isupper() else 97 - yield chr((ord(char) - case_start + offset) % 26 + case_start) + true_offset = (ord(char) - case_start + offset) % 26 + + yield chr(case_start + true_offset) def conversion_func(text: str) -> str: """Encrypts the given string using the Caesar Cipher.""" -- cgit v1.2.3 From c7f6489453ccad4ec10bd316a641461ee8ca4c94 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Thu, 2 Jul 2020 19:31:44 +0800 Subject: Add 'enc'/'dec' aliases for encrypt/decrypt --- bot/exts/evergreen/fun.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 5f5c3718..31dba936 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -151,7 +151,7 @@ class Fun(Cog): await ctx.send(content=converted_text, embed=embed) - @caesarcipher_group.command(name="encrypt", aliases=("rightshift", "rshift")) + @caesarcipher_group.command(name="encrypt", aliases=("rightshift", "rshift", "enc",)) async def caesarcipher_encrypt(self, ctx: Context, offset: int, *, msg: Union[Message, str]) -> None: """ Given a positive integer `offset`, encrypt the given `msg`. @@ -165,7 +165,7 @@ class Fun(Cog): else: await self._caesar_cipher(ctx, offset, msg) - @caesarcipher_group.command(name="decrypt", aliases=("leftshift", "lshift")) + @caesarcipher_group.command(name="decrypt", aliases=("leftshift", "lshift", "dec",)) async def caesarcipher_decrypt(self, ctx: Context, offset: int, *, msg: Union[Message, str]) -> None: """ Given a positive integer `offset`, decrypt the given `msg`. -- cgit v1.2.3 From 411db10969387fbb8e89f87c78a39dc580cc6498 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Wed, 15 Jul 2020 15:03:13 +0800 Subject: Refactor translation methods to avoid repetition --- bot/exts/evergreen/fun.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 31dba936..759a9e2a 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -111,15 +111,21 @@ class Fun(Cog): await ctx.send(embed=embed) @staticmethod - async def _caesar_cipher(ctx: Context, offset: int, msg: Union[Message, str]) -> None: + async def _caesar_cipher(ctx: Context, offset: int, msg: Union[Message, str], left_shift: bool = False) -> None: """ - Given an integer `offset`, translates and sends the given `msg`. + Given a positive integer `offset`, translates and sends the given `msg`. - A positive `offset` will cause the letters to shift right, while - a negative `offset` will cause the letters to shift left. + Performs a right shift by default unless `left_shift` is specified as `True`. Also accepts a valid Discord Message ID or link. """ + if offset < 0: + await ctx.send(":no_entry: Cannot use a negative offset.") + return + + if left_shift: + offset = -offset + def caesar_func(text: str) -> Iterable[str]: """Implements a lazy Caesar Cipher algorithm.""" for char in text: @@ -160,10 +166,7 @@ class Fun(Cog): Also accepts a valid Discord Message ID or link. """ - if offset < 0: - await ctx.send(":no_entry: Cannot use a negative offset.") - else: - await self._caesar_cipher(ctx, offset, msg) + await self._caesar_cipher(ctx, offset, msg, False) @caesarcipher_group.command(name="decrypt", aliases=("leftshift", "lshift", "dec",)) async def caesarcipher_decrypt(self, ctx: Context, offset: int, *, msg: Union[Message, str]) -> None: @@ -174,10 +177,7 @@ class Fun(Cog): Also accepts a valid Discord Message ID or link. """ - if offset < 0: - await ctx.send(":no_entry: Cannot use a negative offset.") - else: - await self._caesar_cipher(ctx, -offset, msg) + await self._caesar_cipher(ctx, offset, msg, True) @staticmethod async def _get_text_and_embed(ctx: Context, text: str) -> Tuple[str, Union[Embed, None]]: -- cgit v1.2.3 From 86387796ca671a47d7f7a7740b63980e6d0346b6 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Wed, 29 Jul 2020 12:37:16 +0800 Subject: Explicitly pass left_shift as a keyword argument --- bot/exts/evergreen/fun.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 759a9e2a..5a499270 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -166,7 +166,7 @@ class Fun(Cog): Also accepts a valid Discord Message ID or link. """ - await self._caesar_cipher(ctx, offset, msg, False) + await self._caesar_cipher(ctx, offset, msg, left_shift=False) @caesarcipher_group.command(name="decrypt", aliases=("leftshift", "lshift", "dec",)) async def caesarcipher_decrypt(self, ctx: Context, offset: int, *, msg: Union[Message, str]) -> None: @@ -177,7 +177,7 @@ class Fun(Cog): Also accepts a valid Discord Message ID or link. """ - await self._caesar_cipher(ctx, offset, msg, True) + await self._caesar_cipher(ctx, offset, msg, left_shift=True) @staticmethod async def _get_text_and_embed(ctx: Context, text: str) -> Tuple[str, Union[Embed, None]]: -- cgit v1.2.3 From 98718712ab38ecb98970ade58fd1afd6796f8718 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Fri, 18 Sep 2020 20:30:56 +0800 Subject: Make the encryption function top-level This makes caesar_func a top-level function and renames it to caesar_cipher. --- bot/exts/evergreen/fun.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 5a499270..3fd0f275 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -28,6 +28,26 @@ UWU_WORDS = { } +def caesar_cipher(text: str, offset: int) -> Iterable[str]: + """ + Implements a lazy Caesar Cipher algorithm. + + Encrypts a `text` given a specific integer `offset`. The sign + of the `offset` dictates the direction in which it shifts to, + with a negative value shifting to the left, and a positive + value shifting to the right. + """ + for char in text: + if not char.isascii() or not char.isalpha() or char.isspace(): + yield char + continue + + case_start = 65 if char.isupper() else 97 + true_offset = (ord(char) - case_start + offset) % 26 + + yield chr(case_start + true_offset) + + class Fun(Cog): """A collection of general commands for fun.""" @@ -126,21 +146,9 @@ class Fun(Cog): if left_shift: offset = -offset - def caesar_func(text: str) -> Iterable[str]: - """Implements a lazy Caesar Cipher algorithm.""" - for char in text: - if not char.isascii() or not char.isalpha() or char.isspace(): - yield char - continue - - case_start = 65 if char.isupper() else 97 - true_offset = (ord(char) - case_start + offset) % 26 - - yield chr(case_start + true_offset) - def conversion_func(text: str) -> str: """Encrypts the given string using the Caesar Cipher.""" - return "".join(caesar_func(text)) + return "".join(caesar_cipher(text, offset)) is_message = isinstance(msg, Message) -- cgit v1.2.3 From d6b331080d57fb122eb63644382717f2fc4860c7 Mon Sep 17 00:00:00 2001 From: PureFunctor Date: Fri, 18 Sep 2020 20:39:38 +0800 Subject: Refactor methods to use _get_text_and_embed This changes the converters used by caesarcipher_encrypt and caesarcipher_decrypt in order to accomodate for the manual conversion that _get_text_and_embed does, which allows for this feature to be easily disabled. --- bot/exts/evergreen/fun.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 3fd0f275..8235d072 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -131,7 +131,7 @@ class Fun(Cog): await ctx.send(embed=embed) @staticmethod - async def _caesar_cipher(ctx: Context, offset: int, msg: Union[Message, str], left_shift: bool = False) -> None: + async def _caesar_cipher(ctx: Context, offset: int, msg: str, left_shift: bool = False) -> None: """ Given a positive integer `offset`, translates and sends the given `msg`. @@ -150,10 +150,7 @@ class Fun(Cog): """Encrypts the given string using the Caesar Cipher.""" return "".join(caesar_cipher(text, offset)) - is_message = isinstance(msg, Message) - - text = msg.content if is_message else msg - embed = msg.embeds[0] if is_message and msg.embeds else None + text, embed = await Fun._get_text_and_embed(ctx, msg) if embed is not None: embed = Fun._convert_embed(conversion_func, embed) @@ -166,7 +163,7 @@ class Fun(Cog): await ctx.send(content=converted_text, embed=embed) @caesarcipher_group.command(name="encrypt", aliases=("rightshift", "rshift", "enc",)) - async def caesarcipher_encrypt(self, ctx: Context, offset: int, *, msg: Union[Message, str]) -> None: + async def caesarcipher_encrypt(self, ctx: Context, offset: int, *, msg: str) -> None: """ Given a positive integer `offset`, encrypt the given `msg`. @@ -177,7 +174,7 @@ class Fun(Cog): await self._caesar_cipher(ctx, offset, msg, left_shift=False) @caesarcipher_group.command(name="decrypt", aliases=("leftshift", "lshift", "dec",)) - async def caesarcipher_decrypt(self, ctx: Context, offset: int, *, msg: Union[Message, str]) -> None: + async def caesarcipher_decrypt(self, ctx: Context, offset: int, *, msg: str) -> None: """ Given a positive integer `offset`, decrypt the given `msg`. -- cgit v1.2.3 From 0567a6fe099465b2a36df7818113fadc8ff1c7d7 Mon Sep 17 00:00:00 2001 From: Kieran Siek Date: Fri, 18 Sep 2020 23:00:23 +0800 Subject: Fix typo in casercipher help. --- bot/exts/evergreen/fun.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 8235d072..c5f8f9c8 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -117,7 +117,7 @@ class Fun(Cog): """ Translates a message using the Caesar Cipher. - See `decrpyt`, `encrypt`, and `info` subcommands. + See `decrypt`, `encrypt`, and `info` subcommands. """ if ctx.invoked_subcommand is None: await ctx.invoke(self.bot.get_command("help"), "caesarcipher") -- cgit v1.2.3 From dc63f50d2445c628806e9ac4f08d5ece6c72b18a Mon Sep 17 00:00:00 2001 From: kosayoda Date: Fri, 18 Sep 2020 23:33:56 +0800 Subject: Stop users from viewing messages they shouldn't. Using a user token, a user could fetch the message ID of a message in any channel, which may leak information when potential Message objects are automatically converted and parsed. Now, the bot will only retrive text from a valid Message object if the user has read permissions for the message the channel is in. --- bot/exts/evergreen/fun.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 2f575c1c..e6cdf716 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -181,18 +181,22 @@ class Fun(Cog): """ Attempts to extract the text and embed from a possible link to a discord Message. + Does not retrieve the text and embed from the Message if it is in a channel the user does + not have read permissions in. + Returns a tuple of: str: If `text` is a valid discord Message, the contents of the message, else `text`. Union[Embed, None]: The embed if found in the valid Message, else None """ embed = None - # message = await Fun._get_discord_message(ctx, text) - # if isinstance(message, Message): - # text = message.content - # # Take first embed because we can't send multiple embeds - # if message.embeds: - # embed = message.embeds[0] + msg = await Fun._get_discord_message(ctx, text) + # Ensure the user has read permissions for the channel the message is in + if isinstance(msg, Message) and ctx.author.permissions_in(msg.channel).read_messages: + text = msg.content + # Take first embed because we can't send multiple embeds + if msg.embeds: + embed = msg.embeds[0] return (text, embed) -- cgit v1.2.3 From a13f5737cdd947ac297f7af790d0c614aab0474a Mon Sep 17 00:00:00 2001 From: Xithrius Date: Sat, 19 Sep 2020 14:52:54 -0700 Subject: Updated issue/pr command to get multiple links. --- bot/exts/evergreen/issues.py | 84 ++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 38 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 0f83731b..58ffe408 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -23,51 +23,59 @@ class Issues(commands.Cog): @commands.command(aliases=("pr",)) @override_in_channel(WHITELISTED_CHANNELS + (Channels.dev_contrib,)) async def issue( - self, ctx: commands.Context, number: int, repository: str = "seasonalbot", user: str = "python-discord" + self, ctx: commands.Context, numbers: commands.Greedy[int], repository: str = "seasonalbot", + user: str = "python-discord" ) -> None: - """Command to retrieve issues from a GitHub repository.""" - url = f"https://api.github.com/repos/{user}/{repository}/issues/{number}" - merge_url = f"https://api.github.com/repos/{user}/{repository}/pulls/{number}/merge" - - log.trace(f"Querying GH issues API: {url}") - async with self.bot.http_session.get(url) as r: - json_data = await r.json() - - if r.status in BAD_RESPONSE: - log.warning(f"Received response {r.status} from: {url}") - return await ctx.send(f"[{str(r.status)}] {BAD_RESPONSE.get(r.status)}") - - # The initial API request is made to the issues API endpoint, which will return information - # if the issue or PR is present. However, the scope of information returned for PRs differs - # from issues: if the 'issues' key is present in the response then we can pull the data we - # need from the initial API call. - if "issues" in json_data.get("html_url"): - if json_data.get("state") == "open": - icon_url = Emojis.issue - else: - icon_url = Emojis.issue_closed - - # If the 'issues' key is not contained in the API response and there is no error code, then - # we know that a PR has been requested and a call to the pulls API endpoint is necessary - # to get the desired information for the PR. - else: - log.trace(f"PR provided, querying GH pulls API for additional information: {merge_url}") - async with self.bot.http_session.get(merge_url) as m: + """Command to retrieve issue(s) from a GitHub repository.""" + links = [] + + for number in set(numbers): + # Convert from list to set to remove duplicates, if any. + url = f"https://api.github.com/repos/{user}/{repository}/issues/{number}" + merge_url = f"https://api.github.com/repos/{user}/{repository}/pulls/{number}/merge" + + log.trace(f"Querying GH issues API: {url}") + async with self.bot.http_session.get(url) as r: + json_data = await r.json() + + if r.status in BAD_RESPONSE: + log.warning(f"Received response {r.status} from: {url}") + return await ctx.send(f"[{str(r.status)}] #{number} {BAD_RESPONSE.get(r.status)}") + + # The initial API request is made to the issues API endpoint, which will return information + # if the issue or PR is present. However, the scope of information returned for PRs differs + # from issues: if the 'issues' key is present in the response then we can pull the data we + # need from the initial API call. + if "issues" in json_data.get("html_url"): if json_data.get("state") == "open": - icon_url = Emojis.pull_request - # When the status is 204 this means that the state of the PR is merged - elif m.status == 204: - icon_url = Emojis.merge + icon_url = Emojis.issue else: - icon_url = Emojis.pull_request_closed + icon_url = Emojis.issue_closed - issue_url = json_data.get("html_url") - description_text = f"[{repository}] #{number} {json_data.get('title')}" + # If the 'issues' key is not contained in the API response and there is no error code, then + # we know that a PR has been requested and a call to the pulls API endpoint is necessary + # to get the desired information for the PR. + else: + log.trace(f"PR provided, querying GH pulls API for additional information: {merge_url}") + async with self.bot.http_session.get(merge_url) as m: + if json_data.get("state") == "open": + icon_url = Emojis.pull_request + # When the status is 204 this means that the state of the PR is merged + elif m.status == 204: + icon_url = Emojis.merge + else: + icon_url = Emojis.pull_request_closed + + issue_url = json_data.get("html_url") + links.append([icon_url, f"[{repository}] #{number} {json_data.get('title')}", issue_url]) + + description_list = ['{0} [{1}]({2})'.format(*link) for link in links] resp = discord.Embed( colour=Colours.bright_green, - description=f"{icon_url} [{description_text}]({issue_url})" + description='\n'.join(description_list) ) - resp.set_author(name="GitHub", url=issue_url) + + resp.set_author(name="GitHub", url=f"https://github.com/python-discord/{repository}") await ctx.send(embed=resp) -- cgit v1.2.3 From 7a29cce2d06303965a89b789b352d4a5d9637f28 Mon Sep 17 00:00:00 2001 From: Xithrius Date: Sat, 19 Sep 2020 15:06:24 -0700 Subject: Replaced single quotes with double, set the repo link to be user-defined --- bot/exts/evergreen/issues.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 58ffe408..2f086f59 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -69,13 +69,13 @@ class Issues(commands.Cog): issue_url = json_data.get("html_url") links.append([icon_url, f"[{repository}] #{number} {json_data.get('title')}", issue_url]) - description_list = ['{0} [{1}]({2})'.format(*link) for link in links] + description_list = ["{0} [{1}]({2})".format(*link) for link in links] resp = discord.Embed( colour=Colours.bright_green, description='\n'.join(description_list) ) - resp.set_author(name="GitHub", url=f"https://github.com/python-discord/{repository}") + resp.set_author(name="GitHub", url=f"https://github.com/{user}/{repository}") await ctx.send(embed=resp) -- cgit v1.2.3 From 733a634784d848fd3b6a1c575aa7d3a0f8be44c9 Mon Sep 17 00:00:00 2001 From: Xithrius Date: Sat, 19 Sep 2020 15:08:11 -0700 Subject: Described how links are shown in the embed. --- bot/exts/evergreen/issues.py | 1 + 1 file changed, 1 insertion(+) (limited to 'bot') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 2f086f59..d909ae6e 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -69,6 +69,7 @@ class Issues(commands.Cog): issue_url = json_data.get("html_url") links.append([icon_url, f"[{repository}] #{number} {json_data.get('title')}", issue_url]) + # Issue/PR format: emoji to show if open/closed/merged, number and the title as a singular link. description_list = ["{0} [{1}]({2})".format(*link) for link in links] resp = discord.Embed( colour=Colours.bright_green, -- cgit v1.2.3 From 24b788d11bec461718b540d5d9fec2785afa5d23 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Sat, 19 Sep 2020 20:31:26 -0400 Subject: Allow `issue` command in #dev-branding --- bot/constants.py | 1 + bot/exts/evergreen/issues.py | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/constants.py b/bot/constants.py index 6605882d..fa428a61 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -52,6 +52,7 @@ class Channels(NamedTuple): devalerts = 460181980097675264 devlog = int(environ.get("CHANNEL_DEVLOG", 622895325144940554)) dev_contrib = 635950537262759947 + dev_branding = 753252897059373066 help_0 = 303906576991780866 help_1 = 303906556754395136 help_2 = 303906514266226689 diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index d909ae6e..97baac6a 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -21,9 +21,12 @@ class Issues(commands.Cog): self.bot = bot @commands.command(aliases=("pr",)) - @override_in_channel(WHITELISTED_CHANNELS + (Channels.dev_contrib,)) + @override_in_channel(WHITELISTED_CHANNELS + (Channels.dev_contrib, Channels.dev_branding)) async def issue( - self, ctx: commands.Context, numbers: commands.Greedy[int], repository: str = "seasonalbot", + self, + ctx: commands.Context, + numbers: commands.Greedy[int], + repository: str = "seasonalbot", user: str = "python-discord" ) -> None: """Command to retrieve issue(s) from a GitHub repository.""" -- cgit v1.2.3 From 91ca04e823eff28907f4018b851bf78a1ffb085e Mon Sep 17 00:00:00 2001 From: Den4200 Date: Sat, 19 Sep 2020 20:36:54 -0400 Subject: Limit maximum PRs/issues to 10 per invocation --- bot/exts/evergreen/issues.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 97baac6a..2a9359b7 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -1,9 +1,10 @@ import logging +import random import discord from discord.ext import commands -from bot.constants import Channels, Colours, Emojis, WHITELISTED_CHANNELS +from bot.constants import Channels, Colours, Emojis, ERROR_REPLIES, WHITELISTED_CHANNELS from bot.utils.decorators import override_in_channel log = logging.getLogger(__name__) @@ -13,6 +14,8 @@ BAD_RESPONSE = { 403: "Rate limit has been hit! Please try again later!" } +MAX_REQUESTS = 10 + class Issues(commands.Cog): """Cog that allows users to retrieve issues from GitHub.""" @@ -31,6 +34,16 @@ class Issues(commands.Cog): ) -> None: """Command to retrieve issue(s) from a GitHub repository.""" links = [] + numbers = set(numbers) + + if len(numbers) > MAX_REQUESTS: + embed = discord.Embed( + title=random.choice(ERROR_REPLIES), + color=Colours.soft_red, + description=f"Too many issues/PRs! (maximum of {MAX_REQUESTS})" + ) + await ctx.send(embed=embed) + return for number in set(numbers): # Convert from list to set to remove duplicates, if any. -- cgit v1.2.3 From d777222ccfad97f17a44b1deb342e5e89bca9946 Mon Sep 17 00:00:00 2001 From: Den4200 Date: Sat, 19 Sep 2020 20:37:36 -0400 Subject: Invoke help command when no PRs/issues are given --- bot/exts/evergreen/issues.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'bot') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 2a9359b7..e128ae50 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -36,6 +36,10 @@ class Issues(commands.Cog): links = [] numbers = set(numbers) + if not numbers: + await ctx.invoke(self.bot.get_command('help'), 'issue') + return + if len(numbers) > MAX_REQUESTS: embed = discord.Embed( title=random.choice(ERROR_REPLIES), -- cgit v1.2.3 From 157cc184c075287eb24a765b15430f2530d44bae Mon Sep 17 00:00:00 2001 From: Den4200 Date: Sat, 19 Sep 2020 20:38:30 -0400 Subject: Authenticate with the GitHub API to allow for more requests --- bot/exts/evergreen/issues.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index e128ae50..15c97473 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -4,7 +4,7 @@ import random import discord from discord.ext import commands -from bot.constants import Channels, Colours, Emojis, ERROR_REPLIES, WHITELISTED_CHANNELS +from bot.constants import Channels, Colours, Emojis, ERROR_REPLIES, Tokens, WHITELISTED_CHANNELS from bot.utils.decorators import override_in_channel log = logging.getLogger(__name__) @@ -16,6 +16,10 @@ BAD_RESPONSE = { MAX_REQUESTS = 10 +REQUEST_HEADERS = dict() +if GITHUB_TOKEN := Tokens.github: + REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}" + class Issues(commands.Cog): """Cog that allows users to retrieve issues from GitHub.""" @@ -30,7 +34,7 @@ class Issues(commands.Cog): ctx: commands.Context, numbers: commands.Greedy[int], repository: str = "seasonalbot", - user: str = "python-discord" + user: str = "python-discord" ) -> None: """Command to retrieve issue(s) from a GitHub repository.""" links = [] @@ -55,7 +59,7 @@ class Issues(commands.Cog): merge_url = f"https://api.github.com/repos/{user}/{repository}/pulls/{number}/merge" log.trace(f"Querying GH issues API: {url}") - async with self.bot.http_session.get(url) as r: + async with self.bot.http_session.get(url, headers=REQUEST_HEADERS) as r: json_data = await r.json() if r.status in BAD_RESPONSE: -- cgit v1.2.3 From fc5837770a0a1b49986768455a36d82193fbaadd Mon Sep 17 00:00:00 2001 From: Den4200 Date: Sat, 19 Sep 2020 20:48:12 -0400 Subject: Fix order of imports --- bot/exts/evergreen/issues.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 15c97473..5a5c82e7 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -4,7 +4,7 @@ import random import discord from discord.ext import commands -from bot.constants import Channels, Colours, Emojis, ERROR_REPLIES, Tokens, WHITELISTED_CHANNELS +from bot.constants import Channels, Colours, ERROR_REPLIES, Emojis, Tokens, WHITELISTED_CHANNELS from bot.utils.decorators import override_in_channel log = logging.getLogger(__name__) -- cgit v1.2.3