mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 11:18:54 -05:00
186 lines
6.6 KiB
Python
186 lines
6.6 KiB
Python
from discord.ext import commands
|
|
from .utils.dataIO import dataIO
|
|
from .utils import checks
|
|
from __main__ import user_allowed
|
|
import os
|
|
import re
|
|
|
|
|
|
class CustomCommands:
|
|
"""Custom commands."""
|
|
|
|
def __init__(self, bot):
|
|
self.bot = bot
|
|
self.file_path = "data/customcom/commands.json"
|
|
self.c_commands = dataIO.load_json(self.file_path)
|
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
|
@checks.mod_or_permissions(administrator=True)
|
|
async def addcom(self, ctx, command : str, *, text):
|
|
"""Adds a custom command
|
|
|
|
Example:
|
|
!addcom yourcommand Text you want
|
|
"""
|
|
server = ctx.message.server
|
|
command = command.lower()
|
|
if command in self.bot.commands.keys():
|
|
await self.bot.say("That command is already a standard command.")
|
|
return
|
|
if not server.id in self.c_commands:
|
|
self.c_commands[server.id] = {}
|
|
cmdlist = self.c_commands[server.id]
|
|
if command not in cmdlist:
|
|
cmdlist[command] = text
|
|
self.c_commands[server.id] = cmdlist
|
|
dataIO.save_json(self.file_path, self.c_commands)
|
|
await self.bot.say("Custom command successfully added.")
|
|
else:
|
|
await self.bot.say("This command already exists. Use editcom to edit it.")
|
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
|
@checks.mod_or_permissions(administrator=True)
|
|
async def editcom(self, ctx, command : str, *, text):
|
|
"""Edits a custom command
|
|
|
|
Example:
|
|
!editcom yourcommand Text you want
|
|
"""
|
|
server = ctx.message.server
|
|
command = command.lower()
|
|
if server.id in self.c_commands:
|
|
cmdlist = self.c_commands[server.id]
|
|
if command in cmdlist:
|
|
cmdlist[command] = text
|
|
self.c_commands[server.id] = cmdlist
|
|
dataIO.save_json(self.file_path, self.c_commands)
|
|
await self.bot.say("Custom command successfully edited.")
|
|
else:
|
|
await self.bot.say("That command doesn't exist. Use addcom [command] [text]")
|
|
else:
|
|
await self.bot.say("There are no custom commands in this server. Use addcom [command] [text]")
|
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
|
@checks.mod_or_permissions(administrator=True)
|
|
async def delcom(self, ctx, command : str):
|
|
"""Deletes a custom command
|
|
|
|
Example:
|
|
!delcom yourcommand"""
|
|
server = ctx.message.server
|
|
command = command.lower()
|
|
if server.id in self.c_commands:
|
|
cmdlist = self.c_commands[server.id]
|
|
if command in cmdlist:
|
|
cmdlist.pop(command, None)
|
|
self.c_commands[server.id] = cmdlist
|
|
dataIO.save_json(self.file_path, self.c_commands)
|
|
await self.bot.say("Custom command successfully deleted.")
|
|
else:
|
|
await self.bot.say("That command doesn't exist.")
|
|
else:
|
|
await self.bot.say("There are no custom commands in this server. Use addcom [command] [text]")
|
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
|
async def customcommands(self, ctx):
|
|
"""Shows custom commands list"""
|
|
server = ctx.message.server
|
|
if server.id in self.c_commands:
|
|
cmdlist = self.c_commands[server.id]
|
|
if cmdlist:
|
|
i = 0
|
|
msg = ["```Custom commands:\n"]
|
|
for cmd in sorted([cmd for cmd in cmdlist.keys()]):
|
|
if len(msg[i]) + len(ctx.prefix) + len(cmd) + 5 > 2000:
|
|
msg[i] += "```"
|
|
i += 1
|
|
msg.append("``` {}{}\n".format(ctx.prefix, cmd))
|
|
else:
|
|
msg[i] += " {}{}\n".format(ctx.prefix, cmd)
|
|
msg[i] += "```"
|
|
for cmds in msg:
|
|
await self.bot.whisper(cmds)
|
|
else:
|
|
await self.bot.say("There are no custom commands in this server. Use addcom [command] [text]")
|
|
else:
|
|
await self.bot.say("There are no custom commands in this server. Use addcom [command] [text]")
|
|
|
|
async def checkCC(self, message):
|
|
if len(message.content) < 2 or message.channel.is_private:
|
|
return
|
|
|
|
msg = message.content
|
|
server = message.server
|
|
prefix = self.get_prefix(msg)
|
|
|
|
if not prefix:
|
|
return
|
|
|
|
if server.id in self.c_commands and user_allowed(message):
|
|
cmdlist = self.c_commands[server.id]
|
|
cmd = msg[len(prefix):]
|
|
if cmd in cmdlist.keys():
|
|
cmd = cmdlist[cmd]
|
|
cmd = self.format_cc(cmd, message)
|
|
await self.bot.send_message(message.channel, cmd)
|
|
elif cmd.lower() in cmdlist.keys():
|
|
cmd = cmdlist[cmd.lower()]
|
|
cmd = self.format_cc(cmd, message)
|
|
await self.bot.send_message(message.channel, cmd)
|
|
|
|
def get_prefix(self, msg):
|
|
for p in self.bot.command_prefix:
|
|
if msg.startswith(p):
|
|
return p
|
|
return False
|
|
|
|
def format_cc(self, command, message):
|
|
results = re.findall("\{([^}]+)\}", command)
|
|
for result in results:
|
|
param = self.transform_parameter(result, message)
|
|
command = command.replace("{" + result + "}", param)
|
|
return command
|
|
|
|
def transform_parameter(self, result, message):
|
|
"""
|
|
For security reasons only specific objects are allowed
|
|
Internals are ignored
|
|
"""
|
|
raw_result = "{" + result + "}"
|
|
objects = {
|
|
"message" : message,
|
|
"author" : message.author,
|
|
"channel" : message.channel,
|
|
"server" : message.server
|
|
}
|
|
if result in objects:
|
|
return str(objects[result])
|
|
try:
|
|
first, second = result.split(".")
|
|
except ValueError:
|
|
return raw_result
|
|
if first in objects and not second.startswith("_"):
|
|
first = objects[first]
|
|
else:
|
|
return raw_result
|
|
return str(getattr(first, second, raw_result))
|
|
|
|
|
|
def check_folders():
|
|
if not os.path.exists("data/customcom"):
|
|
print("Creating data/customcom folder...")
|
|
os.makedirs("data/customcom")
|
|
|
|
def check_files():
|
|
f = "data/customcom/commands.json"
|
|
if not dataIO.is_valid_json(f):
|
|
print("Creating empty commands.json...")
|
|
dataIO.save_json(f, {})
|
|
|
|
def setup(bot):
|
|
check_folders()
|
|
check_files()
|
|
n = CustomCommands(bot)
|
|
bot.add_listener(n.checkCC, "on_message")
|
|
bot.add_cog(n)
|