From 0d5da9e1304865034e8a2349d33b132e149ad890 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 5 Feb 2021 19:17:59 +0000 Subject: First pass of easy to produce errors --- bot/exts/easter/easter_riddle.py | 13 ++++++++++++- bot/exts/evergreen/issues.py | 7 ++++++- bot/exts/evergreen/wolfram.py | 3 ++- bot/exts/halloween/candy_collection.py | 3 +++ 4 files changed, 23 insertions(+), 3 deletions(-) (limited to 'bot') diff --git a/bot/exts/easter/easter_riddle.py b/bot/exts/easter/easter_riddle.py index 3c612eb1..a93b3745 100644 --- a/bot/exts/easter/easter_riddle.py +++ b/bot/exts/easter/easter_riddle.py @@ -7,7 +7,7 @@ from pathlib import Path import discord from discord.ext import commands -from bot.constants import Colours +from bot.constants import Colours, NEGATIVE_REPLIES log = logging.getLogger(__name__) @@ -36,6 +36,17 @@ class EasterRiddle(commands.Cog): if self.current_channel: return await ctx.send(f"A riddle is already being solved in {self.current_channel.mention}!") + # Don't let users start in a DM + if not ctx.guild: + await ctx.send( + embed=discord.Embed( + title=random.choice(NEGATIVE_REPLIES), + description="You can't start riddles in DMs", + colour=discord.Colour.red() + ) + ) + return + self.current_channel = ctx.message.channel random_question = random.choice(RIDDLE_QUESTIONS) diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 73ebe547..1f22f287 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -178,7 +178,12 @@ class Issues(commands.Cog): @commands.Cog.listener() async def on_message(self, message: discord.Message) -> None: """Command to retrieve issue(s) from a GitHub repository using automatic linking if matching #.""" - if not( + # Ignore messages from DMs + if not message.guild: + return + + # Ignore messages not in whitelisted categories / channels + if not ( message.channel.category.id in WHITELISTED_CATEGORIES or message.channel.id in WHITELISTED_CHANNELS_ON_MESSAGE ): diff --git a/bot/exts/evergreen/wolfram.py b/bot/exts/evergreen/wolfram.py index 898e8d2a..077a99f5 100644 --- a/bot/exts/evergreen/wolfram.py +++ b/bot/exts/evergreen/wolfram.py @@ -62,7 +62,8 @@ def custom_cooldown(*ignore: List[int]) -> Callable: # if the invoked command is help we don't want to increase the ratelimits since it's not actually # invoking the command/making a request, so instead just check if the user/guild are on cooldown. guild_cooldown = not guildcd.get_bucket(ctx.message).get_tokens() == 0 # if guild is on cooldown - if not any(r.id in ignore for r in ctx.author.roles): # check user bucket if user is not ignored + # check the message is in a guild, and check user bucket if user is not ignored + if ctx.guild and not any(r.id in ignore for r in ctx.author.roles): return guild_cooldown and not usercd.get_bucket(ctx.message).get_tokens() == 0 return guild_cooldown diff --git a/bot/exts/halloween/candy_collection.py b/bot/exts/halloween/candy_collection.py index 0cb37ecd..40e21f40 100644 --- a/bot/exts/halloween/candy_collection.py +++ b/bot/exts/halloween/candy_collection.py @@ -47,6 +47,9 @@ class CandyCollection(commands.Cog): @commands.Cog.listener() async def on_message(self, message: discord.Message) -> None: """Randomly adds candy or skull reaction to non-bot messages in the Event channel.""" + # Ignore messages in DMs + if not message.guild: + return # make sure its a human message if message.author.bot: return -- cgit v1.2.3 From e3b7b3bc5b56b805112558377e25e2537cc86083 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 2 Apr 2021 14:53:27 +0100 Subject: Don't allow users to run the issue command in DMs, given error feedback. --- bot/exts/evergreen/issues.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 1f22f287..0f0be33a 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -7,7 +7,16 @@ from enum import Enum import discord from discord.ext import commands, tasks -from bot.constants import Categories, Channels, Colours, ERROR_REPLIES, Emojis, Tokens, WHITELISTED_CHANNELS +from bot.constants import ( + Categories, + Channels, + Colours, + ERROR_REPLIES, + Emojis, + NEGATIVE_REPLIES, + Tokens, + WHITELISTED_CHANNELS +) log = logging.getLogger(__name__) @@ -148,11 +157,20 @@ class Issues(commands.Cog): user: str = "python-discord" ) -> None: """Command to retrieve issue(s) from a GitHub repository.""" - if not( + if not ctx.guild or not( ctx.channel.category.id in WHITELISTED_CATEGORIES or ctx.channel.id in WHITELISTED_CHANNELS ): - return + await ctx.send( + embed=discord.Embed( + title=random.choice(NEGATIVE_REPLIES), + description=( + "You can't run this command in this channel. " + f"Try again in {Channels.community_bot_commands}" + ), + colour=discord.Colour.red() + ) + ) result = await self.fetch_issues(set(numbers), repository, user) -- cgit v1.2.3 From 6a8a582c49c2a3d97a2a061eca157502acb0618d Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 2 Apr 2021 17:02:47 +0100 Subject: Send error embed when a user tries to retrieve an issue in DMs --- bot/exts/evergreen/issues.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index ca0e4494..4a73d20b 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -168,11 +168,12 @@ class Issues(commands.Cog): title=random.choice(NEGATIVE_REPLIES), description=( "You can't run this command in this channel. " - f"Try again in {Channels.community_bot_commands}" + f"Try again in <#{Channels.community_bot_commands}>" ), colour=discord.Colour.red() ) ) + return result = await self.fetch_issues(set(numbers), repository, user) @@ -198,12 +199,8 @@ class Issues(commands.Cog): @commands.Cog.listener() async def on_message(self, message: discord.Message) -> None: """Command to retrieve issue(s) from a GitHub repository using automatic linking if matching #.""" - # Ignore messages from DMs - if not message.guild: - return - - # Ignore messages not in whitelisted categories / channels - if not ( + # Ignore messages not in whitelisted categories / channels, only when in guild. + if message.guild and not ( message.channel.category.id in WHITELISTED_CATEGORIES or message.channel.id in WHITELISTED_CHANNELS_ON_MESSAGE ): @@ -213,6 +210,18 @@ class Issues(commands.Cog): links = [] if message_repo_issue_map: + if not message.guild: + await message.channel.send( + embed=discord.Embed( + title=random.choice(NEGATIVE_REPLIES), + description=( + "You can't retreive issues from DMs. " + f"Try again in <#{Channels.community_bot_commands}>" + ), + colour=discord.Colour.red() + ) + ) + return for repo_issue in message_repo_issue_map: if not self.check_in_block(message, " ".join(repo_issue)): result = await self.fetch_issues({repo_issue[1]}, repo_issue[0], "python-discord") -- cgit v1.2.3 From 50cd34e73d77bf71fe214250d5f20a2b4d98664d Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Mon, 5 Apr 2021 15:01:58 +0100 Subject: fix: use get_user in 8bitify to avoid events issues --- bot/exts/evergreen/8bitify.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/8bitify.py b/bot/exts/evergreen/8bitify.py index 54e68f80..ee2b4034 100644 --- a/bot/exts/evergreen/8bitify.py +++ b/bot/exts/evergreen/8bitify.py @@ -25,7 +25,8 @@ class EightBitify(commands.Cog): async def eightbit_command(self, ctx: commands.Context) -> None: """Pixelates your avatar and changes the palette to an 8bit one.""" async with ctx.typing(): - image_bytes = await ctx.author.avatar_url.read() + author = await self.bot.get_user(ctx.author.id) + image_bytes = await author.avatar_url.read() avatar = Image.open(BytesIO(image_bytes)) avatar = avatar.convert("RGBA").resize((1024, 1024)) -- cgit v1.2.3 From 30756d4dcc2dcdba334cc3493d82eccdc9c6aded Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Mon, 5 Apr 2021 15:45:10 +0100 Subject: fix: use api fetch not cache get --- bot/exts/evergreen/8bitify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/8bitify.py b/bot/exts/evergreen/8bitify.py index ee2b4034..7eb4d313 100644 --- a/bot/exts/evergreen/8bitify.py +++ b/bot/exts/evergreen/8bitify.py @@ -25,7 +25,7 @@ class EightBitify(commands.Cog): async def eightbit_command(self, ctx: commands.Context) -> None: """Pixelates your avatar and changes the palette to an 8bit one.""" async with ctx.typing(): - author = await self.bot.get_user(ctx.author.id) + author = await self.bot.fetch_user(ctx.author.id) image_bytes = await author.avatar_url.read() avatar = Image.open(BytesIO(image_bytes)) avatar = avatar.convert("RGBA").resize((1024, 1024)) -- cgit v1.2.3 From 22ba746b3892857d1b2050eab7c1c72cd435ef48 Mon Sep 17 00:00:00 2001 From: onerandomusername <71233171+onerandomusername@users.noreply.github.com> Date: Thu, 8 Apr 2021 13:01:36 -0400 Subject: Remove topic that doesn't make sense in discord Removed the `Name one thing you like about a person to your right.` topic as it doesn't make sense in discord --- bot/resources/evergreen/starter.yaml | 1 - 1 file changed, 1 deletion(-) (limited to 'bot') diff --git a/bot/resources/evergreen/starter.yaml b/bot/resources/evergreen/starter.yaml index 4fec6a10..6b0de0ef 100644 --- a/bot/resources/evergreen/starter.yaml +++ b/bot/resources/evergreen/starter.yaml @@ -6,7 +6,6 @@ - "What is better: Milk, Dark or White chocolate?" - What is your favourite holiday? - If you could have any superpower, what would it be? -- Name one thing you like about a person to your right. - If you could be anyone else for one day, who would it be? - What Easter tradition do you enjoy most? - What is the best gift you've been given? -- cgit v1.2.3 From 0e9ea17010310c7d63d9b0fbba9e3a441cf2c1b5 Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Sat, 10 Apr 2021 05:56:22 +0100 Subject: add timed command for timed execution of commands --- bot/exts/evergreen/timed.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 bot/exts/evergreen/timed.py (limited to 'bot') diff --git a/bot/exts/evergreen/timed.py b/bot/exts/evergreen/timed.py new file mode 100644 index 00000000..0537141b --- /dev/null +++ b/bot/exts/evergreen/timed.py @@ -0,0 +1,36 @@ +from copy import copy +from time import perf_counter + +from discord import Message +from discord.ext import commands + + +class TimedCommands(commands.Cog): + """Time the command execution of a command.""" + + def __init__(self, bot: commands.Bot) -> None: + self.bot = bot + + @staticmethod + async def create_execution_context(ctx: commands.Context, command: str) -> commands.Context: + """Get a new execution context for a command.""" + msg: Message = copy(ctx.message) + msg._update({"content": f"{ctx.prefix}{command}"}) + + return await ctx.bot.get_context(msg) + + @commands.command(name="timed", aliases=["t"]) + async def timed(self, ctx: commands.Context, *, command: str) -> None: + """Time the command execution of a command.""" + new_ctx = await self.create_execution_context(ctx, command) + + t_start = perf_counter() + await new_ctx.command.invoke(new_ctx) + t_end = perf_counter() + + await ctx.send(f"Command execution for `{new_ctx.command}` finished in {(t_end - t_start):.4f} seconds.") + + +def setup(bot: commands.Bot) -> None: + """Cog load.""" + bot.add_cog(TimedCommands(bot)) -- cgit v1.2.3 From d7263238b68a3d55f47ec944cc9e85450e2800b6 Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Sat, 10 Apr 2021 21:48:26 +0100 Subject: removed unused cog init and fixed timing timed command --- bot/exts/evergreen/timed.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/timed.py b/bot/exts/evergreen/timed.py index 0537141b..58da3dc8 100644 --- a/bot/exts/evergreen/timed.py +++ b/bot/exts/evergreen/timed.py @@ -8,9 +8,6 @@ from discord.ext import commands class TimedCommands(commands.Cog): """Time the command execution of a command.""" - def __init__(self, bot: commands.Bot) -> None: - self.bot = bot - @staticmethod async def create_execution_context(ctx: commands.Context, command: str) -> commands.Context: """Get a new execution context for a command.""" @@ -24,6 +21,9 @@ class TimedCommands(commands.Cog): """Time the command execution of a command.""" new_ctx = await self.create_execution_context(ctx, command) + if new_ctx.command and new_ctx.command.qualified_name == "timed": + return await ctx.send("You are not allowed to time the execution of the `timed` command.") + t_start = perf_counter() await new_ctx.command.invoke(new_ctx) t_end = perf_counter() -- cgit v1.2.3 From bf1cd68efaa7f0fe5037c69fbdc7cfffcf284a2d Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Sun, 11 Apr 2021 01:11:21 +0100 Subject: fix message updating and return types --- bot/exts/evergreen/timed.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/timed.py b/bot/exts/evergreen/timed.py index 58da3dc8..57575993 100644 --- a/bot/exts/evergreen/timed.py +++ b/bot/exts/evergreen/timed.py @@ -12,7 +12,7 @@ class TimedCommands(commands.Cog): async def create_execution_context(ctx: commands.Context, command: str) -> commands.Context: """Get a new execution context for a command.""" msg: Message = copy(ctx.message) - msg._update({"content": f"{ctx.prefix}{command}"}) + msg.content = f"{ctx.prefix}{command}" return await ctx.bot.get_context(msg) @@ -22,7 +22,8 @@ class TimedCommands(commands.Cog): new_ctx = await self.create_execution_context(ctx, command) if new_ctx.command and new_ctx.command.qualified_name == "timed": - return await ctx.send("You are not allowed to time the execution of the `timed` command.") + await ctx.send("You are not allowed to time the execution of the `timed` command.") + return t_start = perf_counter() await new_ctx.command.invoke(new_ctx) -- cgit v1.2.3 From b0892e5afbbb13167c7c295c8f8241a2146eddf7 Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Sun, 11 Apr 2021 04:03:59 +0100 Subject: return and send message if command isnt found while timing --- bot/exts/evergreen/timed.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/timed.py b/bot/exts/evergreen/timed.py index 57575993..a0f29838 100644 --- a/bot/exts/evergreen/timed.py +++ b/bot/exts/evergreen/timed.py @@ -21,7 +21,11 @@ class TimedCommands(commands.Cog): """Time the command execution of a command.""" new_ctx = await self.create_execution_context(ctx, command) - if new_ctx.command and new_ctx.command.qualified_name == "timed": + if not new_ctx.command: + await ctx.send("The command you are trying to time doesn't exist. Use `.help` for a list of commands.") + return + + if new_ctx.command.qualified_name == "timed": await ctx.send("You are not allowed to time the execution of the `timed` command.") return -- cgit v1.2.3 From 7c7c6dd2318815bc14e1cb20d5dd5fbd416712a0 Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Sun, 11 Apr 2021 04:19:58 +0100 Subject: fix: use ctx.prefix when suggesting the help command --- bot/exts/evergreen/timed.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/timed.py b/bot/exts/evergreen/timed.py index a0f29838..604204b0 100644 --- a/bot/exts/evergreen/timed.py +++ b/bot/exts/evergreen/timed.py @@ -22,7 +22,10 @@ class TimedCommands(commands.Cog): new_ctx = await self.create_execution_context(ctx, command) if not new_ctx.command: - await ctx.send("The command you are trying to time doesn't exist. Use `.help` for a list of commands.") + help_command = f"{ctx.prefix}help" + error = f"The command you are trying to time doesn't exist. Use `{help_command}` for a list of commands." + + await ctx.send(error) return if new_ctx.command.qualified_name == "timed": -- cgit v1.2.3 From 17cf2b391df6d02083386a450c0f4569ba421762 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 11 Apr 2021 16:21:44 +0100 Subject: Add env var for trashcan eomji override --- 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 416dd0e7..1d35b3f1 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -153,7 +153,7 @@ class Emojis: christmas_tree = "\U0001F384" check = "\u2611" envelope = "\U0001F4E8" - trashcan = "<:trashcan:637136429717389331>" + trashcan = environ.get("TRASHCAN_EMOJI", "<:trashcan:637136429717389331>") ok_hand = ":ok_hand:" hand_raised = "\U0001f64b" -- cgit v1.2.3 From 652e306064efb451660e7c08dc88aeafdcf7360e Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Sun, 11 Apr 2021 19:12:10 +0100 Subject: chore: add time as alias of timed Co-authored-by: Matteo Bertucci --- bot/exts/evergreen/timed.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/timed.py b/bot/exts/evergreen/timed.py index 604204b0..635ccb32 100644 --- a/bot/exts/evergreen/timed.py +++ b/bot/exts/evergreen/timed.py @@ -16,7 +16,7 @@ class TimedCommands(commands.Cog): return await ctx.bot.get_context(msg) - @commands.command(name="timed", aliases=["t"]) + @commands.command(name="timed", aliases=["time", "t"]) async def timed(self, ctx: commands.Context, *, command: str) -> None: """Time the command execution of a command.""" new_ctx = await self.create_execution_context(ctx, command) -- cgit v1.2.3