|
|
@@ -29,6 +29,15 @@ import discord |
|
|
|
from discord.ext import commands |
|
|
|
|
|
|
|
import roxbot |
|
|
|
from roxbot.db import * |
|
|
|
|
|
|
|
|
|
|
|
class CCCommands(db.Entity): |
|
|
|
# Sadly no way to add a custom constraint to this while we either use pony or sqlite/ |
|
|
|
name = Required(str) |
|
|
|
output = Required(Json) |
|
|
|
type = Required(int, py_check=lambda val: 0 <= val <= 2) |
|
|
|
guild_id = Required(int, size=64) |
|
|
|
|
|
|
|
|
|
|
|
class CustomCommands: |
|
|
@@ -52,21 +61,11 @@ class CustomCommands: |
|
|
|
def __init__(self, bot_client): |
|
|
|
self.bot = bot_client |
|
|
|
self.embed_fields = ("title", "description", "colour", "color", "footer", "image", "thumbnail", "url") |
|
|
|
self.settings = { |
|
|
|
"custom_commands": { |
|
|
|
"0": {}, |
|
|
|
"1": {}, |
|
|
|
"2": {}, |
|
|
|
"convert": {"0": "hide", "1": "hide", "2": "hide"} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@staticmethod |
|
|
|
def _get_output(command): |
|
|
|
# Check for a list as the output. If so, randomly select a item from the list. |
|
|
|
if isinstance(command, list): |
|
|
|
command = random.choice(command) |
|
|
|
return command |
|
|
|
return random.choice(command) |
|
|
|
|
|
|
|
@staticmethod |
|
|
|
def _cc_to_embed(command_output): |
|
|
@@ -122,24 +121,27 @@ class CustomCommands: |
|
|
|
if self.bot.blacklisted(message.author): |
|
|
|
raise commands.CheckFailure() |
|
|
|
|
|
|
|
settings = roxbot.guild_settings.get(message.guild) |
|
|
|
msg = message.content.lower() |
|
|
|
channel = message.channel |
|
|
|
if msg.startswith(self.bot.command_prefix): |
|
|
|
command = msg.split(self.bot.command_prefix)[1] |
|
|
|
if command in settings["custom_commands"]["1"]: |
|
|
|
command_output = self._get_output(settings["custom_commands"]["1"][command]) |
|
|
|
return await channel.send(command_output) |
|
|
|
|
|
|
|
elif command in settings["custom_commands"]["2"]: |
|
|
|
command_output = self._get_output(settings["custom_commands"]["2"][command]) |
|
|
|
embed = self._cc_to_embed(command_output) |
|
|
|
return await channel.send(embed=embed) |
|
|
|
else: |
|
|
|
for command in settings["custom_commands"]["0"]: |
|
|
|
if msg == command: |
|
|
|
command_output = self._get_output(settings["custom_commands"]["0"][command]) |
|
|
|
return await channel.send(command_output) |
|
|
|
|
|
|
|
with db_session: |
|
|
|
if msg.startswith(self.bot.command_prefix): |
|
|
|
command_name = msg.split(self.bot.command_prefix)[1] |
|
|
|
command = CCCommands.get(name=command_name, guild_id=message.guild.id) |
|
|
|
try: |
|
|
|
if command.type == 1: |
|
|
|
output = self._get_output(command.output) |
|
|
|
return await channel.send(output) |
|
|
|
elif command.type == 2: |
|
|
|
embed = self._cc_to_embed(command.output) |
|
|
|
return await channel.send(embed=embed) |
|
|
|
except AttributeError: |
|
|
|
pass |
|
|
|
else: |
|
|
|
command = CCCommands.get(name=msg, guild_id=message.guild.id, type=0) |
|
|
|
if command: |
|
|
|
output = self._get_output(command.output) |
|
|
|
return await channel.send(output) |
|
|
|
|
|
|
|
@commands.guild_only() |
|
|
|
@commands.group(aliases=["cc"]) |
|
|
@@ -174,18 +176,16 @@ class CustomCommands: |
|
|
|
# Add an embed command called test3 with a title of "Title" and a description that is a markdown hyperlink to a youtube video, and the colour #deadbf |
|
|
|
;cc add embed test3 title "Title" description "[Click here for a rad video](https://www.youtube.com/watch?v=dQw4w9WgXcQ)" colour #deadbf |
|
|
|
|
|
|
|
Note: With custom commands, it is important to remember that "" is used to pass any text with spaces as one argument. If the output you want requires the use of these characters, surround your output with three speech quotes at either side instead. |
|
|
|
Note: With custom commands, it is important to remember that "" is used to pass any text with spaces as one argument. If the output you want requires the use of these characters, surround your output with three speech quotes at either side instead. |
|
|
|
""" |
|
|
|
command = command.lower() |
|
|
|
|
|
|
|
if command_type in ("0", "no_prefix", "no prefix"): |
|
|
|
command_type = "0" |
|
|
|
if len(output) == 1: |
|
|
|
output = output[0] |
|
|
|
command_type = 0 |
|
|
|
elif command_type in ("1", "prefix"): |
|
|
|
command_type = "1" |
|
|
|
if len(output) == 1: |
|
|
|
output = output[0] |
|
|
|
command_type = 1 |
|
|
|
elif command_type in ("2", "embed"): |
|
|
|
command_type = "2" |
|
|
|
command_type = 2 |
|
|
|
if len(output) < 2: |
|
|
|
raise roxbot.UserError(self.ERROR_EMBED_VALUE) |
|
|
|
try: |
|
|
@@ -195,26 +195,21 @@ class CustomCommands: |
|
|
|
else: |
|
|
|
raise roxbot.UserError(self.ERROR_INCORRECT_TYPE) |
|
|
|
|
|
|
|
settings = roxbot.guild_settings.get(ctx.guild) |
|
|
|
no_prefix_commands = settings["custom_commands"]["0"] |
|
|
|
prefix_commands = settings["custom_commands"]["1"] |
|
|
|
embed_commands = settings["custom_commands"]["2"] |
|
|
|
command = command.lower() |
|
|
|
with db_session: |
|
|
|
|
|
|
|
if ctx.message.mentions or ctx.message.mention_everyone or ctx.message.role_mentions: |
|
|
|
raise roxbot.UserError(self.ERROR_AT_MENTION) |
|
|
|
elif len(output) > 1800: |
|
|
|
raise roxbot.UserError(self.ERROR_OUTPUT_TOO_LONG) |
|
|
|
elif command in self.bot.all_commands.keys() and command_type == "1": |
|
|
|
raise roxbot.UserError(self.ERROR_COMMAND_EXISTS_INTERNAL) |
|
|
|
elif command in no_prefix_commands or command in prefix_commands or command in embed_commands: |
|
|
|
raise roxbot.UserError(self.ERROR_COMMAND_EXISTS) |
|
|
|
elif len(command.split(" ")) > 1 and command_type == "1": |
|
|
|
raise roxbot.UserError(self.ERROR_PREFIX_SPACE) |
|
|
|
|
|
|
|
settings["custom_commands"][command_type][command] = output |
|
|
|
settings.update(settings["custom_commands"], "custom_commands") |
|
|
|
return await ctx.send(self.OUTPUT_ADD.format(command, output)) |
|
|
|
if ctx.message.mentions or ctx.message.mention_everyone or ctx.message.role_mentions: |
|
|
|
raise roxbot.UserError(self.ERROR_AT_MENTION) |
|
|
|
elif len(output) > 1800: |
|
|
|
raise roxbot.UserError(self.ERROR_OUTPUT_TOO_LONG) |
|
|
|
elif command in self.bot.all_commands.keys() and command_type == 1: |
|
|
|
raise roxbot.UserError(self.ERROR_COMMAND_EXISTS_INTERNAL) |
|
|
|
elif select(c for c in CCCommands if c.name == command and c.guild_id == ctx.guild.id).exists(): |
|
|
|
raise roxbot.UserError(self.ERROR_COMMAND_EXISTS) |
|
|
|
elif len(command.split(" ")) > 1 and command_type == "1": |
|
|
|
raise roxbot.UserError(self.ERROR_PREFIX_SPACE) |
|
|
|
|
|
|
|
CCCommands(name=command, guild_id=ctx.guild.id, output=output, type=command_type) |
|
|
|
return await ctx.send(self.OUTPUT_ADD.format(command, output if len(output) > 1 else output[0])) |
|
|
|
|
|
|
|
@commands.has_permissions(manage_messages=True) |
|
|
|
@custom.command() |
|
|
@@ -228,41 +223,29 @@ class CustomCommands: |
|
|
|
For more examples of how to setup a custom command, look at the help for the ;custom add command. |
|
|
|
You cannot change the type of a command. If you want to change the type, remove the command and re-add it. |
|
|
|
""" |
|
|
|
settings = roxbot.guild_settings.get(ctx.guild) |
|
|
|
no_prefix_commands = settings["custom_commands"]["0"] |
|
|
|
prefix_commands = settings["custom_commands"]["1"] |
|
|
|
embed_commands = settings["custom_commands"]["2"] |
|
|
|
|
|
|
|
if ctx.message.mentions or ctx.message.mention_everyone or ctx.message.role_mentions: |
|
|
|
raise roxbot.UserError(self.ERROR_AT_MENTION) |
|
|
|
|
|
|
|
if command in no_prefix_commands: |
|
|
|
if len(edit) == 1: |
|
|
|
edit = edit[0] |
|
|
|
settings["custom_commands"]["0"][command] = edit |
|
|
|
settings.update(settings["custom_commands"], "custom_commands") |
|
|
|
return await ctx.send(self.OUTPUT_EDIT.format(command, edit)) |
|
|
|
|
|
|
|
elif command in prefix_commands: |
|
|
|
if len(edit) == 1: |
|
|
|
edit = edit[0] |
|
|
|
settings["custom_commands"]["1"][command] = edit |
|
|
|
settings.update(settings["custom_commands"], "custom_commands") |
|
|
|
return await ctx.send(self.OUTPUT_EDIT.format(command, edit)) |
|
|
|
|
|
|
|
elif command in embed_commands: |
|
|
|
if len(edit) < 2: |
|
|
|
raise roxbot.UserError(self.ERROR_EMBED_VALUE) |
|
|
|
try: |
|
|
|
edit = self._embed_parse_options(edit) |
|
|
|
except ValueError: |
|
|
|
raise roxbot.UserError(self.ERROR_OUTPUT_TOO_LONG) |
|
|
|
settings["custom_commands"]["2"][command] = edit |
|
|
|
settings.update(settings["custom_commands"], "custom_commands") |
|
|
|
return await ctx.send(self.OUTPUT_EDIT.format(command, edit)) |
|
|
|
|
|
|
|
else: |
|
|
|
raise roxbot.UserError(self.ERROR_COMMAND_NULL) |
|
|
|
if not edit: |
|
|
|
raise commands.BadArgument("Missing required argument: edit") |
|
|
|
|
|
|
|
with db_session: |
|
|
|
query = CCCommands.get(name=command.lower(), guild_id=ctx.guild.id) |
|
|
|
if query: |
|
|
|
if query.type == 2: |
|
|
|
if len(edit) < 2: |
|
|
|
raise roxbot.UserError(self.ERROR_EMBED_VALUE) |
|
|
|
try: |
|
|
|
edit = self._embed_parse_options(edit) |
|
|
|
query.output = edit |
|
|
|
return await ctx.send(self.OUTPUT_EDIT.format(command, edit)) |
|
|
|
except ValueError: |
|
|
|
raise roxbot.UserError(self.ERROR_OUTPUT_TOO_LONG) |
|
|
|
else: |
|
|
|
query.output = edit |
|
|
|
return await ctx.send(self.OUTPUT_EDIT.format(command, edit if len(edit) > 1 else edit[0])) |
|
|
|
else: |
|
|
|
raise roxbot.UserError(self.ERROR_COMMAND_NULL) |
|
|
|
|
|
|
|
@commands.has_permissions(manage_messages=True) |
|
|
|
@custom.command() |
|
|
@@ -273,61 +256,54 @@ class CustomCommands: |
|
|
|
# Remove custom command called "test" |
|
|
|
;cc remove test |
|
|
|
""" |
|
|
|
settings = roxbot.guild_settings.get(ctx.guild) |
|
|
|
|
|
|
|
command = command.lower() |
|
|
|
no_prefix_commands = settings["custom_commands"]["0"] |
|
|
|
prefix_commands = settings["custom_commands"]["1"] |
|
|
|
embed_commands = settings["custom_commands"]["2"] |
|
|
|
|
|
|
|
if command in no_prefix_commands: |
|
|
|
command_type = "0" |
|
|
|
elif command in prefix_commands: |
|
|
|
command_type = "1" |
|
|
|
elif command in embed_commands: |
|
|
|
command_type = "2" |
|
|
|
else: |
|
|
|
raise roxbot.UserError(self.ERROR_COMMAND_NULL) |
|
|
|
|
|
|
|
settings["custom_commands"][command_type].pop(command) |
|
|
|
settings.update(settings["custom_commands"], "custom_commands") |
|
|
|
return await ctx.send(self.OUTPUT_REMOVE.format(command)) |
|
|
|
with db_session: |
|
|
|
c = CCCommands.get(name=command, guild_id=ctx.guild.id) |
|
|
|
if c: |
|
|
|
c.delete() |
|
|
|
return await ctx.send(self.OUTPUT_REMOVE.format(command)) |
|
|
|
else: |
|
|
|
raise roxbot.UserError(self.ERROR_COMMAND_NULL) |
|
|
|
|
|
|
|
@custom.command() |
|
|
|
async def list(self, ctx, debug="0"): |
|
|
|
"""Lists all custom commands for this guild.""" |
|
|
|
if debug != "0" and debug != "1": |
|
|
|
debug = "0" |
|
|
|
settings = roxbot.guild_settings.get(ctx.guild) |
|
|
|
cc = settings["custom_commands"] |
|
|
|
no_prefix_commands = cc["0"] |
|
|
|
prefix_commands = {**cc["1"], **cc["2"]} |
|
|
|
|
|
|
|
paginator = commands.Paginator() |
|
|
|
paginator.add_line("Here is the list of Custom Commands...") |
|
|
|
with db_session: |
|
|
|
no_prefix_commands = select(c for c in CCCommands if c.type == 0 and c.guild_id == ctx.guild.id)[:] |
|
|
|
prefix_commands = select(c for c in CCCommands if c.type == 1 and c.guild_id == ctx.guild.id)[:] |
|
|
|
embed_commands = select(c for c in CCCommands if c.type == 3and c.guild_id == ctx.guild.id)[:] |
|
|
|
|
|
|
|
def add_commands(commands, paginator): |
|
|
|
if not commands: |
|
|
|
paginator.add_line("There are no commands setup.") |
|
|
|
else: |
|
|
|
for command in commands: |
|
|
|
output = command.name |
|
|
|
if debug == "1": |
|
|
|
output += " = '{}'".format(command.output if command.type == 2 else command.output[0]) |
|
|
|
paginator.add_line("- " + output) |
|
|
|
|
|
|
|
paginator = commands.Paginator(prefix="```md") |
|
|
|
paginator.add_line("__Here is the list of Custom Commands...__") |
|
|
|
paginator.add_line() |
|
|
|
|
|
|
|
paginator.add_line("Commands that require Prefix:") |
|
|
|
if not prefix_commands: |
|
|
|
paginator.add_line("There are no commands setup.") |
|
|
|
else: |
|
|
|
for command in prefix_commands: |
|
|
|
if debug == "1": |
|
|
|
command += " = '{}'".format(prefix_commands[command]) |
|
|
|
paginator.add_line("- " + command) |
|
|
|
paginator.add_line("__Prefix Commands (Non Embeds):__") |
|
|
|
add_commands(prefix_commands, paginator) |
|
|
|
paginator.add_line() |
|
|
|
|
|
|
|
paginator.add_line("__Prefix Commands (Embeds):__") |
|
|
|
add_commands(embed_commands, paginator) |
|
|
|
paginator.add_line() |
|
|
|
paginator.add_line("Commands that don't require prefix:") |
|
|
|
if not no_prefix_commands: |
|
|
|
paginator.add_line("There are no commands setup.") |
|
|
|
else: |
|
|
|
for command in no_prefix_commands: |
|
|
|
if debug == "1": |
|
|
|
command += " = '{}'".format(no_prefix_commands[command]) |
|
|
|
paginator.add_line("- " + command) |
|
|
|
|
|
|
|
output = paginator.pages |
|
|
|
for page in output: |
|
|
|
paginator.add_line("__Commands that don't require prefix:__") |
|
|
|
add_commands(no_prefix_commands, paginator) |
|
|
|
|
|
|
|
for page in paginator.pages: |
|
|
|
await ctx.send(page) |
|
|
|
|
|
|
|
|