From a7c23876f7099e69077a03dad1d86c32689719c0 Mon Sep 17 00:00:00 2001 From: fourjr <28086837+fourjr@users.noreply.github.com> Date: Tue, 10 Nov 2020 19:44:06 +0800 Subject: [PATCH] More elegant solution to dummymessage --- CHANGELOG.md | 2 +- bot.py | 2 +- cogs/modmail.py | 5 +---- cogs/utility.py | 2 +- core/models.py | 43 +++++++++++++++++++++++++++++++++++++++++++ core/thread.py | 7 ++----- 6 files changed, 49 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1dda1042e6..812d0ae0d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). This project mostly adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html); however, insignificant breaking changes do not guarantee a major version bump, see the reasoning [here](https://github.com/kyb3r/modmail/issues/319). If you're a plugins developer, note the "BREAKING" section. -# v3.7.0-dev10 +# v3.7.0-dev11 ### Added diff --git a/bot.py b/bot.py index b68e0121aa..00e478531b 100644 --- a/bot.py +++ b/bot.py @@ -1,4 +1,4 @@ -__version__ = "3.7.0-dev10" +__version__ = "3.7.0-dev11" import asyncio diff --git a/cogs/modmail.py b/cogs/modmail.py index 8c0323ae09..01bdb4d341 100644 --- a/cogs/modmail.py +++ b/cogs/modmail.py @@ -338,10 +338,7 @@ async def move(self, ctx, *, arguments): await thread.channel.send(f"{mention}, thread has been moved.") sent_emoji, _ = await self.bot.retrieve_emoji() - try: - await self.bot.add_reaction(ctx.message, sent_emoji) - except discord.NotFound: - pass + await self.bot.add_reaction(ctx.message, sent_emoji) async def send_scheduled_close_message(self, ctx, after, silent=False): human_delta = human_timedelta(after.dt) diff --git a/cogs/utility.py b/cogs/utility.py index 55f237ab3f..9216eb7f12 100644 --- a/cogs/utility.py +++ b/cogs/utility.py @@ -1707,7 +1707,7 @@ async def oauth_show(self, ctx): @commands.group(invoke_without_command=True) @checks.has_permissions(PermissionLevel.OWNER) async def autotrigger(self, ctx): - """Automatically trigger alias-like commands based on a certain keyword""" + """Automatically trigger alias-like commands based on a certain keyword in the user's inital message""" await ctx.send_help(ctx.command) @autotrigger.command(name="add") diff --git a/core/models.py b/core/models.py index 1cd3ec4a6a..a834b033ae 100644 --- a/core/models.py +++ b/core/models.py @@ -211,3 +211,46 @@ def check(c): raise commands.ChannelNotFound(argument) return result + + +class DummyMessage: + """ + A class mimicking the original :class:discord.Message + where all functions that require an actual message to exist + is replaced with a dummy function + """ + def __init__(self, message): + self._message = message + + def __getattr__(self, name: str): + return getattr(self._message, name) + + async def delete(self, *, delay=None): + return + + async def edit(self, **fields): + return + + async def add_reaction(self, emoji): + return + + async def remove_reaction(self, emoji): + return + + async def clear_reaction(self, emoji): + return + + async def clear_reactions(self): + return + + async def pin(self, *, reason=None): + return + + async def unpin(self, *, reason=None): + return + + async def publish(self): + return + + async def ack(self): + return diff --git a/core/thread.py b/core/thread.py index 944ab81387..eca0e19220 100644 --- a/core/thread.py +++ b/core/thread.py @@ -11,8 +11,7 @@ import discord from discord.ext.commands import MissingRequiredArgument, CommandError -from core import checks -from core.models import PermissionLevel, getLogger +from core.models import DummyMessage, getLogger from core.time import human_timedelta from core.utils import ( is_image_url, @@ -200,7 +199,7 @@ async def send_recipient_genesis_message(): await self.bot.add_reaction(msg, close_emoji) async def activate_auto_triggers(): - message = copy.copy(initial_message) + message = DummyMessage(copy.copy(initial_message)) if message: for keyword in list(self.bot.auto_triggers): if keyword in message.content: @@ -894,8 +893,6 @@ async def send( if delete_message and destination == self.channel: try: await message.delete() - except discord.NotFound: - pass except Exception as e: logger.warning("Cannot delete message: %s.", e)