2016-07-09 13:27:19 +12:00
|
|
|
from discord.ext import commands
|
|
|
|
from .utils import checks
|
2016-07-10 03:17:54 +12:00
|
|
|
from .utils import config
|
2016-07-16 02:58:10 +12:00
|
|
|
import discord
|
2016-07-17 00:27:10 +12:00
|
|
|
import re
|
2016-07-16 02:58:10 +12:00
|
|
|
|
2016-08-13 05:37:29 +12:00
|
|
|
valid_perms = [p for p in dir(discord.Permissions) if isinstance(getattr(discord.Permissions, p), property)]
|
2016-07-16 09:39:26 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
class Mod:
|
2016-07-09 13:59:10 +12:00
|
|
|
"""Commands that can be used by a or an admin, depending on the command"""
|
2016-07-16 09:39:26 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
2016-08-13 05:37:29 +12:00
|
|
|
|
2016-08-12 07:00:24 +12:00
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
|
|
|
@checks.customPermsOrRole(kick_members=True)
|
2016-08-12 07:04:25 +12:00
|
|
|
async def alerts(self, ctx, channel: discord.Channel):
|
2016-08-12 07:00:24 +12:00
|
|
|
"""This command is used to set a channel as the server's 'notifications' channel
|
|
|
|
Any notifications (like someone going live on Twitch, or Picarto) will go to that channel"""
|
|
|
|
server_alerts = config.getContent('server_alerts') or {}
|
2016-08-12 07:02:47 +12:00
|
|
|
server_alerts[ctx.message.server.id] = channel.id
|
|
|
|
await self.bot.say("I have just changed this server's 'notifications' channel"
|
2016-08-12 07:33:19 +12:00
|
|
|
"\nAll notifications will now go to `{}`".format(channel))
|
2016-08-13 05:37:29 +12:00
|
|
|
|
2016-08-02 04:10:20 +12:00
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
2016-08-02 04:18:31 +12:00
|
|
|
@checks.customPermsOrRole(kick_members=True)
|
2016-08-13 05:37:29 +12:00
|
|
|
async def usernotify(self, ctx, on_off: str):
|
2016-08-02 04:10:20 +12:00
|
|
|
"""This command can be used to set whether or not you want user notificaitons to show
|
|
|
|
This will save what channel you run this command in, that will be the channel used to send the notification to
|
|
|
|
Provide on, yes, or true to set it on; otherwise it will be turned off"""
|
2016-08-13 05:37:29 +12:00
|
|
|
on_off = ctx.message.channel.id if re.search("(on|yes|true)", on_off.lower()) else None
|
2016-08-02 04:10:20 +12:00
|
|
|
notifications = config.getContent('user_notifications') or {}
|
|
|
|
notifications[ctx.message.server.id] = on_off
|
2016-08-13 05:37:29 +12:00
|
|
|
config.saveContent('user_notifications', notifications)
|
2016-08-12 07:33:19 +12:00
|
|
|
fmt = "notify" if on_off else "not notify"
|
2016-08-02 04:10:20 +12:00
|
|
|
await self.bot.say("This server will now {} if someone has joined or left".format(fmt))
|
2016-08-13 05:37:29 +12:00
|
|
|
|
2016-07-16 23:42:44 +12:00
|
|
|
@commands.group(pass_context=True, no_pm=True)
|
2016-07-10 07:39:32 +12:00
|
|
|
async def nsfw(self, ctx):
|
2016-07-10 07:40:51 +12:00
|
|
|
"""Handles adding or removing a channel as a nsfw channel"""
|
2016-07-10 07:38:48 +12:00
|
|
|
if ctx.invoked_subcommand is None:
|
|
|
|
await self.bot.say('Invalid subcommand passed: {0.subcommand_passed}'.format(ctx))
|
2016-07-16 09:39:26 +12:00
|
|
|
|
2016-07-16 23:42:44 +12:00
|
|
|
@nsfw.command(name="add", pass_context=True, no_pm=True)
|
2016-07-31 00:30:28 +12:00
|
|
|
@checks.customPermsOrRole(kick_members=True)
|
2016-07-10 07:38:48 +12:00
|
|
|
async def nsfw_add(self, ctx):
|
2016-07-10 06:27:14 +12:00
|
|
|
"""Registers this channel as a 'nsfw' channel"""
|
2016-08-10 14:13:53 +12:00
|
|
|
nsfw_channels = config.getContent('nsfw_channels') or {}
|
2016-07-18 03:17:47 +12:00
|
|
|
if ctx.message.channel.id in nsfw_channels:
|
2016-07-10 03:26:13 +12:00
|
|
|
await self.bot.say("This channel is already registered as 'nsfw'!")
|
2016-07-18 03:17:47 +12:00
|
|
|
else:
|
|
|
|
nsfw_channels.append(ctx.message.channel.id)
|
2016-07-29 11:16:42 +12:00
|
|
|
config.saveContent('nsfw_channels', nsfw_channels)
|
2016-07-29 04:59:12 +12:00
|
|
|
await self.bot.say("This channel has just been registered as 'nsfw'! Have fun you naughties ;)")
|
2016-07-16 09:39:26 +12:00
|
|
|
|
2016-07-18 09:10:12 +12:00
|
|
|
@nsfw.command(name="remove", aliases=["delete"], pass_context=True, no_pm=True)
|
2016-07-31 00:30:28 +12:00
|
|
|
@checks.customPermsOrRole(kick_members=True)
|
2016-07-18 09:10:12 +12:00
|
|
|
async def nsfw_remove(self, ctx):
|
2016-07-10 04:28:18 +12:00
|
|
|
"""Removes this channel as a 'nsfw' channel"""
|
2016-08-10 14:13:53 +12:00
|
|
|
nsfw_channels = config.getContent('nsfw_channels') or {}
|
2016-07-18 09:10:12 +12:00
|
|
|
if ctx.message.channel.id not in nsfw_channels:
|
2016-07-10 04:25:28 +12:00
|
|
|
await self.bot.say("This channel is not registered as a ''nsfw' channel!")
|
2016-07-18 03:17:47 +12:00
|
|
|
else:
|
|
|
|
nsfw_channels.remove(ctx.message.channel.id)
|
2016-07-29 11:16:42 +12:00
|
|
|
config.saveContent('nsfw_channels', nsfw_channels)
|
2016-07-29 04:59:12 +12:00
|
|
|
await self.bot.say("This channel has just been unregistered as a nsfw channel")
|
2016-07-16 09:39:26 +12:00
|
|
|
|
2016-07-16 23:42:44 +12:00
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
2016-07-31 00:30:28 +12:00
|
|
|
@checks.customPermsOrRole(kick_members=True)
|
2016-07-29 04:59:12 +12:00
|
|
|
async def say(self, ctx, *, msg: str):
|
2016-07-10 07:14:38 +12:00
|
|
|
"""Tells the bot to repeat what you say"""
|
|
|
|
await self.bot.say(msg)
|
|
|
|
await self.bot.delete_message(ctx.message)
|
2016-07-16 09:39:26 +12:00
|
|
|
|
2016-07-16 23:42:44 +12:00
|
|
|
@commands.group(pass_context=True, invoke_without_command=True, no_pm=True)
|
2016-07-31 00:30:28 +12:00
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
2016-08-13 05:37:29 +12:00
|
|
|
async def perms(self, ctx, *, command: str = None):
|
2016-07-16 10:59:15 +12:00
|
|
|
"""This command can be used to print the current allowed permissions on a specific command
|
|
|
|
This supports groups as well as subcommands; pass no argument to print a list of available permissions"""
|
2016-07-31 14:20:38 +12:00
|
|
|
if command is None:
|
2016-08-13 05:37:29 +12:00
|
|
|
await self.bot.say(
|
|
|
|
"Valid permissions are: ```\n{}```".format("\n".join("{}".format(i) for i in valid_perms)))
|
2016-07-16 07:16:10 +12:00
|
|
|
return
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2016-07-29 04:59:12 +12:00
|
|
|
custom_perms = config.getContent('custom_permissions') or {}
|
2016-07-18 03:17:47 +12:00
|
|
|
server_perms = custom_perms.get(ctx.message.server.id)
|
|
|
|
if server_perms is None:
|
|
|
|
await self.bot.say("There are no custom permissions setup on this server yet!")
|
2016-07-16 01:56:13 +12:00
|
|
|
return
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-31 00:55:26 +12:00
|
|
|
perms_value = server_perms.get(command)
|
|
|
|
if perms_value is None:
|
2016-07-18 03:17:47 +12:00
|
|
|
await self.bot.say("That command has no custom permissions setup on it!")
|
|
|
|
else:
|
2016-07-31 00:55:26 +12:00
|
|
|
permissions = discord.Permissions(perms_value)
|
2016-07-31 12:20:55 +12:00
|
|
|
needed_perm = [perm[0] for perm in permissions if perm[1]][0]
|
2016-07-18 09:10:12 +12:00
|
|
|
await self.bot.say("You need to have the permission `{}` "
|
2016-07-31 00:55:26 +12:00
|
|
|
"to use the command `{}` in this server".format(needed_perm, command))
|
2016-07-16 09:39:26 +12:00
|
|
|
|
2016-07-16 23:42:44 +12:00
|
|
|
@perms.command(name="add", aliases=["setup,create"], pass_context=True, no_pm=True)
|
2016-07-17 00:01:32 +12:00
|
|
|
@commands.has_permissions(manage_server=True)
|
2016-07-16 10:18:04 +12:00
|
|
|
async def add_perms(self, ctx, *msg: str):
|
2016-07-16 11:30:46 +12:00
|
|
|
"""Sets up custom permissions on the provided command
|
2016-07-17 00:04:24 +12:00
|
|
|
Format must be 'perms add <command> <permission>'
|
|
|
|
If you want to open the command to everyone, provide 'none' as the permission"""
|
2016-07-18 09:10:12 +12:00
|
|
|
command = " ".join(msg[0:len(msg) - 1])
|
|
|
|
permissions = msg[len(msg) - 1]
|
2016-07-31 12:20:55 +12:00
|
|
|
|
|
|
|
# If a user can run the command, they have to have send_messages permissions; so use this as the base
|
2016-07-18 08:24:13 +12:00
|
|
|
if permissions.lower() == "none":
|
2016-07-18 08:56:29 +12:00
|
|
|
permissions = "send_messages"
|
2016-07-31 12:20:55 +12:00
|
|
|
|
|
|
|
# Convert the string to an int value of the permissions obj, based on the required permission
|
2016-07-30 23:58:01 +12:00
|
|
|
perm_obj = discord.Permissions.none()
|
2016-07-31 12:20:55 +12:00
|
|
|
setattr(perm_obj, permissions, True)
|
2016-07-30 23:58:01 +12:00
|
|
|
perm_value = perm_obj.value
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-30 23:58:01 +12:00
|
|
|
cmd = None
|
2016-07-31 00:39:10 +12:00
|
|
|
for part in msg[0:len(msg) - 1]:
|
2016-07-16 10:41:16 +12:00
|
|
|
try:
|
2016-07-30 23:58:01 +12:00
|
|
|
if cmd is None:
|
|
|
|
cmd = self.bot.commands.get(part)
|
|
|
|
else:
|
|
|
|
cmd = cmd.commands.get(part)
|
|
|
|
except AttributeError:
|
2016-07-16 10:41:16 +12:00
|
|
|
break
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-30 23:58:01 +12:00
|
|
|
if cmd is None:
|
2016-07-31 12:20:55 +12:00
|
|
|
await self.bot.say(
|
|
|
|
"That command does not exist! You can't have custom permissions on a non-existant command....")
|
2016-07-30 23:58:01 +12:00
|
|
|
return
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2016-07-16 10:18:04 +12:00
|
|
|
for check in cmd.checks:
|
2016-07-18 09:10:12 +12:00
|
|
|
if "isOwner" == check.__name__ or re.search("has_permissions", str(check)) is not None:
|
2016-07-16 07:16:10 +12:00
|
|
|
await self.bot.say("This command cannot have custom permissions setup!")
|
|
|
|
return
|
2016-07-16 09:39:26 +12:00
|
|
|
|
2016-07-30 23:58:01 +12:00
|
|
|
if getattr(discord.Permissions, permissions, None) is None:
|
2016-08-01 07:11:05 +12:00
|
|
|
await self.bot.say("{} does not appear to be a valid permission! Valid permissions are: ```\n{}```"
|
2016-07-16 09:39:26 +12:00
|
|
|
.format(permissions, "\n".join(valid_perms)))
|
2016-07-18 09:10:12 +12:00
|
|
|
return
|
2016-08-15 06:53:45 +12:00
|
|
|
|
|
|
|
|
2016-07-30 23:58:01 +12:00
|
|
|
custom_perms = config.getContent('custom_permissions') or {}
|
|
|
|
server_perms = custom_perms.get(ctx.message.server.id) or {}
|
2016-08-15 06:53:45 +12:00
|
|
|
server_perms[cmd.qualified_name] = perm_value
|
2016-07-30 23:58:01 +12:00
|
|
|
custom_perms[ctx.message.server.id] = server_perms
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-30 23:58:01 +12:00
|
|
|
config.saveContent('custom_permissions', custom_perms)
|
|
|
|
await self.bot.say("I have just added your custom permissions; "
|
|
|
|
"you now need to have `{}` permissions to use the command `{}`".format(permissions, command))
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2016-07-16 23:59:28 +12:00
|
|
|
@perms.command(name="remove", aliases=["delete"], pass_context=True, no_pm=True)
|
2016-07-17 00:01:32 +12:00
|
|
|
@commands.has_permissions(manage_server=True)
|
2016-07-16 23:59:28 +12:00
|
|
|
async def remove_perms(self, ctx, *command: str):
|
2016-07-17 00:04:24 +12:00
|
|
|
"""Removes the custom permissions setup on the command specified"""
|
2016-07-16 23:59:28 +12:00
|
|
|
cmd = " ".join(command)
|
2016-07-30 23:58:01 +12:00
|
|
|
custom_perms = config.getContent('custom_permissions') or {}
|
2016-08-10 14:13:53 +12:00
|
|
|
server_perms = custom_perms.get(ctx.message.server.id) or {}
|
2016-07-18 03:17:47 +12:00
|
|
|
if server_perms is None:
|
|
|
|
await self.bot.say("There are no custom permissions setup on this server yet!")
|
|
|
|
return
|
2016-07-18 05:48:50 +12:00
|
|
|
command_perms = server_perms.get(cmd)
|
2016-07-18 03:17:47 +12:00
|
|
|
if command_perms is None:
|
2016-07-16 23:59:28 +12:00
|
|
|
await self.bot.say("You do not have custom permissions setup on this command yet!")
|
|
|
|
return
|
2016-07-18 05:48:50 +12:00
|
|
|
del custom_perms[ctx.message.server.id][cmd]
|
2016-07-30 23:58:01 +12:00
|
|
|
config.saveContent('custom_permissions', custom_perms)
|
|
|
|
await self.bot.say("I have just removed the custom permissions for {}!".format(cmd))
|
2016-08-13 05:37:29 +12:00
|
|
|
|
2016-08-08 07:26:44 +12:00
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
|
|
|
@checks.customPermsOrRole(manage_messages=True)
|
2016-08-13 05:37:29 +12:00
|
|
|
async def purge(self, ctx, limit: int = 100):
|
2016-08-08 07:26:44 +12:00
|
|
|
"""This command is used to a purge a number of messages from the channel"""
|
2016-08-13 05:37:29 +12:00
|
|
|
await self.bot.purge_from(ctx.message.channel, limit=limit)
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-31 07:11:56 +12:00
|
|
|
@commands.group(aliases=['rule'], pass_context=True, no_pm=True, invoke_without_command=True)
|
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
|
|
|
async def rules(self, ctx):
|
|
|
|
"""This command can be used to view the current rules on the server"""
|
|
|
|
rules = config.getContent('rules') or {}
|
|
|
|
server_rules = rules.get(ctx.message.server.id)
|
2016-07-31 07:18:49 +12:00
|
|
|
if server_rules is None or len(server_rules) == 0:
|
2016-07-31 07:11:56 +12:00
|
|
|
await self.bot.say("This server currently has no rules on it! I see you like to live dangerously...")
|
|
|
|
return
|
2016-07-31 12:20:55 +12:00
|
|
|
fmt = "\n".join("{}) {}".format(num + 1, rule) for num, rule in enumerate(server_rules))
|
2016-08-01 07:11:05 +12:00
|
|
|
await self.bot.say('```\n{}```'.format(fmt))
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-31 07:11:56 +12:00
|
|
|
@rules.command(name='add', aliases=['create'], pass_context=True, no_pm=True)
|
|
|
|
@checks.customPermsOrRole(manage_server=True)
|
2016-07-31 07:12:35 +12:00
|
|
|
async def rules_add(self, ctx, *, rule: str):
|
2016-07-31 07:11:56 +12:00
|
|
|
"""Adds a rule to this server's rules"""
|
|
|
|
rules = config.getContent('rules') or {}
|
|
|
|
server_rules = rules.get(ctx.message.server.id) or []
|
|
|
|
server_rules.append(rule)
|
|
|
|
rules[ctx.message.server.id] = server_rules
|
2016-07-31 12:20:55 +12:00
|
|
|
config.saveContent('rules', rules)
|
2016-07-31 07:11:56 +12:00
|
|
|
await self.bot.say("I have just saved your new rule, use the rules command to view this server's current rules")
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-31 07:11:56 +12:00
|
|
|
@rules.command(name='remove', aliases=['delete'], pass_context=True, no_pm=True)
|
|
|
|
@checks.customPermsOrRole(manage_server=True)
|
2016-08-13 05:37:29 +12:00
|
|
|
async def rules_delete(self, ctx, rule: int = None):
|
2016-07-31 07:20:31 +12:00
|
|
|
"""Removes one of the rules from the list of this server's rules
|
|
|
|
Provide a number to delete that rule; if no number is provided
|
|
|
|
I'll print your current rules and ask for a number"""
|
2016-07-31 07:11:56 +12:00
|
|
|
rules = config.getContent('rules') or {}
|
|
|
|
server_rules = rules.get(ctx.message.server.id)
|
2016-07-31 07:18:49 +12:00
|
|
|
if server_rules is None or len(server_rules) == 0:
|
2016-07-31 12:20:55 +12:00
|
|
|
await self.bot.say(
|
|
|
|
"This server currently has no rules on it! Can't remove something that doesn't exist bro")
|
2016-07-31 07:11:56 +12:00
|
|
|
return
|
2016-07-31 12:20:55 +12:00
|
|
|
list_rules = "\n".join("{}) {}".format(num + 1, rule) for num, rule in enumerate(server_rules))
|
|
|
|
|
2016-07-31 07:11:56 +12:00
|
|
|
if rule is None:
|
2016-08-01 07:11:05 +12:00
|
|
|
await self.bot.say("Your rules are:\n```\n{}```Please provide the rule number"
|
2016-07-31 12:20:55 +12:00
|
|
|
"you would like to remove (just the number)".format(list_rules))
|
|
|
|
|
|
|
|
msg = await self.bot.wait_for_message(timeout=60.0, author=ctx.message.author, channel=ctx.message.channel,
|
|
|
|
check=lambda m: m.content.isdigit())
|
2016-07-31 07:17:36 +12:00
|
|
|
if msg is None:
|
|
|
|
await self.bot.say("You took too long...it's just a number, seriously? Try typing a bit quicker")
|
|
|
|
return
|
2016-07-31 12:20:55 +12:00
|
|
|
del server_rules[int(msg.content) - 1]
|
2016-07-31 07:17:36 +12:00
|
|
|
rules[ctx.message.server.id] = server_rules
|
2016-07-31 12:20:55 +12:00
|
|
|
config.saveContent('rules', rules)
|
|
|
|
|
2016-07-31 07:11:56 +12:00
|
|
|
try:
|
2016-07-31 12:20:55 +12:00
|
|
|
del server_rules[rule - 1]
|
2016-07-31 07:11:56 +12:00
|
|
|
rules[ctx.message.server.id] = server_rules
|
2016-07-31 12:20:55 +12:00
|
|
|
config.saveContent('rules', rules)
|
2016-07-31 07:16:58 +12:00
|
|
|
await self.bot.say("I have just removed that rule from your list of rules!")
|
2016-07-31 07:11:56 +12:00
|
|
|
except IndexError:
|
2016-07-31 12:20:55 +12:00
|
|
|
await self.bot.say("That is not a valid rule number, try running the command again. "
|
2016-08-01 07:11:05 +12:00
|
|
|
"Your current rules are:\n```\n{}```".format(list_rules))
|
2016-07-31 12:20:55 +12:00
|
|
|
|
|
|
|
|
2016-07-09 13:31:18 +12:00
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Mod(bot))
|