Skip to content

Commit 47f72e7

Browse files
committed
Added dynamic snippet configuration.
1 parent e4281da commit 47f72e7

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

bot.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class ModmailBot(commands.Bot):
6262

6363
def __init__(self):
6464
super().__init__(command_prefix=self.get_pre)
65+
self.version = __version__
6566
self.start_time = datetime.datetime.utcnow()
6667
self.threads = ThreadManager(self)
6768
self.session = aiohttp.ClientSession(loop=self.loop)
@@ -103,7 +104,11 @@ def run(self):
103104

104105
@property
105106
def snippets(self):
106-
return self.config.get('snippets', {})
107+
return {k: v for k, v in self.config.get('snippets', {}).items() if v}
108+
109+
@property
110+
def aliases(self):
111+
return {k: v for k, v in self.config.get('aliases', {}).items() if v}
107112

108113
@property
109114
def token(self):
@@ -127,12 +132,15 @@ def blocked_users(self):
127132
if self.guild:
128133
top_chan = self.main_category.channels[0]
129134
return [int(i) for i in re.findall(r'\d+', top_chan.topic)]
135+
136+
@property
137+
def prefix(self):
138+
return self.config.get('prefix', '?')
130139

131140
@staticmethod
132141
async def get_pre(bot, message):
133142
'''Returns the prefix.'''
134-
p = bot.config.get('prefix') or 'm.'
135-
return [p, f'<@{bot.user.id}> ', f'<@!{bot.user.id}> ']
143+
return [bot.prefix, f'<@{bot.user.id}> ', f'<@!{bot.user.id}> ']
136144

137145
async def on_connect(self):
138146
print(line)
@@ -184,12 +192,14 @@ async def on_message(self, message):
184192
if isinstance(message.channel, discord.DMChannel):
185193
return await self.process_modmail(message)
186194

187-
prefix = self.config.get('prefix', 'm.')
195+
prefix = self.prefix
188196

189197
if message.content.startswith(prefix):
190198
cmd = message.content[len(prefix):].strip()
191199
if cmd in self.snippets:
192200
message.content = f'{prefix}reply {self.snippets[cmd]}'
201+
if cmd in self.aliases:
202+
message.content = f'{prefix}{self.aliases[cmd]}'
193203

194204
await self.process_commands(message)
195205

cogs/modmail.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import datetime
55
import dateutil.parser
66
from core.decorators import trigger_typing
7+
from core.paginator import PaginatorSession
78

89
class Modmail:
910
'''Commands directly related to Modmail functionality.'''
@@ -32,10 +33,13 @@ async def setup(self, ctx):
3233

3334
await ctx.send('Successfully set up server.')
3435

35-
@commands.command(name='snippets')
36+
@commands.group(name='snippets')
3637
@commands.has_permissions(manage_messages=True)
37-
async def _snippets(self, ctx):
38+
async def snippets(self, ctx):
3839
'''Returns a list of snippets that are currently set.'''
40+
if ctx.invoked_subcommand is not None:
41+
return
42+
3943
embeds = []
4044

4145
em = discord.Embed(color=discord.Color.green())
@@ -47,7 +51,8 @@ async def _snippets(self, ctx):
4751

4852
if not self.bot.snippets:
4953
em.color = discord.Color.red()
50-
em.description = 'You dont have any snippets at the moment.'
54+
em.description = f'You dont have any snippets at the moment.'
55+
em.set_footer(text=f'Do {self.bot.prefix}help snippets for more commands.')
5156

5257
for name, value in self.bot.snippets.items():
5358
if len(em.fields) == 5:
@@ -58,6 +63,46 @@ async def _snippets(self, ctx):
5863

5964
session = PaginatorSession(ctx, *embeds)
6065
await session.run()
66+
67+
@snippets.command(name='add')
68+
async def _add(self, ctx, name: str.lower, *, value):
69+
'''Add a snippet to the bot config.'''
70+
if 'snippets' not in self.bot.config.cache:
71+
self.bot.config['snippets'] = {}
72+
73+
self.bot.config.snippets[name] = value
74+
await self.bot.config.update()
75+
76+
em = discord.Embed(
77+
title='Added snippet',
78+
color=discord.Color.green(),
79+
description=f'`{name}` points to: {value}'
80+
)
81+
82+
await ctx.send(embed=em)
83+
84+
@snippets.command(name='del')
85+
async def __del(self, ctx, *, name: str.lower):
86+
'''Removes a snippet from bot config.'''
87+
88+
if 'snippets' not in self.bot.config.cache:
89+
self.bot.config['snippets'] = {}
90+
91+
em = discord.Embed(
92+
title='Removed snippet',
93+
color=discord.Color.green(),
94+
description=f'`{name}` no longer exists.'
95+
)
96+
97+
if not self.bot.config.snippets.get(name):
98+
em.title = 'Error'
99+
em.color = discord.Color.red()
100+
em.description = f'Snippet `{name}` does not exist.'
101+
else:
102+
self.bot.config['snippets'][name] = None
103+
await self.bot.config.update()
104+
105+
await ctx.send(embed=em)
61106

62107
@commands.command(name='close')
63108
@commands.has_permissions(manage_channels=True)

core/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ async def refresh(self):
4242
def __getattr__(self, value):
4343
return self.cache[value]
4444

45+
def __setitem__(self, key, item):
46+
self.cache[key] = item
47+
48+
def __getitem__(self, key):
49+
return self.cache[key]
50+
4551
def get(self, value, default=None):
4652
return self.cache.get(value) or default
4753

0 commit comments

Comments
 (0)