Skip to content

Commit f7cf9d3

Browse files
committed
Support alias arguments
1 parent b31590a commit f7cf9d3

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929

3030
<img src='https://i.imgur.com/aHtn4C5.png' align='right' height=140>
3131

32-
Assuming you got the bot setup (Read below on how to set it up), the first thing that you would do is type the command ```<prefix>setup [modrole]``` where `[modrole]` is an optional role you can specify which determines who can see the relayed messages. If a role is not specified, the bot will choose the first role that has `manage guild` permissions as the modrole. The bot will then set up a channel category named `Mod Mail`.
33-
34-
When a user sends a direct message to the bot, a channel is created within this new category. This channel is where messages will be relayed. To reply to a message, simply type the command `<prefix>reply <message>` in the channel.
32+
When a user sends a direct message to the bot, a channel is created within an isolated category. This channel is where messages will be relayed. To reply to a message, simply use the command `reply` in the channel. See a full list of commands [below](#commands).
3533

3634
## What it looks like
3735

bot.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
init()
4747

4848
from discord.ext import commands
49+
from discord.ext.commands.view import StringView
4950
import discord
5051
import aiohttp
5152

@@ -186,6 +187,44 @@ async def process_modmail(self, message):
186187
thread = await self.threads.find_or_create(message.author)
187188
await thread.send(message)
188189

190+
async def get_context(self, message, *, cls=commands.Context):
191+
"""
192+
Returns the invocation context from the message.
193+
Supports getting the prefix from database as well as command aliases.
194+
"""
195+
196+
view = StringView(message.content)
197+
ctx = cls(prefix=None, view=view, bot=self, message=message)
198+
199+
if self._skip_check(message.author.id, self.user.id):
200+
return ctx
201+
202+
203+
prefixes = [self.prefix, f'<@{bot.user.id}> ', f'<@!{bot.user.id}>']
204+
205+
invoked_prefix = discord.utils.find(view.skip_string, prefixes)
206+
if invoked_prefix is None:
207+
return ctx
208+
209+
invoker = view.get_word().lower()
210+
211+
# Check if there is any aliases being called.
212+
alias = self.config.get('aliases', {}).get(invoker)
213+
if alias is not None:
214+
ctx._alias_invoked = True
215+
_len = len(f'{invoked_prefix}{invoker}')
216+
ctx.view = view = StringView(f'{alias}{ctx.message.content[_len:]}')
217+
invoker = view.get_word()
218+
219+
ctx.invoked_with = invoker
220+
ctx.prefix = self.prefix # Sane prefix (No mentions)
221+
ctx.command = self.all_commands.get(invoker)
222+
223+
# if hasattr(ctx, '_alias_invoked'):
224+
# ctx.command.checks = None # Let anyone use the command.
225+
226+
return ctx
227+
189228
async def on_message(self, message):
190229
if message.author.bot:
191230
return
@@ -198,8 +237,6 @@ async def on_message(self, message):
198237
cmd = message.content[len(prefix):].strip()
199238
if cmd in self.snippets:
200239
message.content = f'{prefix}reply {self.snippets[cmd]}'
201-
if cmd in self.aliases:
202-
message.content = f'{prefix}{self.aliases[cmd]}'
203240

204241
await self.process_commands(message)
205242

cogs/utility.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,16 +440,17 @@ async def aliases(self, ctx):
440440
embeds.append(em)
441441

442442
em.description = 'Here is a list of aliases that are currently configured.'
443+
em.set_footer(text=f'Do {self.bot.prefix}help aliases for more commands.')
443444

444445
if not self.bot.aliases:
445446
em.color = discord.Color.red()
446447
em.description = f'You dont have any aliases at the moment.'
447-
em.set_footer(text=f'Do {self.bot.prefix}help aliases for more commands.')
448448

449449
for name, value in self.bot.aliases.items():
450450
if len(em.fields) == 5:
451451
em = discord.Embed(color=discord.Color.green(), description=em.description)
452452
em.set_author(name='Command aliases', icon_url=ctx.guild.icon_url)
453+
em.set_footer(text=f'Do {self.bot.prefix}help aliases for more commands.')
453454
embeds.append(em)
454455
em.add_field(name=name, value=value, inline=False)
455456

0 commit comments

Comments
 (0)