From c0c8db9810906e993c7b68b8731ff1d52a3990ea Mon Sep 17 00:00:00 2001 From: DAzVise Date: Fri, 15 Nov 2019 15:01:55 +0300 Subject: [PATCH 1/4] added fallback category --- bot.py | 2 +- core/config.py | 1 + core/config_help.json | 10 ++++++++++ core/thread.py | 20 ++++++++++++++++++-- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/bot.py b/bot.py index 5e578cac7d..89ac13bb1a 100644 --- a/bot.py +++ b/bot.py @@ -738,7 +738,7 @@ async def process_dm_modmail(self, message: discord.Message) -> None: await self.add_reaction(message, blocked_emoji) return await message.channel.send(embed=embed) - thread = self.threads.create(message.author) + thread = await self.threads.create(message.author) else: if self.config["dm_disabled"] == 2: embed = discord.Embed( diff --git a/core/config.py b/core/config.py index 695b743f56..a0eda4a2dc 100644 --- a/core/config.py +++ b/core/config.py @@ -27,6 +27,7 @@ class ConfigManager: "twitch_url": "https://www.twitch.tv/discordmodmail/", # bot settings "main_category_id": None, + "fallback_category_id": None, "prefix": "?", "mention": "@here", "main_color": str(discord.Color.blurple()), diff --git a/core/config_help.json b/core/config_help.json index c6c0fc95d0..177e488094 100644 --- a/core/config_help.json +++ b/core/config_help.json @@ -20,6 +20,16 @@ "If the Modmail category ended up being non-existent/invalid, Modmail will break. To fix this, run `{prefix}setup` again or set `main_category_id` to a valid category." ] }, + "fallback_category_id": { + "default": "`Fallback Modmail` (created when the main category is full)", + "description": "This is the category that will hold the threads when the main category is full.\n\nTo change the Fallback category, you will need to find the [category’s ID](https://support.discordapp.com/hc/en-us/articles/206346498).", + "examples": [ + "`{prefix}config set fallback_category_id 9234932582312` (`9234932582312` is the category ID)" + ], + "notes": [ + "If the Fallback category ended up being non-existent/invalid, Modmail will create a new one. To fix this, set `fallback_category_id` to a valid category." + ] + }, "prefix": { "default": "`?`", "description": "The prefix of the bot.", diff --git a/core/thread.py b/core/thread.py index bf13030776..b6dac99eaf 100644 --- a/core/thread.py +++ b/core/thread.py @@ -846,7 +846,7 @@ def _find_from_channel(self, channel): return thread return None - def create( + async def create( self, recipient: typing.Union[discord.Member, discord.User], *, @@ -859,11 +859,27 @@ def create( self.cache[recipient.id] = thread # Schedule thread setup for later + cat = self.bot.main_category + if len(cat.channels) == 50: + fallback_id = self.bot.config["fallback_category_id"] + fallback = discord.utils.get(cat.guild.categories, id=int(fallback_id)) + if fallback and len(fallback.channels) != 50: + self.bot.loop.create_task(thread.setup(creator=creator, category=fallback)) + return thread + + fallback = await cat.clone(name="Fallback Modmail") + self.bot.config.set("fallback_category_id", fallback.id) + await self.bot.config.update() + self.bot.loop.create_task(thread.setup(creator=creator, category=fallback)) + return thread + + self.bot.loop.create_task(thread.setup(creator=creator, category=category)) + return thread self.bot.loop.create_task(thread.setup(creator=creator, category=category)) return thread async def find_or_create(self, recipient) -> Thread: - return await self.find(recipient=recipient) or self.create(recipient) + return await self.find(recipient=recipient) or await self.create(recipient) def format_channel_name(self, author): """Sanitises a username for use with text channel names""" From 9e0e6cac4df4a0e85f68500c5e449340ee50575d Mon Sep 17 00:00:00 2001 From: DAzVise <52792999+DAzVise@users.noreply.github.com> Date: Fri, 15 Nov 2019 21:03:54 +0300 Subject: [PATCH 2/4] whoops --- core/thread.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/thread.py b/core/thread.py index b6dac99eaf..0e6d43e12a 100644 --- a/core/thread.py +++ b/core/thread.py @@ -875,8 +875,6 @@ async def create( self.bot.loop.create_task(thread.setup(creator=creator, category=category)) return thread - self.bot.loop.create_task(thread.setup(creator=creator, category=category)) - return thread async def find_or_create(self, recipient) -> Thread: return await self.find(recipient=recipient) or await self.create(recipient) From b41acc734430211fe79354886dbaa1d9f6c474dc Mon Sep 17 00:00:00 2001 From: DAzVise Date: Sat, 16 Nov 2019 11:19:57 +0300 Subject: [PATCH 3/4] black formatting --- bot.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bot.py b/bot.py index 89ac13bb1a..af313d2e85 100644 --- a/bot.py +++ b/bot.py @@ -639,10 +639,7 @@ async def is_blocked( blocked_reason = self.blocked_users.get(str(author.id)) or "" - if ( - not self.check_account_age(author) - or not self.check_guild_age(author) - ): + if not self.check_account_age(author) or not self.check_guild_age(author): new_reason = self.blocked_users.get(str(author.id)) if new_reason != blocked_reason: if send_message: From 48e801d688ff433199e8aa8b8553a3f0b2488d58 Mon Sep 17 00:00:00 2001 From: DAzVise Date: Tue, 19 Nov 2019 18:31:26 +0300 Subject: [PATCH 4/4] better logic --- cogs/modmail.py | 2 +- core/thread.py | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/cogs/modmail.py b/cogs/modmail.py index 6a2b3bbfe8..cee11f84bc 100644 --- a/cogs/modmail.py +++ b/cogs/modmail.py @@ -900,7 +900,7 @@ async def contact( await ctx.channel.send(embed=embed) else: - thread = self.bot.threads.create(user, creator=ctx.author, category=category) + thread = await self.bot.threads.create(user, creator=ctx.author, category=category) if self.bot.config["dm_disabled"] >= 1: logger.info("Contacting user %s when Modmail DM is disabled.", user) diff --git a/core/thread.py b/core/thread.py index 0e6d43e12a..83f7f5267b 100644 --- a/core/thread.py +++ b/core/thread.py @@ -97,7 +97,7 @@ async def setup(self, *, creator=None, category=None): overwrites=overwrites, reason="Creating a thread channel.", ) - except discord.HTTPException as e: # Failed to create due to 50 channel limit. + except discord.HTTPException as e: # Failed to create due to missing perms. logger.critical("An error occurred while creating a thread.", exc_info=True) self.manager.cache.pop(self.id) @@ -860,18 +860,17 @@ async def create( # Schedule thread setup for later cat = self.bot.main_category - if len(cat.channels) == 50: + if category is None and len(cat.channels) == 50: fallback_id = self.bot.config["fallback_category_id"] - fallback = discord.utils.get(cat.guild.categories, id=int(fallback_id)) - if fallback and len(fallback.channels) != 50: - self.bot.loop.create_task(thread.setup(creator=creator, category=fallback)) - return thread - - fallback = await cat.clone(name="Fallback Modmail") - self.bot.config.set("fallback_category_id", fallback.id) - await self.bot.config.update() - self.bot.loop.create_task(thread.setup(creator=creator, category=fallback)) - return thread + if fallback_id: + fallback = discord.utils.get(cat.guild.categories, id=int(fallback_id)) + if fallback and len(fallback.channels) != 50: + category = fallback + + if not category: + category = await cat.clone(name="Fallback Modmail") + self.bot.config.set("fallback_category_id", category.id) + await self.bot.config.update() self.bot.loop.create_task(thread.setup(creator=creator, category=category)) return thread