aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Akarys42 <[email protected]>2020-02-09 17:03:35 +0100
committerGravatar Akarys42 <[email protected]>2020-02-09 17:03:35 +0100
commit032e1b80934194b85c43d67f3a26cf51b972696d (patch)
tree8ecd7629a9a550ee3b3369d99337c6244b67fb5e
parentMerge branch 'master' into eval-enhancements (diff)
Use actual functions instead of lambdas for bot.wait_for
The use of lambdas made the functions hard to test, this new format allows us to easily test those functions and document them.
-rw-r--r--bot/cogs/snekbox.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/bot/cogs/snekbox.py b/bot/cogs/snekbox.py
index 1688c0278..3fc8d9937 100644
--- a/bot/cogs/snekbox.py
+++ b/bot/cogs/snekbox.py
@@ -3,9 +3,11 @@ import datetime
import logging
import re
import textwrap
+from functools import partial
from signal import Signals
from typing import Optional, Tuple
+from discord import Message, Reaction, User
from discord.ext.commands import Cog, Context, command, guild_only
from bot.bot import Bot
@@ -232,13 +234,13 @@ class Snekbox(Cog):
try:
_, new_message = await self.bot.wait_for(
'message_edit',
- check=lambda o, n: n.id == ctx.message.id and o.content != n.content,
+ check=partial(predicate_eval_message_edit, ctx),
timeout=10
)
await ctx.message.add_reaction('🔁')
await self.bot.wait_for(
'reaction_add',
- check=lambda r, u: r.message.id == ctx.message.id and u.id == ctx.author.id and str(r) == '🔁',
+ check=partial(predicate_eval_emoji_reaction, ctx),
timeout=10
)
@@ -251,6 +253,16 @@ class Snekbox(Cog):
return
+def predicate_eval_message_edit(ctx: Context, old_msg: Message, new_msg: Message) -> bool:
+ """Return True if the edited message is the context message and the content was indeed modified."""
+ return new_msg.id == ctx.message.id and old_msg.content != new_msg.content
+
+
+def predicate_eval_emoji_reaction(ctx: Context, reaction: Reaction, user: User) -> bool:
+ """Return True if the reaction 🔁 was added by the context message author on this message."""
+ return reaction.message.id == ctx.message.id and user.id == ctx.author.id and str(reaction) == '🔁'
+
+
def setup(bot: Bot) -> None:
"""Load the Snekbox cog."""
bot.add_cog(Snekbox(bot))