mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-07 11:48:55 -05:00
Aliases do more, better now
This commit is contained in:
parent
8760fb92d5
commit
74332e2cda
@ -24,8 +24,13 @@ class Alias:
|
||||
|
||||
Example: !alias add test flip @Twentysix"""
|
||||
server = ctx.message.server
|
||||
if self.get_prefix(to_execute) is False:
|
||||
to_execute = self.bot.command_prefix[0] + to_execute
|
||||
if self.part_of_existing_command(command, server.id):
|
||||
await self.bot.say('I can\'t safely add an alias that starts with '
|
||||
'an existing command or alias. Sry <3')
|
||||
return
|
||||
prefix = self.get_prefix(to_execute)
|
||||
if prefix is not None:
|
||||
to_execute = to_execute[len(prefix):]
|
||||
if server.id not in self.aliases:
|
||||
self.aliases[server.id] = {}
|
||||
if command not in self.bot.commands:
|
||||
@ -83,14 +88,32 @@ class Alias:
|
||||
prefix = self.get_prefix(msg)
|
||||
|
||||
if prefix and server.id in self.aliases:
|
||||
aliaslist = self.aliases[server.id]
|
||||
alias = msg[len(prefix):].split(" ")[0]
|
||||
args = msg[len(self.first_word(message.content)):]
|
||||
if alias in aliaslist.keys():
|
||||
content = aliaslist[alias] + args
|
||||
new_message = message
|
||||
new_message.content = content
|
||||
await self.bot.process_commands(new_message)
|
||||
for alias in self.aliases[server.id]:
|
||||
if msg[len(prefix):].startswith(alias):
|
||||
new_command = self.aliases[server.id][alias]
|
||||
args = message.content[len(prefix+alias):]
|
||||
message.content = prefix + new_command + args
|
||||
await self.bot.process_commands(message)
|
||||
|
||||
def part_of_existing_command(self, alias, server):
|
||||
'''Command or alias'''
|
||||
for command in self.bot.commands:
|
||||
if alias.startswith(command):
|
||||
return True
|
||||
if server not in self.aliases:
|
||||
return False
|
||||
for aname in self.aliases[server]:
|
||||
if aname.startswith(alias):
|
||||
return True
|
||||
return False
|
||||
|
||||
def remove_old(self):
|
||||
for sid in self.aliases:
|
||||
for aliasname, alias in self.aliases[sid].items():
|
||||
prefix = self.get_prefix(alias)
|
||||
if prefix is not None:
|
||||
self.aliases[sid][aliasname] = alias[len(prefix):]
|
||||
fileIO("data/alias/aliases.json", "save", self.aliases)
|
||||
|
||||
def first_word(self, msg):
|
||||
return msg.split(" ")[0]
|
||||
@ -99,7 +122,7 @@ class Alias:
|
||||
for p in self.bot.command_prefix:
|
||||
if msg.startswith(p):
|
||||
return p
|
||||
return False
|
||||
return None
|
||||
|
||||
|
||||
def check_folder():
|
||||
@ -121,5 +144,6 @@ def setup(bot):
|
||||
check_folder()
|
||||
check_file()
|
||||
n = Alias(bot)
|
||||
n.remove_old()
|
||||
bot.add_listener(n.check_aliases, "on_message")
|
||||
bot.add_cog(n)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user