2016-07-09 13:27:19 +12:00
|
|
|
from discord.ext import commands
|
2017-02-13 10:39:57 +13:00
|
|
|
|
|
|
|
from . import utils
|
2016-09-29 12:39:34 +13:00
|
|
|
|
2016-07-16 02:58:10 +12:00
|
|
|
import discord
|
2016-07-17 00:27:10 +12:00
|
|
|
import re
|
2016-08-26 17:12:54 +12:00
|
|
|
import asyncio
|
2016-09-29 12:39:34 +13:00
|
|
|
import rethinkdb as r
|
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-09-29 12:39:34 +13:00
|
|
|
def find_command(self, command):
|
|
|
|
# This method ensures the command given is valid. We need to loop through commands
|
|
|
|
# As self.bot.commands only includes parent commands
|
|
|
|
# So we are splitting the command in parts, looping through the commands
|
|
|
|
# And getting the subcommand based on the next part
|
|
|
|
# If we try to access commands of a command that isn't a group
|
|
|
|
# We'll hit an AttributeError, meaning an invalid command was given
|
|
|
|
# If we loop through and don't find anything, cmd will still be None
|
|
|
|
# And we'll report an invalid was given as well
|
|
|
|
cmd = None
|
|
|
|
|
|
|
|
for part in command.split():
|
|
|
|
try:
|
|
|
|
if cmd is None:
|
|
|
|
cmd = self.bot.commands.get(part)
|
|
|
|
else:
|
|
|
|
cmd = cmd.commands.get(part)
|
|
|
|
except AttributeError:
|
|
|
|
cmd = None
|
|
|
|
break
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
2016-12-15 10:52:11 +13:00
|
|
|
@commands.command(pass_context=True, no_pm=True, aliases=['nick'])
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(kick_members=True)
|
2016-12-15 10:52:11 +13:00
|
|
|
async def nickname(self, ctx, *, name=None):
|
|
|
|
"""Used to set the nickname for Bonfire (provide no nickname and it will reset)
|
|
|
|
|
|
|
|
EXAMPLE: !nick Music Bot
|
|
|
|
RESULT: My nickname is now music bot"""
|
|
|
|
await self.bot.change_nickname(ctx.message.server.me, name)
|
|
|
|
await self.bot.say("\N{OK HAND SIGN}")
|
|
|
|
|
2016-11-29 21:05:22 +13:00
|
|
|
@commands.command(no_pm=True)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(kick_members=True)
|
2016-11-29 21:05:22 +13:00
|
|
|
async def kick(self, member: discord.Member):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Used to kick a member from this server
|
|
|
|
|
|
|
|
EXAMPLE: !kick @Member
|
|
|
|
RESULT: They're kicked from the server?"""
|
2016-10-10 11:19:20 +13:00
|
|
|
try:
|
|
|
|
await self.bot.kick(member)
|
|
|
|
await self.bot.say("\N{OK HAND SIGN}")
|
|
|
|
except discord.Forbidden:
|
|
|
|
await self.bot.say("But I can't, muh permissions >:c")
|
|
|
|
|
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(ban_members=True)
|
2016-10-10 11:19:20 +13:00
|
|
|
async def unban(self, ctx, member_id: int):
|
|
|
|
"""Used to unban a member from this server
|
|
|
|
Due to the fact that I cannot find a user without being in a server with them
|
2016-11-29 17:55:55 +13:00
|
|
|
only the ID should be provided
|
|
|
|
|
|
|
|
EXAMPLE: !unban 353217589321750912
|
|
|
|
RESULT: That dude be unbanned"""
|
2016-10-10 11:19:20 +13:00
|
|
|
|
|
|
|
# Lets only accept an int for this method, in order to ensure only an ID is provided
|
|
|
|
# Due to that though, we need to ensure a string is passed as the member's ID
|
|
|
|
member = discord.Object(id=str(member_id))
|
|
|
|
try:
|
|
|
|
await self.bot.unban(ctx.message.server, member)
|
|
|
|
await self.bot.say("\N{OK HAND SIGN}")
|
|
|
|
except discord.Forbidden:
|
|
|
|
await self.bot.say("But I can't, muh permissions >:c")
|
|
|
|
except discord.HTTPException:
|
|
|
|
await self.bot.say("Sorry, I failed to unban that user!")
|
|
|
|
|
2016-10-10 10:56:47 +13:00
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(ban_members=True)
|
2016-10-10 10:56:47 +13:00
|
|
|
async def ban(self, ctx, *, member):
|
|
|
|
"""Used to ban a member
|
|
|
|
This can be used to ban someone preemptively as well.
|
2016-11-29 17:55:55 +13:00
|
|
|
Provide the ID of the user and this should ban them without them being in the server
|
|
|
|
|
|
|
|
EXAMPLE: !ban 531251325312
|
|
|
|
RESULT: That dude be banned"""
|
2016-10-10 10:56:47 +13:00
|
|
|
|
|
|
|
# Lets first check if a user ID was provided, as that will be the easiest case to ban
|
|
|
|
if member.isdigit():
|
|
|
|
# First convert it to a discord object based on the ID that was given
|
|
|
|
member = discord.Object(id=member)
|
|
|
|
# Next, to ban from the server the API takes a server obejct and uses that ID
|
|
|
|
# So set "this" server as the member's server. This creates the "fake" member we need
|
|
|
|
member.server = ctx.message.server
|
|
|
|
else:
|
|
|
|
# If no ID was provided, lets try to convert what was given using the internal coverter
|
|
|
|
converter = commands.converter.UserConverter(ctx, member)
|
|
|
|
try:
|
|
|
|
member = converter.convert()
|
|
|
|
except commands.converter.BadArgument:
|
2016-10-13 10:54:30 +13:00
|
|
|
await self.bot.say(
|
2016-11-29 21:05:22 +13:00
|
|
|
'{} does not appear to be a valid member. If this member is not in this server, please provide '
|
|
|
|
'their ID'.format(member))
|
2016-10-10 10:56:47 +13:00
|
|
|
return
|
|
|
|
# Now lets try actually banning the member we've been given
|
|
|
|
try:
|
|
|
|
await self.bot.ban(member)
|
|
|
|
await self.bot.say("\N{OK HAND SIGN}")
|
2016-10-10 11:19:20 +13:00
|
|
|
except discord.Forbidden:
|
|
|
|
await self.bot.say("But I can't, muh permissions >:c")
|
2016-11-29 21:05:22 +13:00
|
|
|
except discord.HTTPException:
|
|
|
|
await self.bot.say("Sorry, I failed to ban that user!")
|
2016-10-10 10:56:47 +13:00
|
|
|
|
2017-03-17 18:21:30 +13:00
|
|
|
@commands.command(no_pm=True, aliases=['alerts'], pass_context=True)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(kick_members=True)
|
2017-03-17 18:21:30 +13:00
|
|
|
async def notifications(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
|
2016-11-29 17:55:55 +13:00
|
|
|
Any notifications (like someone going live on Twitch, or Picarto) will go to that channel
|
|
|
|
EXAMPLE: !alerts #alerts
|
|
|
|
RESULT: No more alerts spammed in #general!"""
|
2017-03-17 18:21:30 +13:00
|
|
|
if str(channel.type) != "text":
|
|
|
|
await self.bot.say("The notifications channel must be a text channel!")
|
|
|
|
return
|
|
|
|
|
|
|
|
key = ctx.message.server.id
|
|
|
|
entry = {'server_id': key,
|
|
|
|
'notification_channel': channel.id}
|
|
|
|
if not await utils.update_content('server_settings', entry, key):
|
|
|
|
await utils.add_content('server_settings', entry)
|
2016-08-12 07:02:47 +12:00
|
|
|
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)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(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
|
2016-11-29 17:55:55 +13:00
|
|
|
Provide on, yes, or true to set it on; otherwise it will be turned off
|
|
|
|
EXAMPLE: !usernotify on
|
|
|
|
RESULT: Annying join/leave notifications! Yay!"""
|
2016-08-18 08:46:20 +12:00
|
|
|
# Join/Leave notifications can be kept separate from normal alerts
|
|
|
|
# So we base this channel on it's own and not from alerts
|
2016-08-16 15:30:52 +12:00
|
|
|
# When mod logging becomes available, that will be kept to it's own channel if wanted as well
|
2017-03-17 18:21:30 +13:00
|
|
|
on_off = True if re.search("(on|yes|true)", on_off.lower()) else False
|
|
|
|
key = ctx.message.server.id
|
|
|
|
entry = {'server_id': key,
|
|
|
|
'join_leave': on_off}
|
|
|
|
if not await utils.update_content('server_settings', entry, key):
|
|
|
|
await utils.add_content('server_settings', entry)
|
|
|
|
|
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-10-07 14:16:25 +13:00
|
|
|
@commands.group(pass_context=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"""
|
2017-03-17 18:21:30 +13:00
|
|
|
pass
|
2016-07-16 09:39:26 +12:00
|
|
|
|
2016-10-07 14:16:25 +13:00
|
|
|
@nsfw.command(name="add", pass_context=True)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(kick_members=True)
|
2016-07-10 07:38:48 +12:00
|
|
|
async def nsfw_add(self, ctx):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Registers this channel as a 'nsfw' channel
|
|
|
|
EXAMPLE: !nsfw add
|
|
|
|
RESULT: ;)"""
|
2017-03-17 18:21:30 +13:00
|
|
|
key = ctx.message.server.id
|
|
|
|
entry = {'server_id': key,
|
|
|
|
'nsfw_channels': [ctx.message.channel.id]}
|
|
|
|
update = {'nsfw_channels': r.row['nsfw_channels'].append(ctx.message.channel.id)}
|
|
|
|
|
|
|
|
server_settings = await utils.get_content('server_settings', key)
|
|
|
|
if server_settings and 'nsfw_channels' in server_settings.keys():
|
|
|
|
await utils.update_content('server_settings', update, key)
|
|
|
|
elif server_settings:
|
|
|
|
await utils.update_content('server_settings', entry, key)
|
2016-09-29 12:39:34 +13:00
|
|
|
else:
|
2017-03-17 18:21:30 +13:00
|
|
|
await utils.add_content('server_settings', entry)
|
|
|
|
|
|
|
|
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-10-07 14:16:25 +13:00
|
|
|
@nsfw.command(name="remove", aliases=["delete"], pass_context=True)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(kick_members=True)
|
2016-07-18 09:10:12 +12:00
|
|
|
async def nsfw_remove(self, ctx):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Removes this channel as a 'nsfw' channel
|
|
|
|
EXAMPLE: !nsfw remove
|
|
|
|
RESULT: ;("""
|
2017-03-17 18:21:30 +13:00
|
|
|
|
|
|
|
key = ctx.message.server.id
|
|
|
|
server_settings = await utils.get_content('server_settings', key)
|
|
|
|
channel = ctx.message.channel.id
|
|
|
|
try:
|
|
|
|
channels = server_settings['nsfw_channels']
|
|
|
|
if channel in channels:
|
|
|
|
channels.remove(channel)
|
|
|
|
|
|
|
|
entry = {'nsfw_channels': channels}
|
|
|
|
await utils.update_content('server_settings', entry, key)
|
|
|
|
await self.bot.say("This channel has just been unregistered as a nsfw channel")
|
|
|
|
return
|
|
|
|
except (TypeError, IndexError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
await self.bot.say("This channel is not registered as a 'nsfw' channel!")
|
2016-07-16 09:39:26 +12:00
|
|
|
|
2016-10-07 14:16:25 +13:00
|
|
|
@commands.command(pass_context=True)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(kick_members=True)
|
2016-12-22 19:58:53 +13:00
|
|
|
async def say(self, ctx, *, msg: str):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Tells the bot to repeat what you say
|
|
|
|
|
|
|
|
EXAMPLE: !say I really like orange juice
|
|
|
|
RESULT: I really like orange juice"""
|
2016-08-21 11:54:51 +12:00
|
|
|
fmt = "\u200B{}".format(msg)
|
2016-08-26 16:50:09 +12:00
|
|
|
await self.bot.say(fmt)
|
2016-08-22 13:52:27 +12:00
|
|
|
try:
|
|
|
|
await self.bot.delete_message(ctx.message)
|
|
|
|
except:
|
|
|
|
pass
|
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)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(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
|
2016-11-29 17:55:55 +13:00
|
|
|
This supports groups as well as subcommands; pass no argument to print a list of available permissions
|
2016-11-29 21:05:22 +13:00
|
|
|
EXAMPLE: !perms help RESULT: Hopefully a result saying you just need send_messages permissions; otherwise lol
|
|
|
|
this server's admin doesn't like me """
|
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
|
|
|
|
2017-03-17 18:21:30 +13:00
|
|
|
cmd = utils.find_command(self.bot, command)
|
2016-08-31 10:33:46 +12:00
|
|
|
|
2016-08-29 07:39:28 +12:00
|
|
|
if cmd is None:
|
|
|
|
await self.bot.say("That is not a valid command!")
|
2016-07-16 01:56:13 +12:00
|
|
|
return
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2017-03-17 18:21:30 +13:00
|
|
|
server_settings = await utils.get_content('server_settings', ctx.message.server.id)
|
|
|
|
try:
|
|
|
|
server_perms = server_settings['permissions']
|
|
|
|
except (TypeError, IndexError, KeyError):
|
|
|
|
server_perms = {}
|
|
|
|
|
2016-08-29 07:39:28 +12:00
|
|
|
perms_value = server_perms.get(cmd.qualified_name)
|
2016-07-31 00:55:26 +12:00
|
|
|
if perms_value is None:
|
2016-08-29 07:39:28 +12:00
|
|
|
# If we don't find custom permissions, get the required permission for a command
|
2017-02-13 10:39:57 +13:00
|
|
|
# based on what we set in utils.custom_perms, if custom_perms isn't found, we'll get an IndexError
|
2016-08-29 07:39:28 +12:00
|
|
|
try:
|
2017-02-20 10:12:13 +13:00
|
|
|
custom_perms = [func for func in cmd.checks if "custom_perms" in func.__qualname__][0]
|
2016-08-29 07:40:34 +12:00
|
|
|
except IndexError:
|
2016-08-31 10:33:46 +12:00
|
|
|
# Loop through and check if there is a check called is_owner
|
2016-09-29 12:39:34 +13:00
|
|
|
# If we loop through and don't find one, this means that the only other choice is to be
|
2017-02-13 10:39:57 +13:00
|
|
|
# Able to manage the server (for the utils on perm commands)
|
2017-03-17 18:21:30 +13:00
|
|
|
for func in cmd.checks:
|
2016-08-29 07:39:28 +12:00
|
|
|
if "is_owner" in func.__qualname__:
|
|
|
|
await self.bot.say("You need to own the bot to run this command")
|
|
|
|
return
|
2016-08-31 10:33:46 +12:00
|
|
|
await self.bot.say(
|
|
|
|
"You are required to have `manage_server` permissions to run `{}`".format(cmd.qualified_name))
|
2016-08-29 07:39:28 +12:00
|
|
|
return
|
2016-08-31 10:33:46 +12:00
|
|
|
|
2016-09-29 12:39:34 +13:00
|
|
|
# Perms will be an attribute if custom_perms is found no matter what, so no need to check this
|
2016-08-29 07:39:28 +12:00
|
|
|
perms = "\n".join(attribute for attribute, setting in custom_perms.perms.items() if setting)
|
2016-08-31 10:33:46 +12:00
|
|
|
await self.bot.say(
|
|
|
|
"You are required to have `{}` permissions to run `{}`".format(perms, cmd.qualified_name))
|
2016-07-18 03:17:47 +12:00
|
|
|
else:
|
2016-08-16 15:30:52 +12:00
|
|
|
# Permissions are saved as bit values, so create an object based on that value
|
|
|
|
# Then check which permission is true, that is our required permission
|
|
|
|
# There's no need to check for errors here, as we ensure a permission is valid when adding it
|
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>'
|
2016-11-29 17:55:55 +13:00
|
|
|
If you want to open the command to everyone, provide 'none' as the permission
|
|
|
|
EXAMPLE: !perms add skip ban_members
|
|
|
|
RESULT: No more random people voting to skip a song"""
|
2016-08-18 08:46:20 +12:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# Since subcommands exist, base the last word in the list as the permission, and the rest of it as the command
|
2016-07-18 09:10:12 +12:00
|
|
|
command = " ".join(msg[0:len(msg) - 1])
|
2016-10-02 09:29:37 +13:00
|
|
|
try:
|
|
|
|
permissions = msg[len(msg) - 1]
|
|
|
|
except IndexError:
|
|
|
|
await self.bot.say("Please provide the permissions you want to setup, the format for this must be in:\n"
|
2016-10-03 07:08:20 +13:00
|
|
|
"`perms add <command> <permission>`")
|
|
|
|
return
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2017-03-17 18:21:30 +13:00
|
|
|
cmd = utils.find_command(self.bot, command)
|
|
|
|
|
|
|
|
if cmd is None:
|
|
|
|
await self.bot.say(
|
|
|
|
"That command does not exist! You can't have custom permissions on a non-existant command....")
|
|
|
|
return
|
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# If a user can run a 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
|
|
|
|
2016-09-29 12:39:34 +13:00
|
|
|
# Convert the string to an int value of the permissions object, based on the required permission
|
|
|
|
# If we hit an attribute error, that means the permission given was not correct
|
|
|
|
perm_obj = discord.Permissions.none()
|
|
|
|
try:
|
|
|
|
setattr(perm_obj, permissions, True)
|
|
|
|
except AttributeError:
|
2016-09-18 13:30:20 +12:00
|
|
|
await self.bot.say("{} does not appear to be a valid permission! Valid permissions are: ```\n{}```"
|
|
|
|
.format(permissions, "\n".join(valid_perms)))
|
|
|
|
return
|
2016-07-30 23:58:01 +12:00
|
|
|
perm_value = perm_obj.value
|
2016-08-18 08:46:20 +12:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# Two cases I use should never have custom permissions setup on them, is_owner for obvious reasons
|
2016-08-18 08:46:20 +12:00
|
|
|
# The other case is if I'm using the default has_permissions case
|
|
|
|
# Which means I do not want to check custom permissions at all
|
2016-08-16 15:30:52 +12:00
|
|
|
# Currently the second case is only on adding and removing permissions, to avoid abuse on these
|
2017-02-13 21:06:56 +13:00
|
|
|
for check in cmd.checks:
|
2017-03-17 18:21:30 +13:00
|
|
|
if "is_owner" == check.__name__ or "has_permissions" in str(check):
|
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
|
|
|
|
2017-03-17 18:21:30 +13:00
|
|
|
key = ctx.message.server.id
|
|
|
|
entry = {'server_id': key,
|
|
|
|
'permissions': {cmd.qualified_name: perm_value}}
|
2016-09-29 12:39:34 +13:00
|
|
|
|
2017-03-17 18:21:30 +13:00
|
|
|
if not await utils.update_content('server_settings', entry, key):
|
|
|
|
await utils.add_content('server_settings', entry)
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-30 23:58:01 +12:00
|
|
|
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-09-29 15:31:53 +13:00
|
|
|
async def remove_perms(self, ctx, *, command: str):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Removes the custom permissions setup on the command specified
|
|
|
|
|
|
|
|
EXAMPLE: !perms remove play
|
|
|
|
RESULT: Freedom!"""
|
2016-08-18 08:46:20 +12:00
|
|
|
|
2017-03-17 18:21:30 +13:00
|
|
|
cmd = utils.find_command(self.bot, command)
|
2016-08-16 15:30:52 +12:00
|
|
|
|
|
|
|
if cmd is None:
|
|
|
|
await self.bot.say(
|
|
|
|
"That command does not exist! You can't have custom permissions on a non-existant command....")
|
|
|
|
return
|
2016-08-18 08:46:20 +12:00
|
|
|
|
2017-03-17 18:21:30 +13:00
|
|
|
update = {'permissions': {cmd.qualified_name: None}}
|
|
|
|
await utils.update_content('server_settings', update, ctx.message.server.id)
|
2016-07-30 23:58:01 +12:00
|
|
|
await self.bot.say("I have just removed the custom permissions for {}!".format(cmd))
|
2016-08-13 05:37:29 +12:00
|
|
|
|
2016-09-01 07:10:33 +12:00
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(manage_server=True)
|
2016-09-01 07:10:33 +12:00
|
|
|
async def prefix(self, ctx, *, prefix: str):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""This command can be used to set a custom prefix per server
|
|
|
|
|
|
|
|
EXAMPLE: !prefix new_prefix
|
|
|
|
RESULT: You probably screwing it up and not realizing you now need to do new_prefixprefix"""
|
2017-03-17 18:21:30 +13:00
|
|
|
key = ctx.message.server.id
|
2016-10-13 10:49:52 +13:00
|
|
|
if prefix.lower().strip() == "none":
|
2016-09-29 12:39:34 +13:00
|
|
|
prefix = None
|
|
|
|
|
2017-03-17 18:21:30 +13:00
|
|
|
entry = {'server_id': key,
|
2016-09-29 12:39:34 +13:00
|
|
|
'prefix': prefix}
|
|
|
|
|
2017-03-17 18:21:30 +13:00
|
|
|
if not await utils.update_content('server_settings', entry, key):
|
|
|
|
await utils.add_content('server_settings', entry)
|
2016-10-13 10:52:36 +13:00
|
|
|
|
|
|
|
if prefix is None:
|
|
|
|
fmt = "I have just cleared your custom prefix, the default prefix will have to be used now"
|
|
|
|
else:
|
2016-10-13 10:54:30 +13:00
|
|
|
fmt = "I have just updated the prefix for this server; you now need to call commands with `{0}`. " \
|
|
|
|
"For example, you can call this command again with {0}prefix".format(prefix)
|
2016-10-13 10:52:36 +13:00
|
|
|
await self.bot.say(fmt)
|
2016-09-01 07:10:33 +12:00
|
|
|
|
2016-08-08 07:26:44 +12:00
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(manage_messages=True)
|
2016-08-13 05:37:29 +12:00
|
|
|
async def purge(self, ctx, limit: int = 100):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""This command is used to a purge a number of messages from the channel
|
|
|
|
|
|
|
|
EXAMPLE: !purge 50
|
|
|
|
RESULT: -50 messages in this channel"""
|
2016-08-28 07:36:07 +12:00
|
|
|
if not ctx.message.channel.permissions_for(ctx.message.server.me).manage_messages:
|
|
|
|
await self.bot.say("I do not have permission to delete messages...")
|
|
|
|
return
|
2017-01-22 11:58:23 +13:00
|
|
|
try:
|
|
|
|
await self.bot.purge_from(ctx.message.channel, limit=limit)
|
|
|
|
except discord.HTTPException:
|
2017-03-04 20:48:47 +13:00
|
|
|
await self.bot.send_message(ctx.message.channel, "Detected messages that are too far "
|
|
|
|
"back for me to delete; I can only bulk delete messages"
|
|
|
|
" that are under 14 days old.")
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-08-26 16:50:09 +12:00
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(manage_messages=True)
|
2017-03-17 18:21:30 +13:00
|
|
|
async def prune(self, ctx, limit=None):
|
2016-08-26 16:50:09 +12:00
|
|
|
"""This command can be used to prune messages from certain members
|
|
|
|
Mention any user you want to prune messages from; if no members are mentioned, the messages removed will be mine
|
2016-11-29 17:55:55 +13:00
|
|
|
If no limit is provided, then 100 will be used. This is also the max limit we can use
|
|
|
|
|
|
|
|
EXAMPLE: !prune 50
|
|
|
|
RESULT: 50 of my messages are removed from this channel"""
|
2016-08-26 16:50:09 +12:00
|
|
|
# We can only get logs from 100 messages at a time, so make sure we are not above that threshold
|
2017-03-07 11:11:49 +13:00
|
|
|
try:
|
|
|
|
# We may not have been passed a limit, and only mentions
|
|
|
|
# If this happens, the limit will be set to that first mention
|
|
|
|
limit = int(limit)
|
2017-03-07 11:15:48 +13:00
|
|
|
except (TypeError, ValueError):
|
2017-03-07 11:11:49 +13:00
|
|
|
limit = 100
|
|
|
|
|
2016-08-26 16:50:09 +12:00
|
|
|
if limit > 100:
|
|
|
|
limit = 100
|
2017-03-07 11:11:49 +13:00
|
|
|
if limit < 0:
|
|
|
|
await self.bot.say("Limit cannot be less than 0!")
|
|
|
|
return
|
2016-08-26 16:50:09 +12:00
|
|
|
|
|
|
|
# If no members are provided, assume we're trying to prune our own messages
|
|
|
|
members = ctx.message.mentions
|
2017-03-07 11:09:41 +13:00
|
|
|
roles = ctx.message.role_mentions
|
2016-08-26 16:50:09 +12:00
|
|
|
if len(members) == 0:
|
|
|
|
members = [ctx.message.server.me]
|
2017-03-07 11:09:41 +13:00
|
|
|
|
|
|
|
# Our check for if a message should be deleted
|
|
|
|
def check(m):
|
|
|
|
if m.author in members:
|
|
|
|
return True
|
|
|
|
if any(x in m.author.roles for x in roles):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-08-28 07:36:07 +12:00
|
|
|
# If we're not setting the user to the bot, then we're deleting someone elses messages
|
|
|
|
# To do so, we need manage_messages permission, so check if we have that
|
2017-03-07 11:13:22 +13:00
|
|
|
if not ctx.message.channel.permissions_for(ctx.message.server.me).manage_messages:
|
2016-08-28 07:36:07 +12:00
|
|
|
await self.bot.say("I do not have permission to delete messages...")
|
|
|
|
return
|
2016-08-26 16:50:09 +12:00
|
|
|
|
|
|
|
# Since logs_from will give us any message, not just the user's we need
|
|
|
|
# We'll increment count, and stop deleting messages if we hit the limit.
|
|
|
|
count = 0
|
2017-03-07 11:08:55 +13:00
|
|
|
async for msg in self.bot.logs_from(ctx.message.channel, before=ctx.message):
|
2017-03-07 11:09:41 +13:00
|
|
|
if check(msg):
|
2016-09-18 13:30:20 +12:00
|
|
|
try:
|
|
|
|
await self.bot.delete_message(msg)
|
|
|
|
count += 1
|
2017-03-07 11:09:41 +13:00
|
|
|
except:
|
2016-09-18 13:30:20 +12:00
|
|
|
pass
|
2016-08-26 16:50:09 +12:00
|
|
|
if count >= limit:
|
|
|
|
break
|
2016-08-26 17:12:54 +12:00
|
|
|
msg = await self.bot.say("{} messages succesfully deleted".format(count))
|
2017-03-07 11:08:55 +13:00
|
|
|
await asyncio.sleep(5)
|
2016-09-19 09:13:41 +12:00
|
|
|
try:
|
|
|
|
await self.bot.delete_message(msg)
|
2017-03-07 11:08:55 +13:00
|
|
|
await self.bot.delete_message(ctx.message)
|
2016-09-19 09:13:41 +12:00
|
|
|
except discord.NotFound:
|
|
|
|
pass
|
2016-08-26 16:50:09 +12:00
|
|
|
|
2016-07-31 07:11:56 +12:00
|
|
|
@commands.group(aliases=['rule'], pass_context=True, no_pm=True, invoke_without_command=True)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2016-09-29 12:39:34 +13:00
|
|
|
async def rules(self, ctx, rule: int = None):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""This command can be used to view the current rules on the server
|
|
|
|
|
|
|
|
EXAMPLE: !rules 5
|
|
|
|
RESULT: Rule 5 is printed"""
|
2017-03-17 18:21:30 +13:00
|
|
|
server_settings = await utils.get_content('server_settings', ctx.message.server.id)
|
|
|
|
rules = server_settings.get('rules')
|
|
|
|
|
|
|
|
if not rules or len(rules) == 0:
|
2016-09-29 15:31:53 +13:00
|
|
|
await self.bot.say("This server currently has no rules on it! I see you like to live dangerously...")
|
|
|
|
return
|
|
|
|
|
2016-09-29 12:39:34 +13:00
|
|
|
if rule is None:
|
2017-02-13 12:36:11 +13:00
|
|
|
try:
|
2017-02-13 12:58:27 +13:00
|
|
|
pages = utils.Pages(self.bot, message=ctx.message, entries=rules, per_page=5)
|
|
|
|
pages.title = "Rules for {}".format(ctx.message.server.name)
|
2017-02-13 12:36:11 +13:00
|
|
|
await pages.paginate()
|
|
|
|
except utils.CannotPaginate as e:
|
|
|
|
await self.bot.say(str(e))
|
2016-09-29 12:39:34 +13:00
|
|
|
else:
|
2016-09-29 15:31:53 +13:00
|
|
|
try:
|
|
|
|
fmt = rules[rule - 1]
|
|
|
|
except IndexError:
|
|
|
|
await self.bot.say("That rules does not exist.")
|
|
|
|
return
|
2016-09-29 12:39:34 +13:00
|
|
|
await self.bot.say("Rule {}: \"{}\"".format(rule, 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)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(manage_server=True)
|
2016-07-31 07:12:35 +12:00
|
|
|
async def rules_add(self, ctx, *, rule: str):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Adds a rule to this server's rules
|
|
|
|
|
|
|
|
EXAMPLE: !rules add No fun allowed in this server >:c
|
|
|
|
RESULT: No more fun...unless they break the rules!"""
|
2017-03-17 18:21:30 +13:00
|
|
|
key = ctx.message.server.id
|
|
|
|
entry = {'server_id': key,
|
2016-09-29 12:39:34 +13:00
|
|
|
'rules': [rule]}
|
2016-11-29 21:35:27 +13:00
|
|
|
update = {'rules': r.row['rules'].append(rule)}
|
2017-03-17 18:21:30 +13:00
|
|
|
|
|
|
|
server_settings = await utils.get_content('server_settings', key)
|
|
|
|
if server_settings and 'rules' in server_settings.keys():
|
|
|
|
await utils.update_content('server_settings', update, key)
|
|
|
|
elif server_settings:
|
|
|
|
await utils.update_content('server_settings', entry, key)
|
|
|
|
else:
|
|
|
|
await utils.add_content('server_settings', entry)
|
2016-09-29 12:39:34 +13:00
|
|
|
|
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)
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(manage_server=True)
|
2016-09-29 12:39:34 +13:00
|
|
|
async def rules_delete(self, ctx, rule: int):
|
2016-07-31 07:20:31 +12:00
|
|
|
"""Removes one of the rules from the list of this server's rules
|
2016-11-29 17:55:55 +13:00
|
|
|
Provide a number to delete that rule
|
|
|
|
|
|
|
|
EXAMPLE: !rules delete 5
|
|
|
|
RESULT: Freedom from opression!"""
|
2016-09-29 12:39:34 +13:00
|
|
|
update = {'rules': r.row['rules'].delete_at(rule - 1)}
|
2017-03-17 18:21:30 +13:00
|
|
|
if not await utils.update_content('server_settings', update, ctx.message.server.id):
|
2016-09-29 12:39:34 +13:00
|
|
|
await self.bot.say("That is not a valid rule number, try running the command again.")
|
|
|
|
else:
|
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 12:20:55 +12:00
|
|
|
|
|
|
|
|
2016-07-09 13:31:18 +12:00
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Mod(bot))
|