mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-09 04:38:55 -05:00
Alias case insensitivity (#303)
This commit is contained in:
parent
ec2d166e3e
commit
f255973ad5
@ -4,6 +4,7 @@ from .utils.dataIO import fileIO
|
|||||||
from .utils import checks
|
from .utils import checks
|
||||||
from __main__ import user_allowed, send_cmd_help
|
from __main__ import user_allowed, send_cmd_help
|
||||||
import os
|
import os
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
|
|
||||||
class Alias:
|
class Alias:
|
||||||
@ -11,19 +12,20 @@ class Alias:
|
|||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.aliases = fileIO("data/alias/aliases.json", "load")
|
self.aliases = fileIO("data/alias/aliases.json", "load")
|
||||||
|
|
||||||
@commands.group(pass_context=True)
|
@commands.group(pass_context=True, no_pm=True)
|
||||||
@checks.mod_or_permissions(administrator=True)
|
|
||||||
async def alias(self, ctx):
|
async def alias(self, ctx):
|
||||||
"""Manage per-server aliases for commands"""
|
"""Manage per-server aliases for commands"""
|
||||||
if ctx.invoked_subcommand is None:
|
if ctx.invoked_subcommand is None:
|
||||||
await send_cmd_help(ctx)
|
await send_cmd_help(ctx)
|
||||||
|
|
||||||
@alias.command(name="add", pass_context=True)
|
@alias.command(name="add", pass_context=True, no_pm=True)
|
||||||
async def _add_alias(self, ctx, command: str, *, to_execute):
|
@checks.mod_or_permissions(manage_server=True)
|
||||||
|
async def _add_alias(self, ctx, command, *, to_execute):
|
||||||
"""Add an alias for a command
|
"""Add an alias for a command
|
||||||
|
|
||||||
Example: !alias add test flip @Twentysix"""
|
Example: !alias add test flip @Twentysix"""
|
||||||
server = ctx.message.server
|
server = ctx.message.server
|
||||||
|
command = command.lower()
|
||||||
if len(command.split(" ")) != 1:
|
if len(command.split(" ")) != 1:
|
||||||
await self.bot.say("I can't safely do multi-word aliases because"
|
await self.bot.say("I can't safely do multi-word aliases because"
|
||||||
" of the fact that I allow arguments to"
|
" of the fact that I allow arguments to"
|
||||||
@ -46,7 +48,7 @@ class Alias:
|
|||||||
await self.bot.say("Cannot add '{}' because it's a real bot "
|
await self.bot.say("Cannot add '{}' because it's a real bot "
|
||||||
"command.".format(command))
|
"command.".format(command))
|
||||||
|
|
||||||
@alias.command(name="help", pass_context=True)
|
@alias.command(name="help", pass_context=True, no_pm=True)
|
||||||
async def _help_alias(self, ctx, command):
|
async def _help_alias(self, ctx, command):
|
||||||
"""Tries to execute help for the base command of the alias"""
|
"""Tries to execute help for the base command of the alias"""
|
||||||
server = ctx.message.server
|
server = ctx.message.server
|
||||||
@ -63,7 +65,7 @@ class Alias:
|
|||||||
else:
|
else:
|
||||||
await self.bot.say("That alias doesn't exist.")
|
await self.bot.say("That alias doesn't exist.")
|
||||||
|
|
||||||
@alias.command(name="show", pass_context=True)
|
@alias.command(name="show", pass_context=True, no_pm=True)
|
||||||
async def _show_alias(self, ctx, command):
|
async def _show_alias(self, ctx, command):
|
||||||
"""Shows what command the alias executes."""
|
"""Shows what command the alias executes."""
|
||||||
server = ctx.message.server
|
server = ctx.message.server
|
||||||
@ -74,28 +76,35 @@ class Alias:
|
|||||||
else:
|
else:
|
||||||
await self.bot.say("That alias doesn't exist.")
|
await self.bot.say("That alias doesn't exist.")
|
||||||
|
|
||||||
@alias.command(name="del", pass_context=True)
|
@alias.command(name="del", pass_context=True, no_pm=True)
|
||||||
async def _del_alias(self, ctx, command: str):
|
@checks.mod_or_permissions(manage_server=True)
|
||||||
|
async def _del_alias(self, ctx, command):
|
||||||
"""Deletes an alias"""
|
"""Deletes an alias"""
|
||||||
|
command = command.lower()
|
||||||
server = ctx.message.server
|
server = ctx.message.server
|
||||||
if server.id in self.aliases:
|
if server.id in self.aliases:
|
||||||
self.aliases[server.id].pop(command, None)
|
self.aliases[server.id].pop(command, None)
|
||||||
fileIO("data/alias/aliases.json", "save", self.aliases)
|
fileIO("data/alias/aliases.json", "save", self.aliases)
|
||||||
await self.bot.say("Alias '{}' deleted.".format(command))
|
await self.bot.say("Alias '{}' deleted.".format(command))
|
||||||
|
|
||||||
@commands.command(pass_context=True)
|
@alias.command(name="list", pass_context=True, no_pm=True)
|
||||||
async def aliaslist(self, ctx):
|
async def _alias_list(self, ctx):
|
||||||
|
"""Lists aliases available on this server
|
||||||
|
|
||||||
|
Responds in DM"""
|
||||||
server = ctx.message.server
|
server = ctx.message.server
|
||||||
if server.id in self.aliases:
|
if server.id in self.aliases:
|
||||||
message = "```Alias list:\n"
|
message = "```Alias list:\n"
|
||||||
for alias in sorted(self.aliases[server.id]):
|
for alias in sorted(self.aliases[server.id]):
|
||||||
if len(message) + len(alias) + 3 > 2000:
|
if len(message) + len(alias) + 3 > 2000:
|
||||||
await self.bot.say(message)
|
await self.bot.whisper(message)
|
||||||
message = "```\n"
|
message = "```\n"
|
||||||
message += "\t{}\n".format(alias)
|
message += "\t{}\n".format(alias)
|
||||||
if len(message) > 4:
|
if message != "```Alias list:\n":
|
||||||
message += "```"
|
message += "```"
|
||||||
await self.bot.say(message)
|
await self.bot.whisper(message)
|
||||||
|
else:
|
||||||
|
await self.bot.say("There are no aliases on this server.")
|
||||||
|
|
||||||
async def check_aliases(self, message):
|
async def check_aliases(self, message):
|
||||||
if not user_allowed(message):
|
if not user_allowed(message):
|
||||||
@ -110,12 +119,13 @@ class Alias:
|
|||||||
prefix = self.get_prefix(msg)
|
prefix = self.get_prefix(msg)
|
||||||
|
|
||||||
if prefix and server.id in self.aliases:
|
if prefix and server.id in self.aliases:
|
||||||
if self.first_word(msg[len(prefix):]) in self.aliases[server.id]:
|
alias = self.first_word(msg[len(prefix):]).lower()
|
||||||
alias = self.first_word(msg[len(prefix):])
|
if alias in self.aliases[server.id]:
|
||||||
new_command = self.aliases[server.id][alias]
|
new_command = self.aliases[server.id][alias]
|
||||||
args = message.content[len(prefix + alias):]
|
args = message.content[len(prefix + alias):]
|
||||||
message.content = prefix + new_command + args
|
new_message = deepcopy(message)
|
||||||
await self.bot.process_commands(message)
|
new_message.content = prefix + new_command + args
|
||||||
|
await self.bot.process_commands(new_message)
|
||||||
|
|
||||||
def part_of_existing_command(self, alias, server):
|
def part_of_existing_command(self, alias, server):
|
||||||
'''Command or alias'''
|
'''Command or alias'''
|
||||||
@ -127,15 +137,22 @@ class Alias:
|
|||||||
def remove_old(self):
|
def remove_old(self):
|
||||||
for sid in self.aliases:
|
for sid in self.aliases:
|
||||||
to_delete = []
|
to_delete = []
|
||||||
|
to_add = []
|
||||||
for aliasname, alias in self.aliases[sid].items():
|
for aliasname, alias in self.aliases[sid].items():
|
||||||
|
lower = aliasname.lower()
|
||||||
|
if aliasname != lower:
|
||||||
|
to_delete.append(aliasname)
|
||||||
|
to_add.append((lower, alias))
|
||||||
if aliasname != self.first_word(aliasname):
|
if aliasname != self.first_word(aliasname):
|
||||||
to_delete.append(aliasname)
|
to_delete.append(aliasname)
|
||||||
continue
|
continue
|
||||||
prefix = self.get_prefix(alias)
|
prefix = self.get_prefix(alias)
|
||||||
if prefix is not None:
|
if prefix is not None:
|
||||||
self.aliases[sid][aliasname] = alias[len(prefix):]
|
self.aliases[sid][aliasname] = alias[len(prefix):]
|
||||||
for alias in to_delete:
|
for alias in to_delete: # Fixes caps and bad prefixes
|
||||||
del self.aliases[sid][alias]
|
del self.aliases[sid][alias]
|
||||||
|
for alias, command in to_add: # For fixing caps
|
||||||
|
self.aliases[sid][alias] = command
|
||||||
fileIO("data/alias/aliases.json", "save", self.aliases)
|
fileIO("data/alias/aliases.json", "save", self.aliases)
|
||||||
|
|
||||||
def first_word(self, msg):
|
def first_word(self, msg):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user