mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-08 20:28:55 -05:00
commit
284ee5b0e3
@ -1,4 +1,3 @@
|
|||||||
import discord
|
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from .utils.chat_formatting import *
|
from .utils.chat_formatting import *
|
||||||
from .utils.dataIO import fileIO
|
from .utils.dataIO import fileIO
|
||||||
@ -6,6 +5,7 @@ from .utils import checks
|
|||||||
from __main__ import send_cmd_help
|
from __main__ import send_cmd_help
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
class Alias:
|
class Alias:
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
@ -24,17 +24,22 @@ class Alias:
|
|||||||
|
|
||||||
Example: !alias add test flip @Twentysix"""
|
Example: !alias add test flip @Twentysix"""
|
||||||
server = ctx.message.server
|
server = ctx.message.server
|
||||||
if self.get_prefix(to_execute) == False:
|
if self.part_of_existing_command(command, server.id):
|
||||||
to_execute = self.bot.command_prefix[0] + to_execute
|
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:
|
if server.id not in self.aliases:
|
||||||
self.aliases[server.id] = {}
|
self.aliases[server.id] = {}
|
||||||
#curr_aliases = self.aliases[server.id]
|
|
||||||
if command not in self.bot.commands:
|
if command not in self.bot.commands:
|
||||||
self.aliases[server.id][command] = to_execute
|
self.aliases[server.id][command] = to_execute
|
||||||
fileIO("data/alias/aliases.json", "save", self.aliases)
|
fileIO("data/alias/aliases.json", "save", self.aliases)
|
||||||
await self.bot.say("Alias '{}' added.".format(command))
|
await self.bot.say("Alias '{}' added.".format(command))
|
||||||
else:
|
else:
|
||||||
await self.bot.say("Cannot add '{}' because it's a real bot command.".format(command))
|
await self.bot.say("Cannot add '{}' because it's a real bot "
|
||||||
|
"command.".format(command))
|
||||||
|
|
||||||
@alias.command(name="help", pass_context=True)
|
@alias.command(name="help", pass_context=True)
|
||||||
async def _help_alias(self, ctx, command):
|
async def _help_alias(self, ctx, command):
|
||||||
@ -74,7 +79,8 @@ class Alias:
|
|||||||
await self.bot.say("Alias '{}' deleted.".format(command))
|
await self.bot.say("Alias '{}' deleted.".format(command))
|
||||||
|
|
||||||
async def check_aliases(self, message):
|
async def check_aliases(self, message):
|
||||||
if message.author.id == self.bot.user.id or len(message.content) < 2 or message.channel.is_private:
|
if message.author.id == self.bot.user.id or \
|
||||||
|
len(message.content) < 2 or message.channel.is_private:
|
||||||
return
|
return
|
||||||
|
|
||||||
msg = message.content
|
msg = message.content
|
||||||
@ -82,14 +88,31 @@ 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:
|
||||||
aliaslist = self.aliases[server.id]
|
for alias in self.aliases[server.id]:
|
||||||
alias = msg[len(prefix):].split(" ")[0]
|
if msg[len(prefix):].startswith(alias):
|
||||||
args = msg[len(self.first_word(message.content)):]
|
new_command = self.aliases[server.id][alias]
|
||||||
if alias in aliaslist.keys():
|
args = message.content[len(prefix+alias):]
|
||||||
content = aliaslist[alias] + args
|
message.content = prefix + new_command + args
|
||||||
new_message = message
|
await self.bot.process_commands(message)
|
||||||
new_message.content = content
|
|
||||||
await self.bot.process_commands(new_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
|
||||||
|
if alias.split(" ")[0] in self.aliases[server]:
|
||||||
|
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):
|
def first_word(self, msg):
|
||||||
return msg.split(" ")[0]
|
return msg.split(" ")[0]
|
||||||
@ -98,13 +121,15 @@ class Alias:
|
|||||||
for p in self.bot.command_prefix:
|
for p in self.bot.command_prefix:
|
||||||
if msg.startswith(p):
|
if msg.startswith(p):
|
||||||
return p
|
return p
|
||||||
return False
|
return None
|
||||||
|
|
||||||
|
|
||||||
def check_folder():
|
def check_folder():
|
||||||
if not os.path.exists("data/alias"):
|
if not os.path.exists("data/alias"):
|
||||||
print("Creating data/alias folder...")
|
print("Creating data/alias folder...")
|
||||||
os.makedirs("data/alias")
|
os.makedirs("data/alias")
|
||||||
|
|
||||||
|
|
||||||
def check_file():
|
def check_file():
|
||||||
aliases = {}
|
aliases = {}
|
||||||
|
|
||||||
@ -113,9 +138,11 @@ def check_file():
|
|||||||
print("Creating default alias's aliases.json...")
|
print("Creating default alias's aliases.json...")
|
||||||
fileIO(f, "save", aliases)
|
fileIO(f, "save", aliases)
|
||||||
|
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
check_folder()
|
check_folder()
|
||||||
check_file()
|
check_file()
|
||||||
n = Alias(bot)
|
n = Alias(bot)
|
||||||
|
n.remove_old()
|
||||||
bot.add_listener(n.check_aliases, "on_message")
|
bot.add_listener(n.check_aliases, "on_message")
|
||||||
bot.add_cog(n)
|
bot.add_cog(n)
|
||||||
Loading…
x
Reference in New Issue
Block a user