Skip to content

Commit 9d636e4

Browse files
committed
Store blocked users in the database now.
1 parent 82151c3 commit 9d636e4

File tree

4 files changed

+42
-38
lines changed

4 files changed

+42
-38
lines changed

CHANGELOG.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8-
# [ unreleased ]
8+
# v2.0.8
99

10-
Improvements in commands and new config option available.
10+
Improvements in commands and new config options available.
1111

1212
### Added
1313
- Added the ability to use your own log channel
14-
- You can do this by using the `config set log_channel_id <id>` command.
14+
- You can do this via the `config set log_channel_id <id>` command.
15+
- Added the ability to use your own main inbox category.
16+
- You can do this via the `config set main_category_id <id>` command.
1517

1618
### Changed
1719
- You now have the ability to supply a reason when blocking a user.
1820
- Blocked users are now stored in the database instead of in the channel topic.
19-
- This means you can delete the top channel in the modmail category now.
20-
21+
- This means you can delete the top channel in the modmail category now. (Migrate first though.)
2122

2223
# v2.0.7
2324

bot.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
SOFTWARE.
2323
"""
2424

25-
__version__ = '2.0.7'
25+
__version__ = '2.0.8'
2626

2727
import asyncio
2828
import textwrap
@@ -94,7 +94,7 @@ def run(self):
9494

9595
@property
9696
def log_channel(self):
97-
channel_id = self.config.get('bot_log_channel_id')
97+
channel_id = self.config.get('log_channel_id')
9898
if channel_id is not None:
9999
return self.get_channel(int(channel_id))
100100
else:
@@ -136,14 +136,15 @@ def using_multiple_server_setup(self):
136136

137137
@property
138138
def main_category(self):
139+
category_id = self.config.get('main_category_id')
140+
if category_id is not None:
141+
return discord.utils.get(self.modmail_guild.categories, id=int(category_id))
139142
if self.modmail_guild:
140143
return discord.utils.get(self.modmail_guild.categories, name='Mod Mail')
141144

142145
@property
143146
def blocked_users(self):
144-
if self.modmail_guild:
145-
top_chan = self.main_category.channels[0]
146-
return [int(i) for i in re.findall(r'\d+', top_chan.topic)]
147+
return self.config.get('blocked', {})
147148

148149
@property
149150
def prefix(self):
@@ -422,7 +423,7 @@ async def autoupdate_loop(self):
422423
short_sha = commit_data['sha'][:6]
423424
em.add_field(name='Merge Commit', value=f"[`{short_sha}`]({html_url}) {message} - {user['username']}")
424425
print('Updating bot.')
425-
channel = self.main_category.channels[0]
426+
channel = self.log_channel
426427
await channel.send(embed=em)
427428

428429
await asyncio.sleep(3600)

cogs/modmail.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ async def setup(self, ctx):
3030
await categ.edit(position=0)
3131

3232
c = await self.bot.modmail_guild.create_text_channel(name='bot-logs', category=categ)
33-
await c.edit(topic='Manually add user id\'s to block users.\n\n'
34-
'Blocked\n-------\n\n')
33+
await c.edit(topic='You can delete this channel if you set up your own log channel.')
34+
await c.send('Use the `config set log_channel_id` command to set up a custom log channel.')
3535

3636
await ctx.send('Successfully set up server.')
3737

@@ -150,6 +150,9 @@ async def _close(self, ctx):
150150
}
151151
})
152152

153+
if isinstance(log_data, str):
154+
print(log_data) # error
155+
153156
log_url = f"https://logs.modmail.tk/{log_data['user_id']}/{log_data['key']}"
154157

155158
user = thread.recipient.mention if thread.recipient else f'`{thread.id}`'
@@ -299,19 +302,19 @@ async def blocked(self, ctx):
299302
users = []
300303
not_reachable = []
301304

302-
for id in self.bot.blocked_users:
303-
user = self.bot.get_user(id)
305+
for id, reason in self.bot.blocked_users.items():
306+
user = self.bot.get_user(int(id))
304307
if user:
305-
users.append(user)
308+
users.append((user, reason))
306309
else:
307-
not_reachable.append(id)
310+
not_reachable.append((id, reason))
308311

309312
em.description = 'Here is a list of blocked users.'
310313

311314
if users:
312-
em.add_field(name='Currently Known', value=' '.join(u.mention for u in users))
315+
em.add_field(name='Currently Known', value='\n'.join(u.mention + (f' - `{r}`' if r else '') for u, r in users))
313316
if not_reachable:
314-
em.add_field(name='Unknown', value='\n'.join(f'`{i}`' for i in not_reachable), inline=False)
317+
em.add_field(name='Unknown', value='\n'.join(f'`{i}`' + (f' - `{r}`' if r else '') for i, r in not_reachable), inline=False)
315318

316319
if not users and not not_reachable:
317320
em.description = 'Currently there are no blocked users'
@@ -321,7 +324,7 @@ async def blocked(self, ctx):
321324
@commands.command()
322325
@trigger_typing
323326
@commands.has_permissions(manage_channels=True)
324-
async def block(self, ctx, *, user: Union[discord.Member, discord.User, obj]=None):
327+
async def block(self, ctx, user: Union[discord.Member, discord.User, obj]=None, *, reason=None):
325328
"""Block a user from using modmail."""
326329

327330
if user is None:
@@ -331,21 +334,18 @@ async def block(self, ctx, *, user: Union[discord.Member, discord.User, obj]=Non
331334
else:
332335
raise commands.UserInputError
333336

334-
categ = self.bot.main_category
335-
top_chan = categ.channels[0] # bot-info
336-
topic = str(top_chan.topic)
337-
topic += '\n' + str(user.id)
338-
337+
339338
mention = user.mention if hasattr(user, 'mention') else f'`{user.id}`'
340339

341340
em = discord.Embed()
342341
em.color = discord.Color.green()
343342

344-
if str(user.id) not in top_chan.topic:
345-
await top_chan.edit(topic=topic)
343+
if str(user.id) not in self.bot.blocked_users:
344+
self.bot.config.blocked[str(user.id)] = reason
345+
await self.bot.config.update()
346346

347347
em.title = 'Success'
348-
em.description = f'{mention} is now blocked'
348+
em.description = f'{mention} is now blocked ' + f'for `{reason}`' if reason else ''
349349

350350
await ctx.send(embed=em)
351351
else:
@@ -368,18 +368,14 @@ async def unblock(self, ctx, *, user: Union[discord.Member, discord.User, obj]=N
368368
else:
369369
raise commands.UserInputError
370370

371-
categ = self.bot.main_category
372-
top_chan = categ.channels[0] # thread-logs
373-
topic = str(top_chan.topic)
374-
topic = topic.replace('\n' + str(user.id), '')
375-
376371
mention = user.mention if hasattr(user, 'mention') else f'`{user.id}`'
377372

378373
em = discord.Embed()
379374
em.color = discord.Color.green()
380375

381-
if str(user.id) in top_chan.topic:
382-
await top_chan.edit(topic=topic)
376+
if str(user.id) in self.bot.blocked_users:
377+
del self.bot.config.blocked[str(user.id)]
378+
await self.bot.config.update()
383379

384380
em.title = 'Success'
385381
em.description = f'{mention} is no longer blocked'

core/config.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ class ConfigManager:
77
"""Class that manages a cached configuration"""
88

99
allowed_to_change_in_command = {
10-
'status', 'bot_log_channel_id', 'mention', 'disable_autoupdates', 'prefix'
10+
'status', 'log_channel_id', 'mention', 'disable_autoupdates', 'prefix',
11+
'main_category_id'
1112
}
1213

1314
internal_keys = {
@@ -28,10 +29,15 @@ def api(self):
2829
return self.bot.modmail_api
2930

3031
def populate_cache(self):
32+
data = {
33+
'snippets': {},
34+
'aliases': {},
35+
'blocked': {}
36+
}
3137
try:
32-
data = json.load(open('config.json'))
38+
data.update(json.load(open('config.json')))
3339
except FileNotFoundError:
34-
data = {}
40+
pass
3541
finally:
3642
data.update(os.environ)
3743
data = {k.lower(): v for k, v in data.items() if k.lower() in self.valid_keys}

0 commit comments

Comments
 (0)