Browse Source

Customcommands cog converted over to new settings.

tags/v1.5.0
Roxie Gibson 6 years ago
parent
commit
a3aa2558f2
2 changed files with 37 additions and 36 deletions
  1. +34
    -34
      Roxbot/cogs/customcommands.py
  2. +3
    -2
      main.py

+ 34
- 34
Roxbot/cogs/customcommands.py View File

import discord import discord
from Roxbot import checks, load_config from Roxbot import checks, load_config
from discord.ext.commands import group from discord.ext.commands import group
from Roxbot.settings.guild_settings import ServerConfig
from Roxbot.settings import guild_settings




def blacklisted(user): def blacklisted(user):
with open("settings/blacklist.txt", "r") as fp:
with open("Roxbot/blacklist.txt", "r") as fp:
for line in fp.readlines(): for line in fp.readlines():
if user.id+"\n" == line: if user.id+"\n" == line:
return True return True
class CustomCommands(): class CustomCommands():
def __init__(self, bot_client): def __init__(self, bot_client):
self.bot = bot_client self.bot = bot_client
self.con = ServerConfig()
self.servers = self.con.servers


async def on_message(self, message): async def on_message(self, message):
if blacklisted(message.author) or type(message.channel) != discord.TextChannel:
return
settings = guild_settings.get(message.guild)
msg = message.content.lower() msg = message.content.lower()
channel = message.channel channel = message.channel
server = str(message.guild.id)

if blacklisted(message.author) or type(message.channel) != discord.TextChannel:
return
if message.author == self.bot.user: if message.author == self.bot.user:
return return

if msg.startswith(self.bot.command_prefix): if msg.startswith(self.bot.command_prefix):
if msg.split(self.bot.command_prefix)[1] in self.servers[server]["custom_commands"]["1"]:
return await channel.send(self.servers[server]["custom_commands"]["1"][msg.split(self.bot.command_prefix)[1]])
if msg.split(self.bot.command_prefix)[1] in settings.custom_commands["1"]:
return await channel.send(settings.custom_commands["1"][msg.split(self.bot.command_prefix)[1]])
else: else:
for command in self.servers[server]["custom_commands"]["0"]:
for command in settings.custom_commands["0"]:
if msg == command: if msg == command:
return await channel.send(self.servers[server]["custom_commands"]["0"][command])
return await channel.send(settings.custom_commands["0"][command])


@group(pass_context=True, aliases=["cc"]) @group(pass_context=True, aliases=["cc"])
@checks.is_owner_or_admin() @checks.is_owner_or_admin()
@custom.command(pass_context=True) @custom.command(pass_context=True)
async def add(self, ctx, command, output, prefix_required = "0"): async def add(self, ctx, command, output, prefix_required = "0"):
"Adds a custom command to the list of custom commands." "Adds a custom command to the list of custom commands."
self.servers = self.con.load_config()
settings = guild_settings.get(ctx.guild)
command = command.lower() command = command.lower()
output = output output = output
zero = self.servers[str(ctx.guild.id)]["custom_commands"]["0"]
one = self.servers[str(ctx.guild.id)]["custom_commands"]["1"]
zero = settings.custom_commands["0"]
one = settings.custom_commands["1"]


if ctx.message.mentions or ctx.message.mention_everyone or ctx.message.role_mentions: if ctx.message.mentions or ctx.message.mention_everyone or ctx.message.role_mentions:
return await ctx.send("Custom Commands cannot mention people/roles/everyone.") return await ctx.send("Custom Commands cannot mention people/roles/everyone.")
elif len(command.split(" ")) > 1 and prefix_required == "1": elif len(command.split(" ")) > 1 and prefix_required == "1":
return await ctx.send("Custom commands with a prefix can only be one word with no spaces.") return await ctx.send("Custom commands with a prefix can only be one word with no spaces.")


self.servers[str(ctx.guild.id)]["custom_commands"][prefix_required][command] = output
self.con.update_config(self.servers)
settings.custom_commands[prefix_required][command] = output
settings.update(settings.custom_commands, "custom_commands")
return await ctx.send("{} has been added with the output: '{}'".format(command, output)) return await ctx.send("{} has been added with the output: '{}'".format(command, output))


@custom.command(pass_context=True) @custom.command(pass_context=True)
async def edit(self, ctx, command, edit): async def edit(self, ctx, command, edit):
"Edits an existing custom command." "Edits an existing custom command."
self.servers = self.con.load_config()
zero = self.servers[str(ctx.guild.id)]["custom_commands"]["0"]
one = self.servers[str(ctx.guild.id)]["custom_commands"]["1"]
settings = guild_settings.get(ctx.guild)
zero = settings.custom_commands["0"]
one = settings.custom_commands["1"]


if ctx.message.mentions or ctx.message.mention_everyone or ctx.message.role_mentions: if ctx.message.mentions or ctx.message.mention_everyone or ctx.message.role_mentions:
return await ctx.send("Custom Commands cannot mention people/roles/everyone.") return await ctx.send("Custom Commands cannot mention people/roles/everyone.")


if command in zero: if command in zero:
self.servers[str(ctx.guild.id)]["custom_commands"]["0"][command] = edit
self.con.update_config(self.servers)
settings.custom_commands["0"][command] = edit
settings.update(settings.custom_commands, "custom_commands")
return await ctx.send("Edit made. {} now outputs {}".format(command, edit)) return await ctx.send("Edit made. {} now outputs {}".format(command, edit))
elif command in one: elif command in one:
self.servers[str(ctx.guild.id)]["custom_commands"]["1"][command] = edit
self.con.update_config(self.servers)
settings.custom_commands["1"][command] = edit
settings.update(settings.custom_commands, "custom_commands")
return await ctx.send("Edit made. {} now outputs {}".format(command, edit)) return await ctx.send("Edit made. {} now outputs {}".format(command, edit))
else: else:
return await ctx.send("That Custom Command doesn't exist.") return await ctx.send("That Custom Command doesn't exist.")
@custom.command(pass_context=True) @custom.command(pass_context=True)
async def remove(self, ctx, command): async def remove(self, ctx, command):
"Removes a custom command." "Removes a custom command."
self.servers = self.con.load_config()
settings = guild_settings.get(ctx.guild)
command = command.lower() command = command.lower()
if command in self.servers[str(ctx.guild.id)]["custom_commands"]["1"]:
self.servers[str(ctx.guild.id)]["custom_commands"]["1"].pop(command)
self.con.update_config(self.servers)
if command in settings.custom_commands["1"]:
settings.custom_commands["1"].pop(command)
settings.update(settings.custom_commands, "custom_commands")
return await ctx.send("Removed {} custom command".format(command)) return await ctx.send("Removed {} custom command".format(command))
elif command in self.servers[str(ctx.guild.id)]["custom_commands"]["0"]:
self.servers[str(ctx.guild.id)]["custom_commands"]["0"].pop(command)
self.con.update_config(self.servers)
elif command in settings.custom_commands["0"]:
settings.custom_commands["0"].pop(command)
settings.update(settings.custom_commands, "custom_commands")
return await ctx.send("Removed {} custom command".format(command)) return await ctx.send("Removed {} custom command".format(command))
else: else:
return await ctx.send("Custom Command doesn't exist.") return await ctx.send("Custom Command doesn't exist.")
"Lists all custom commands for this server." "Lists all custom commands for this server."
if debug != "0" and debug != "1": if debug != "0" and debug != "1":
debug = "0" debug = "0"
self.servers = self.con.load_config()
l = self.servers[str(ctx.guild.id)]["custom_commands"]
settings = guild_settings.get(ctx.guild)
l = settings.custom_commands
listzero = "" listzero = ""
listone = "" listone = ""


for command in l["0"]: for command in l["0"]:
if debug == "1": if debug == "1":
command += command + " - {}".format(l["0"][command])
command += " - {}".format(l["0"][command])
listzero = listzero + "- " + command + "\n" listzero = listzero + "- " + command + "\n"
for command in l["1"]: for command in l["1"]:
if debug == "1": if debug == "1":
command += command + " - {}".format(l["1"][command])
command += " - {}".format(l["1"][command])
listone = listone + "- " + command + "\n" listone = listone + "- " + command + "\n"
if not listone: if not listone:
listone = "There are no commands setup.\n" listone = "There are no commands setup.\n"

+ 3
- 2
main.py View File



# Sets up Logging that discord.py does on its own # Sets up Logging that discord.py does on its own
logger = logging.getLogger('discord') logger = logging.getLogger('discord')
logger.setLevel(logging.WARN)
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w') handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler) logger.addHandler(handler)


start_time = time.time() start_time = time.time()
bot.load_extension("Roxbot.settings.settings") bot.load_extension("Roxbot.settings.settings")
#bot.load_extension("Roxbot.err_handle")
bot.load_extension("Roxbot.err_handle")
bot.load_extension("Roxbot.cogs.customcommands")
bot.run(load_config.token) bot.run(load_config.token)

Loading…
Cancel
Save