Skip to content

Commit b4b7778

Browse files
committed
Add support for an optional second guild resolves #81
1 parent 2c09bc6 commit b4b7778

File tree

4 files changed

+49
-39
lines changed

4 files changed

+49
-39
lines changed

bot.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,23 @@ def guild_id(self):
121121
@property
122122
def guild(self):
123123
return discord.utils.get(self.guilds, id=self.guild_id)
124+
125+
@property
126+
def modmail_guild(self):
127+
modmail_guild_id = self.config.get('modmail_guild_id')
128+
if not modmail_guild_id:
129+
return self.guild
130+
else:
131+
return discord.utils.get(self.guilds, id=int(modmail_guild_id))
124132

125133
@property
126134
def main_category(self):
127135
if self.guild:
128-
return discord.utils.get(self.guild.categories, name='Mod Mail')
136+
return discord.utils.get(self.modmail_guild.categories, name='Mod Mail')
129137

130138
@property
131139
def blocked_users(self):
132-
if self.guild:
140+
if self.modmail_guild:
133141
top_chan = self.main_category.channels[0]
134142
return [int(i) for i in re.findall(r'\d+', top_chan.topic)]
135143

cogs/modmail.py

+32-30
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ def __init__(self, bot):
2020
async def setup(self, ctx):
2121
'''Sets up a server for modmail'''
2222
if self.bot.main_category:
23-
return await ctx.send('This server is already set up.')
23+
return await ctx.send(f'{self.bot.modmail_guild} is already set up.')
2424

25-
categ = await ctx.guild.create_category(
25+
categ = await self.bot.modmail_guild.create_category(
2626
name='Mod Mail',
2727
overwrites=self.bot.overwrites(ctx, modrole=modrole)
2828
)
2929

3030
await categ.edit(position=0)
3131

32-
c = await ctx.guild.create_text_channel(name='thread-logs', category=categ)
32+
c = await self.bot.modmail_guild.create_text_channel(name='thread-logs', category=categ)
3333
await c.edit(topic='Manually add user id\'s to block users.\n\n'
3434
'Blocked\n-------\n\n')
3535

@@ -163,11 +163,11 @@ async def _close(self, ctx):
163163
@commands.command()
164164
async def nsfw(self, ctx):
165165
'''Flags a modmail thread as nsfw.'''
166-
if ctx.channel.category and ctx.channel.category.name == 'Mod Mail':
167-
await ctx.edit(nsfw=True)
168-
em = discord.Embed(description=desc, color=discord.Color.green())
169-
em.set_author(name='Thread closed', url=log_url)
170-
await ctx.send('Done')
166+
thread = self.bot.threads.find(channel=ctx.channel)
167+
if thread is None:
168+
return
169+
await ctx.edit(nsfw=True)
170+
await ctx.message.add_reaction('✅')
171171

172172
@commands.command()
173173
@trigger_typing
@@ -228,7 +228,7 @@ async def reply(self, ctx, *, msg=''):
228228
'''
229229
ctx.message.content = msg
230230
thread = await self.bot.threads.find(channel=ctx.channel)
231-
if thread and thread.channel.category.name == 'Mod Mail':
231+
if thread:
232232
await thread.reply(ctx.message)
233233

234234
@commands.command()
@@ -241,27 +241,29 @@ async def edit(self, ctx, message_id: Optional[int]=None, *, new_message):
241241
`<new_message>` is the new message that will be edited in.
242242
'''
243243
thread = await self.bot.threads.find(channel=ctx.channel)
244-
print(message_id, new_message)
245-
if thread and thread.channel.category.name == 'Mod Mail':
246-
linked_message_id = None
247-
248-
async for msg in ctx.channel.history():
249-
if message_id is None and msg.embeds:
250-
em = msg.embeds[0]
251-
if 'Moderator' not in str(em.footer.text):
252-
continue
253-
linked_message_id = int(re.findall(r'\d+', em.author.url)[0])
254-
break
255-
elif message_id and msg.id == message_id:
256-
url = msg.embeds[0].author.url
257-
linked_message_id = int(re.findall(r'\d+', url)[0])
258-
break
244+
245+
if thread is None:
246+
return
247+
248+
linked_message_id = None
259249

260-
if not linked_message_id:
261-
raise commands.UserInputError
250+
async for msg in ctx.channel.history():
251+
if message_id is None and msg.embeds:
252+
em = msg.embeds[0]
253+
if 'Moderator' not in str(em.footer.text):
254+
continue
255+
linked_message_id = int(re.findall(r'\d+', em.author.url)[0])
256+
break
257+
elif message_id and msg.id == message_id:
258+
url = msg.embeds[0].author.url
259+
linked_message_id = int(re.findall(r'\d+', url)[0])
260+
break
261+
262+
if not linked_message_id:
263+
raise commands.UserInputError
262264

263-
await thread.edit_message(linked_message_id, new_message)
264-
await ctx.message.add_reaction('✅')
265+
await thread.edit_message(linked_message_id, new_message)
266+
await ctx.message.add_reaction('✅')
265267

266268
@commands.command()
267269
@trigger_typing
@@ -363,8 +365,8 @@ async def unblock(self, ctx, id=None):
363365
else:
364366
raise commands.UserInputError
365367

366-
categ = discord.utils.get(ctx.guild.categories, name='Mod Mail')
367-
top_chan = categ.channels[0] #bot-info
368+
categ = self.bot.main_category
369+
top_chan = categ.channels[0] #thread-logs
368370
topic = str(top_chan.topic)
369371
topic = topic.replace('\n'+id, '')
370372

core/decorators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async def wrapper(self, ctx, *args, **kwargs):
1313
def auth_required(func):
1414
@functools.wraps(func)
1515
async def wrapper(self, ctx, *args, **kwargs):
16-
if self.bot.config.get('MODMAIL_API_TOKEN'):
16+
if self.bot.config.get('modmail_api_token'):
1717
return await func(self, ctx, *args, **kwargs)
1818
em = discord.Embed(
1919
color=discord.Color.red(),

core/thread.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def __init__(self, bot):
133133
self.cache = {}
134134

135135
async def populate_cache(self):
136-
for channel in self.bot.guild.text_channels:
136+
for channel in self.bot.modmail_guild.text_channels:
137137
await self.find(channel=channel)
138138

139139
def __len__(self):
@@ -153,7 +153,7 @@ async def find(self, *, recipient=None, channel=None):
153153
thread = self.cache[recipient.id]
154154
except KeyError:
155155
channel = discord.utils.get(
156-
self.bot.guild.text_channels,
156+
self.bot.modmail_guild.text_channels,
157157
topic=f'User ID: {recipient.id}'
158158
)
159159
if not channel:
@@ -175,8 +175,8 @@ async def _find_from_channel(self, channel):
175175

176176
if channel.topic and 'User ID: ' in channel.topic:
177177
user_id = int(re.findall(r'\d+', channel.topic)[0])
178-
elif channel.topic is None and channel.category.name == 'Mod Mail':
179-
async for message in channel.history():
178+
elif channel.topic is None:
179+
async for message in channel.history(limit=50):
180180
if message.embeds:
181181
em = message.embeds[0]
182182
matches = re.findall(r'<@(\d+)>', str(em.description))
@@ -213,7 +213,7 @@ async def create(self, recipient, *, creator=None):
213213

214214
self.cache[recipient.id] = thread = Thread(self, recipient)
215215

216-
channel = await self.bot.guild.create_text_channel(
216+
channel = await self.bot.modmail_guild.create_text_channel(
217217
name=self._format_channel_name(recipient),
218218
category=self.bot.main_category
219219
)
@@ -282,7 +282,7 @@ def _format_channel_name(self, author):
282282
allowed = string.ascii_letters + string.digits + '-'
283283
new_name = ''.join(l for l in name if l in allowed) or 'null'
284284
new_name += f'-{author.discriminator}'
285-
while new_name in [c.name for c in self.bot.guild.text_channels]:
285+
while new_name in [c.name for c in self.bot.modmail_guild.text_channels]:
286286
new_name += '-x' # two channels with same name
287287
return new_name
288288

0 commit comments

Comments
 (0)