Skip to content

Commit a0d820b

Browse files
authored
Merge pull request #636 from DAzVise/development
added fallback category. #303
2 parents d283dac + 48e801d commit a0d820b

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

bot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ async def process_dm_modmail(self, message: discord.Message) -> None:
736736
await self.add_reaction(message, blocked_emoji)
737737
return await message.channel.send(embed=embed)
738738

739-
thread = self.threads.create(message.author)
739+
thread = await self.threads.create(message.author)
740740
else:
741741
if self.config["dm_disabled"] == 2:
742742
embed = discord.Embed(

cogs/modmail.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ async def contact(
900900
await ctx.channel.send(embed=embed)
901901

902902
else:
903-
thread = self.bot.threads.create(user, creator=ctx.author, category=category)
903+
thread = await self.bot.threads.create(user, creator=ctx.author, category=category)
904904
if self.bot.config["dm_disabled"] >= 1:
905905
logger.info("Contacting user %s when Modmail DM is disabled.", user)
906906

core/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ConfigManager:
2727
"twitch_url": "https://www.twitch.tv/discordmodmail/",
2828
# bot settings
2929
"main_category_id": None,
30+
"fallback_category_id": None,
3031
"prefix": "?",
3132
"mention": "@here",
3233
"main_color": str(discord.Color.blurple()),

core/config_help.json

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
"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."
2121
]
2222
},
23+
"fallback_category_id": {
24+
"default": "`Fallback Modmail` (created when the main category is full)",
25+
"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).",
26+
"examples": [
27+
"`{prefix}config set fallback_category_id 9234932582312` (`9234932582312` is the category ID)"
28+
],
29+
"notes": [
30+
"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."
31+
]
32+
},
2333
"prefix": {
2434
"default": "`?`",
2535
"description": "The prefix of the bot.",

core/thread.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ async def setup(self, *, creator=None, category=None):
9797
overwrites=overwrites,
9898
reason="Creating a thread channel.",
9999
)
100-
except discord.HTTPException as e: # Failed to create due to 50 channel limit.
100+
except discord.HTTPException as e: # Failed to create due to missing perms.
101101
logger.critical("An error occurred while creating a thread.", exc_info=True)
102102
self.manager.cache.pop(self.id)
103103

@@ -846,7 +846,7 @@ def _find_from_channel(self, channel):
846846
return thread
847847
return None
848848

849-
def create(
849+
async def create(
850850
self,
851851
recipient: typing.Union[discord.Member, discord.User],
852852
*,
@@ -859,11 +859,24 @@ def create(
859859
self.cache[recipient.id] = thread
860860

861861
# Schedule thread setup for later
862+
cat = self.bot.main_category
863+
if category is None and len(cat.channels) == 50:
864+
fallback_id = self.bot.config["fallback_category_id"]
865+
if fallback_id:
866+
fallback = discord.utils.get(cat.guild.categories, id=int(fallback_id))
867+
if fallback and len(fallback.channels) != 50:
868+
category = fallback
869+
870+
if not category:
871+
category = await cat.clone(name="Fallback Modmail")
872+
self.bot.config.set("fallback_category_id", category.id)
873+
await self.bot.config.update()
874+
862875
self.bot.loop.create_task(thread.setup(creator=creator, category=category))
863876
return thread
864877

865878
async def find_or_create(self, recipient) -> Thread:
866-
return await self.find(recipient=recipient) or self.create(recipient)
879+
return await self.find(recipient=recipient) or await self.create(recipient)
867880

868881
def format_channel_name(self, author):
869882
"""Sanitises a username for use with text channel names"""

0 commit comments

Comments
 (0)