[Core] Server based prefixes (#492)

Added `[p]set serverprefix`
`Bot.command_prefix` is now a callable that returns the prefixes set for the server. If none are set, it returns the global ones.
This commit is contained in:
Twentysix
2016-12-01 02:30:51 +01:00
committed by GitHub
parent 8b7ba988f4
commit d628bacef5
6 changed files with 127 additions and 62 deletions

View File

@@ -3,8 +3,9 @@ from .utils.chat_formatting import *
from .utils.dataIO import dataIO
from .utils import checks
from __main__ import user_allowed, send_cmd_help
import os
from copy import deepcopy
import os
import discord
class Alias:
@@ -12,6 +13,7 @@ class Alias:
self.bot = bot
self.file_path = "data/alias/aliases.json"
self.aliases = dataIO.load_json(self.file_path)
self.remove_old()
@commands.group(pass_context=True, no_pm=True)
async def alias(self, ctx):
@@ -36,7 +38,7 @@ class Alias:
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)
prefix = self.get_prefix(server, to_execute)
if prefix is not None:
to_execute = to_execute[len(prefix):]
if server.id not in self.aliases:
@@ -57,9 +59,10 @@ class Alias:
server_aliases = self.aliases[server.id]
if command in server_aliases:
help_cmd = server_aliases[command].split(" ")[0]
new_content = self.bot.command_prefix[0]
new_content = self.bot.settings.get_prefixes(server)[0]
new_content += "help "
new_content += help_cmd[len(self.get_prefix(help_cmd)):]
new_content += help_cmd[len(self.get_prefix(server,
help_cmd)):]
message = ctx.message
message.content = new_content
await self.bot.process_commands(message)
@@ -107,13 +110,13 @@ class Alias:
else:
await self.bot.say("There are no aliases on this server.")
async def check_aliases(self, message):
async def on_message(self, message):
if len(message.content) < 2 or message.channel.is_private:
return
msg = message.content
server = message.server
prefix = self.get_prefix(msg)
prefix = self.get_prefix(server, msg)
if not prefix:
return
@@ -146,7 +149,8 @@ class Alias:
if aliasname != self.first_word(aliasname):
to_delete.append(aliasname)
continue
prefix = self.get_prefix(alias)
server = discord.Object(id=sid)
prefix = self.get_prefix(server, alias)
if prefix is not None:
self.aliases[sid][aliasname] = alias[len(prefix):]
for alias in to_delete: # Fixes caps and bad prefixes
@@ -158,8 +162,9 @@ class Alias:
def first_word(self, msg):
return msg.split(" ")[0]
def get_prefix(self, msg):
for p in self.bot.command_prefix:
def get_prefix(self, server, msg):
prefixes = self.bot.settings.get_prefixes(server)
for p in prefixes:
if msg.startswith(p):
return p
return None
@@ -183,7 +188,4 @@ def check_file():
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)
bot.add_cog(Alias(bot))