Skip to content

Commit a314eab

Browse files
committed
Populate thread cache on bot ready
1 parent 39d2e59 commit a314eab

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

bot.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ async def on_ready(self):
175175
{Fore.YELLOW}Guild ID: {self.guild.id if self.guild else 0}
176176
{line}
177177
''').strip())
178+
179+
await self.threads.populate_cache()
178180

179181
async def on_message(self, message):
180182
if message.author.bot:
@@ -734,9 +736,11 @@ async def edit(self, ctx, message_id: int, *, new_message):
734736
async def contact(self, ctx, *, user: discord.Member):
735737
'''Create a thread with a specified member.'''
736738

737-
# TODO: HANDLE WHEN A THREAD ALREADY EXISTS
738-
739-
thread = await self.threads.create(user, creator=ctx.author)
739+
exists = await self.threads.find(recipient=user)
740+
if exists:
741+
return await ctx.send('Thread already exists.')
742+
else:
743+
thread = await self.threads.create(user, creator=ctx.author)
740744

741745
em = discord.Embed(
742746
title='Created thread',

utils/modmail.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ def __init__(self, manager, recipient):
1313
self.channel = None
1414
self.ready_event = asyncio.Event()
1515

16+
def __repr__(self):
17+
return f'Thread(recipient="{self.recipient}", channel={self.channel.id})'
18+
1619
def wait_until_ready(self):
1720
'''Blocks execution until the thread is fully set up.'''
1821
return self.ready_event.wait()
@@ -121,6 +124,10 @@ def __iter__(self):
121124
def __getitem__(self, item):
122125
return self.cache[item]
123126

127+
async def populate_cache(self):
128+
for channel in self.bot.guild.text_channels:
129+
await self.find(channel=channel)
130+
124131
async def find(self, *, recipient=None, channel=None):
125132
'''Finds a thread from cache or from discord channel topics.'''
126133
if recipient is None and channel is not None:
@@ -151,7 +158,7 @@ async def _find_from_channel(self, channel):
151158

152159
if channel.topic and 'User ID: ' in channel.topic:
153160
user_id = int(re.findall(r'\d+', channel.topic)[0])
154-
elif channel.topic is None:
161+
elif channel.topic is None and channel.category.name == 'Mod Mail':
155162
async for message in channel.history():
156163
if message.embeds:
157164
em = message.embeds[0]
@@ -204,7 +211,7 @@ async def create(self, recipient, *, creator=None):
204211
info_embed = self.bot.format_info(recipient, creator, log_url, log_count)
205212

206213
topic = f'User ID: {recipient.id}'
207-
mention = self.bot.config.get('MENTION', '@here')
214+
mention = self.bot.config.get('MENTION', '@here') if not creator else None
208215

209216
await asyncio.gather(
210217
channel.edit(topic=topic),

0 commit comments

Comments
 (0)