2016-08-01 03:25:17 +12:00
|
|
|
from discord.ext import commands
|
|
|
|
import discord
|
2017-03-08 11:35:30 +13:00
|
|
|
|
|
|
|
from . import utils
|
2016-08-16 15:30:52 +12:00
|
|
|
|
2016-08-01 03:25:17 +12:00
|
|
|
import re
|
2016-08-16 15:30:52 +12:00
|
|
|
import asyncio
|
2016-08-01 03:25:17 +12:00
|
|
|
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2016-08-01 03:25:17 +12:00
|
|
|
class Roles:
|
|
|
|
"""Class to handle management of roles on the server"""
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2016-08-01 03:25:17 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.group(aliases=['roles'], invoke_without_command=True)
|
|
|
|
@commands.guild_only()
|
2017-03-08 11:35:30 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2016-08-01 09:09:41 +12:00
|
|
|
async def role(self, ctx):
|
2016-08-01 09:26:54 +12:00
|
|
|
"""This command can be used to modify the roles on the server.
|
2016-11-29 17:55:55 +13:00
|
|
|
Pass no subcommands and this will print the roles currently available on this server
|
|
|
|
|
|
|
|
EXAMPLE: !role
|
|
|
|
RESULT: A list of all your roles"""
|
2016-08-16 15:30:52 +12:00
|
|
|
# Simply get a list of all roles in this server and send them
|
2017-03-09 09:50:29 +13:00
|
|
|
server_roles = [role.name for role in ctx.message.guild.roles if not role.is_default()]
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("Your server's roles are: ```\n{}```".format("\n".join(server_roles)))
|
2016-08-01 09:26:54 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@role.command(name='remove')
|
|
|
|
@commands.guild_only()
|
2017-03-08 11:35:30 +13:00
|
|
|
@utils.custom_perms(manage_roles=True)
|
2016-08-01 09:26:54 +12:00
|
|
|
async def remove_role(self, ctx):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Use this to remove roles from a number of members
|
|
|
|
|
|
|
|
EXAMPLE: !role remove @Jim @Bot @Joe
|
|
|
|
RESULT: A follow-along to remove the role(s) you want to, from these 3 members"""
|
2016-08-16 15:30:52 +12:00
|
|
|
# No use in running through everything if the bot cannot manage roles
|
2017-03-08 12:56:24 +13:00
|
|
|
if not ctx.message.guild.me.permissions_in(ctx.message.channel).manage_roles:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I can't manage roles in this server, do you not trust me? :c")
|
2016-08-16 15:30:52 +12:00
|
|
|
return
|
2017-03-08 11:35:30 +13:00
|
|
|
check = lambda m: m.author == ctx.message.author and m.channel == ctx.message.channel
|
2016-10-10 15:46:06 +13:00
|
|
|
|
2017-03-09 09:50:29 +13:00
|
|
|
server_roles = [role for role in ctx.message.guild.roles if not role.is_default()]
|
2016-08-16 15:30:52 +12:00
|
|
|
# First get the list of all mentioned users
|
2016-08-01 09:26:54 +12:00
|
|
|
members = ctx.message.mentions
|
2016-08-16 15:30:52 +12:00
|
|
|
# If no users are mentioned, ask the author for a list of the members they want to remove the role from
|
2016-08-01 09:26:54 +12:00
|
|
|
if len(members) == 0:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("Please provide the list of members you want to remove a role from")
|
2017-03-08 12:47:00 +13:00
|
|
|
try:
|
|
|
|
msg = await self.bot.wait_for('message', check=check, timeout=60)
|
|
|
|
except asyncio.TimeoutError:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("You took too long. I'm impatient, don't make me wait")
|
2016-08-01 09:26:54 +12:00
|
|
|
return
|
|
|
|
if len(msg.mentions) == 0:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I cannot remove a role from someone if you don't provide someone...")
|
2016-08-01 09:26:54 +12:00
|
|
|
return
|
2016-08-16 15:30:52 +12:00
|
|
|
# Override members if everything has gone alright, and then continue
|
2016-08-01 09:26:54 +12:00
|
|
|
members = msg.mentions
|
2016-10-10 15:46:06 +13:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# This allows the user to remove multiple roles from the list of users, if they want.
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("Alright, please provide the roles you would like to remove from this member. "
|
|
|
|
"Make sure the roles, if more than one is provided, are separate by commas. "
|
|
|
|
"Here is a list of this server's roles:"
|
|
|
|
"```\n{}```".format("\n".join([r.name for r in server_roles])))
|
2017-03-08 12:47:00 +13:00
|
|
|
try:
|
|
|
|
msg = await self.bot.wait_for('message', check=check, timeout=60)
|
|
|
|
except asyncio.TimeoutError:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("You took too long. I'm impatient, don't make me wait")
|
2016-08-01 09:26:54 +12:00
|
|
|
return
|
2016-10-10 15:46:06 +13:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# Split the content based on commas, using regex so we can split if a space was not provided or if it was
|
2016-08-01 09:26:54 +12:00
|
|
|
role_names = re.split(', ?', msg.content)
|
|
|
|
roles = []
|
2016-08-16 15:30:52 +12:00
|
|
|
# This loop is just to get the actual role objects based on the name
|
2016-08-01 09:26:54 +12:00
|
|
|
for role in role_names:
|
|
|
|
_role = discord.utils.get(server_roles, name=role)
|
|
|
|
if _role is not None:
|
|
|
|
roles.append(_role)
|
2016-10-10 15:46:06 +13:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# If no valid roles were given, let them know that and return
|
2016-08-01 09:52:29 +12:00
|
|
|
if len(roles) == 0:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("Please provide a valid role next time!")
|
2016-08-01 09:52:29 +12:00
|
|
|
return
|
2016-10-10 15:46:06 +13:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# Otherwise, remove the roles from each member given
|
2016-08-01 09:26:54 +12:00
|
|
|
for member in members:
|
2017-03-09 09:59:05 +13:00
|
|
|
await member.remove_roles(*roles)
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I have just removed the following roles:```\n{}``` from the following members:"
|
|
|
|
"```\n{}```".format("\n".join(role_names), "\n".join([m.display_name for m in members])))
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@role.command(name='add')
|
|
|
|
@commands.guild_only()
|
2017-03-08 11:35:30 +13:00
|
|
|
@utils.custom_perms(manage_roles=True)
|
2016-08-01 09:14:29 +12:00
|
|
|
async def add_role(self, ctx):
|
2016-08-01 08:30:55 +12:00
|
|
|
"""Use this to add a role to multiple members.
|
|
|
|
Provide the list of members, and I'll ask for the role
|
2016-11-29 17:55:55 +13:00
|
|
|
If no members are provided, I'll first ask for them
|
|
|
|
|
|
|
|
EXAMPLE: !role add @Bob @Joe @jim
|
|
|
|
RESULT: A follow along to add the roles you want to these 3"""
|
2016-08-16 15:30:52 +12:00
|
|
|
# No use in running through everything if the bot cannot manage roles
|
2017-03-08 12:56:24 +13:00
|
|
|
if not ctx.message.guild.me.permissions_in(ctx.message.channel).manage_roles:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I can't manage roles in this server, do you not trust me? :c")
|
2016-08-16 15:30:52 +12:00
|
|
|
return
|
2017-03-08 11:35:30 +13:00
|
|
|
check = lambda m: m.author == ctx.message.author and m.channel == ctx.message.channel
|
2016-10-10 15:46:06 +13:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# This is exactly the same as removing roles, except we call add_roles instead.
|
2017-03-09 09:50:29 +13:00
|
|
|
server_roles = [role for role in ctx.message.guild.roles if not role.is_default()]
|
2016-08-01 09:14:29 +12:00
|
|
|
members = ctx.message.mentions
|
|
|
|
if len(members) == 0:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("Please provide the list of members you want to add a role to")
|
2017-03-08 12:47:00 +13:00
|
|
|
try:
|
|
|
|
msg = await self.bot.wait_for('message', check=check, timeout=60)
|
|
|
|
except asyncio.TimeoutError:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("You took too long. I'm impatient, don't make me wait")
|
2016-08-01 08:30:55 +12:00
|
|
|
return
|
|
|
|
if len(msg.mentions) == 0:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I cannot add a role to someone if you don't provide someone...")
|
2016-08-01 08:30:55 +12:00
|
|
|
return
|
|
|
|
members = msg.mentions
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("Alright, please provide the roles you would like to add to this member. "
|
|
|
|
"Make sure the roles, if more than one is provided, are separate by commas. "
|
|
|
|
"Here is a list of this server's roles:"
|
|
|
|
"```\n{}```".format("\n".join([r.name for r in server_roles])))
|
2017-03-08 12:47:00 +13:00
|
|
|
try:
|
|
|
|
msg = await self.bot.wait_for('message', check=check, timeout=60)
|
|
|
|
except asyncio.TimeoutError:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("You took too long. I'm impatient, don't make me wait")
|
2016-08-01 08:30:55 +12:00
|
|
|
return
|
2016-08-01 09:06:46 +12:00
|
|
|
role_names = re.split(', ?', msg.content)
|
|
|
|
roles = []
|
|
|
|
for role in role_names:
|
2016-08-01 08:30:55 +12:00
|
|
|
_role = discord.utils.get(server_roles, name=role)
|
|
|
|
if _role is not None:
|
2016-08-01 09:06:46 +12:00
|
|
|
roles.append(_role)
|
|
|
|
|
2016-08-01 09:52:29 +12:00
|
|
|
if len(roles) == 0:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("Please provide a valid role next time!")
|
2016-08-01 09:52:29 +12:00
|
|
|
return
|
|
|
|
|
2016-08-01 09:06:46 +12:00
|
|
|
for member in members:
|
2017-03-08 11:35:30 +13:00
|
|
|
await member.add_roles(*roles)
|
|
|
|
await ctx.send("I have just added the following roles:```\n{}``` to the following members:"
|
|
|
|
"```\n{}```".format("\n".join(role_names), "\n".join([m.display_name for m in members])))
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@role.command(name='delete')
|
|
|
|
@commands.guild_only()
|
2017-03-08 11:35:30 +13:00
|
|
|
@utils.custom_perms(manage_roles=True)
|
2016-08-01 09:36:24 +12:00
|
|
|
async def delete_role(self, ctx, *, role: discord.Role = None):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""This command can be used to delete one of the roles from the server
|
|
|
|
|
|
|
|
EXAMPLE: !role delete StupidRole
|
|
|
|
RESULT: No more role called StupidRole"""
|
2016-08-16 15:30:52 +12:00
|
|
|
# No use in running through everything if the bot cannot manage roles
|
2017-03-08 12:56:24 +13:00
|
|
|
if not ctx.message.guild.me.permissions_in(ctx.message.channel).manage_roles:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I can't delete roles in this server, do you not trust me? :c")
|
2016-08-16 15:30:52 +12:00
|
|
|
return
|
2016-10-10 15:46:06 +13:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# If no role was given, get the current roles on the server and ask which ones they'd like to remove
|
2016-08-01 07:53:21 +12:00
|
|
|
if role is None:
|
2017-03-09 09:50:29 +13:00
|
|
|
server_roles = [role for role in ctx.message.guild.roles if not role.is_default()]
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send(
|
2016-08-01 09:06:46 +12:00
|
|
|
"Which role would you like to remove from the server? Here is a list of this server's roles:"
|
|
|
|
"```\n{}```".format("\n".join([r.name for r in server_roles])))
|
2016-10-10 15:46:06 +13:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# For this method we're only going to delete one role at a time
|
|
|
|
# This check attempts to find a role based on the content provided, if it can't find one it returns None
|
|
|
|
# We can use that fact to simply use just that as our check
|
2017-03-08 11:35:30 +13:00
|
|
|
def check(m):
|
|
|
|
if m.author == ctx.message.author and m.channel == ctx.message.channel:
|
|
|
|
return discord.utils.get(server_roles, name=m.content) is not None
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2017-03-08 12:47:00 +13:00
|
|
|
try:
|
|
|
|
msg = await self.bot.wait_for('message', timeout=60, check=check)
|
|
|
|
except asyncio.TimeoutError:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("You took too long. I'm impatient, don't make me wait")
|
2016-08-01 07:53:21 +12:00
|
|
|
return
|
2016-08-16 15:30:52 +12:00
|
|
|
# If we have gotten here, based on our previous check, we know that the content provided is a valid role.
|
|
|
|
# Due to that, no need for any error checking here
|
2016-08-01 07:57:52 +12:00
|
|
|
role = discord.utils.get(server_roles, name=msg.content)
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2017-03-08 11:35:30 +13:00
|
|
|
await role.delete()
|
|
|
|
await ctx.send("I have just removed the role {} from this server".format(role.name))
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@role.command(name='create')
|
|
|
|
@commands.guild_only()
|
2017-03-08 11:35:30 +13:00
|
|
|
@utils.custom_perms(manage_roles=True)
|
2016-08-01 03:25:17 +12:00
|
|
|
async def create_role(self, ctx):
|
2016-08-01 05:03:54 +12:00
|
|
|
"""This command can be used to create a new role for this server
|
|
|
|
A prompt will follow asking what settings you would like for this new role
|
2016-11-29 17:55:55 +13:00
|
|
|
I'll then ask if you'd like to set anyone to use this role
|
|
|
|
|
|
|
|
EXAMPLE: !role create
|
|
|
|
RESULT: A follow along in order to create a new role"""
|
2016-08-01 05:03:54 +12:00
|
|
|
# No use in running through everything if the bot cannot create the role
|
2017-03-08 12:56:24 +13:00
|
|
|
if not ctx.message.guild.me.permissions_in(ctx.message.channel).manage_roles:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I can't create roles in this server, do you not trust me? :c")
|
2016-08-01 05:03:54 +12:00
|
|
|
return
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2016-08-01 03:25:17 +12:00
|
|
|
# Save a couple variables that will be used repeatedly
|
|
|
|
author = ctx.message.author
|
2017-03-08 12:56:24 +13:00
|
|
|
server = ctx.message.guild
|
2016-08-01 03:25:17 +12:00
|
|
|
channel = ctx.message.channel
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2016-08-01 03:25:17 +12:00
|
|
|
# A couple checks that will be used in the wait_for_message's
|
2017-03-08 11:35:30 +13:00
|
|
|
def num_seperated_check(m):
|
|
|
|
if m.author == author and m.channel == channel:
|
|
|
|
return re.search("(\d(, ?| )?|[nN]one)", m.content) is not None
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def yes_no_check(m):
|
|
|
|
if m.author == author and m.channel == channel:
|
|
|
|
return re.search("(yes|no)", m.content.lower()) is not None
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def members_check(m):
|
|
|
|
if m.author == author and m.channel == channel:
|
|
|
|
return len(m.mentions) > 0
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
author_check = lambda m: m.author == author and m.channel == channel
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# Start the checks for the role, get the name of the role first
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send(
|
2016-08-01 09:06:46 +12:00
|
|
|
"Alright! I'm ready to create a new role, please respond with the name of the role you want to create")
|
2017-03-08 12:47:00 +13:00
|
|
|
try:
|
|
|
|
msg = await self.bot.wait_for('message', timeout=60.0, check=author_check)
|
|
|
|
except asyncio.TimeoutError:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("You took too long. I'm impatient, don't make me wait")
|
2016-08-01 03:25:17 +12:00
|
|
|
return
|
|
|
|
name = msg.content
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2016-08-01 03:25:17 +12:00
|
|
|
# Print a list of all the permissions available, then ask for which ones need to be active on this new role
|
2016-08-01 09:06:46 +12:00
|
|
|
all_perms = [p for p in dir(discord.Permissions) if isinstance(getattr(discord.Permissions, p), property)]
|
|
|
|
fmt = "\n".join("{}) {}".format(i, perm) for i, perm in enumerate(all_perms))
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("Sounds fancy! Here is a list of all the permissions available. Please respond with just "
|
|
|
|
"the numbers, seperated by commas, of the permissions you want this role to have.\n"
|
|
|
|
"```\n{}```".format(fmt))
|
2016-08-31 10:33:46 +12:00
|
|
|
# For this we're going to give a couple extra minutes before we timeout
|
|
|
|
# as it might take a bit to figure out which permissions they want
|
2017-03-08 12:47:00 +13:00
|
|
|
try:
|
|
|
|
msg = await self.bot.wait_for('message', timeout=180.0, check=num_seperated_check)
|
|
|
|
except asyncio.TimeoutError:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("You took too long. I'm impatient, don't make me wait")
|
2016-08-01 03:25:17 +12:00
|
|
|
return
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2016-08-01 03:25:17 +12:00
|
|
|
# Check if any integer's were provided that are within the length of the list of permissions
|
2016-08-01 09:06:46 +12:00
|
|
|
num_permissions = [int(i) for i in re.split(' ?,?', msg.content) if i.isdigit() and int(i) < len(all_perms)]
|
|
|
|
|
2016-08-01 03:25:17 +12:00
|
|
|
# Check if this role should be in a separate section on the sidebard, i.e. hoisted
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("Do you want this role to be in a separate section on the sidebar? (yes or no)")
|
2017-03-08 12:47:00 +13:00
|
|
|
try:
|
|
|
|
msg = await self.bot.wait_for('message', timeout=60.0, check=yes_no_check)
|
|
|
|
except asyncio.TimeoutError:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("You took too long. I'm impatient, don't make me wait")
|
2016-08-01 03:25:17 +12:00
|
|
|
return
|
|
|
|
hoist = True if msg.content.lower() == "yes" else False
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2016-08-01 03:25:17 +12:00
|
|
|
# Check if this role should be able to be mentioned
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("Do you want this role to be mentionable? (yes or no)")
|
2017-03-08 12:47:00 +13:00
|
|
|
try:
|
|
|
|
msg = await self.bot.wait_for('message', timeout=60.0, check=yes_no_check)
|
|
|
|
except asyncio.TimeoutError:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("You took too long. I'm impatient, don't make me wait")
|
2016-08-01 03:25:17 +12:00
|
|
|
return
|
|
|
|
mentionable = True if msg.content.lower() == "yes" else False
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2016-08-01 03:25:17 +12:00
|
|
|
# Ready to actually create the role
|
2016-08-16 15:30:52 +12:00
|
|
|
# First create a permissions object based on the numbers provided
|
2016-08-01 03:25:17 +12:00
|
|
|
perms = discord.Permissions.none()
|
|
|
|
for index in num_permissions:
|
|
|
|
setattr(perms, all_perms[index], True)
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2016-08-01 03:25:17 +12:00
|
|
|
payload = {
|
2016-08-01 09:06:46 +12:00
|
|
|
'name': name,
|
|
|
|
'permissions': perms,
|
|
|
|
'hoist': hoist,
|
|
|
|
'mentionable': mentionable
|
2016-08-01 03:25:17 +12:00
|
|
|
}
|
2016-08-16 15:30:52 +12:00
|
|
|
# Create the role, and wait a second, sometimes it goes too quickly and we get a role with 'new role' to print
|
2017-03-08 11:35:30 +13:00
|
|
|
role = await server.create_role(**payload)
|
2016-08-16 15:30:52 +12:00
|
|
|
await asyncio.sleep(1)
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("We did it! You just created the new role {}\nIf you want to add this role"
|
|
|
|
" to some people, mention them now".format(role.name))
|
2017-03-08 12:47:00 +13:00
|
|
|
try:
|
|
|
|
msg = await self.bot.wait_for('message', timeout=60.0, check=members_check)
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
# There's no need to mention the users, so don't send a failure message if they didn't, just return
|
2016-08-01 05:03:54 +12:00
|
|
|
return
|
2016-10-10 15:46:06 +13:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# Otherwise members were mentioned, add the new role to them now
|
2016-08-01 05:11:20 +12:00
|
|
|
for member in msg.mentions:
|
2017-03-08 11:35:30 +13:00
|
|
|
await member.add_roles(role)
|
2016-08-01 09:06:46 +12:00
|
|
|
|
2016-08-01 05:11:20 +12:00
|
|
|
fmt = "\n".join(m.display_name for m in msg.mentions)
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I have just added the role {} to: ```\n{}```".format(name, fmt))
|
2016-08-01 09:06:46 +12:00
|
|
|
|
|
|
|
|
2017-05-01 11:56:02 +12:00
|
|
|
@commands.group(invoke_without_command=True, aliases=['assign'])
|
|
|
|
@commands.guild_only()
|
|
|
|
@utils.custom_perms(manage_roles=True)
|
|
|
|
async def assigns(self, ctx, *role: discord.Role):
|
|
|
|
"""Adds the provided role(s) to the list of available self-assignable roles
|
|
|
|
|
|
|
|
EXAMPLE: !assigns Member NSFW
|
|
|
|
RESULT: Allows users to self-assign the roles Member, and NSFW"""
|
|
|
|
roles = [str(r.id) for r in role]
|
|
|
|
key = str(ctx.message.guild.id)
|
|
|
|
server_settings = await utils.get_content('server_settings', key)
|
|
|
|
|
|
|
|
if server_settings is None:
|
|
|
|
entry = {'server_id': key, 'self_assignable_roles': roles}
|
|
|
|
await utils.add_content('server_settings', entry)
|
|
|
|
else:
|
|
|
|
self_assignable_roles = server_settings.get('self_assignable_roles', [])
|
|
|
|
self_assignable_roles.extend(roles)
|
|
|
|
self_assignable_roles = list(set(self_assignable_roles))
|
|
|
|
update = {'self_assignable_roles': self_assignable_roles}
|
|
|
|
await utils.update_content('server_settings', update, key)
|
|
|
|
|
|
|
|
if len(roles) == 1:
|
|
|
|
fmt = "Successfully added {} as a self-assignable role".format(role[0].name)
|
|
|
|
else:
|
|
|
|
fmt = "Succesfully added the following roles as self-assignable:\n{}".format(
|
|
|
|
"\n".join(["**{}**".format(r.name) for r in role])
|
|
|
|
)
|
|
|
|
await ctx.send(fmt)
|
|
|
|
|
|
|
|
@assigns.command(name='remove')
|
|
|
|
@commands.guild_only()
|
|
|
|
@utils.custom_perms(manage_roles=True)
|
|
|
|
async def _remove_assigns(self, ctx, *role: discord.Role):
|
|
|
|
"""Removes the provided role(s) from the list of available self-assignable roles
|
|
|
|
|
|
|
|
EXAMPLE: !assigns remove Member NSFW
|
|
|
|
RESULT: Removes the ability for users to self-assign the roles Member, and NSFW"""
|
|
|
|
roles = [str(r.id) for r in role]
|
|
|
|
key = str(ctx.message.guild.id)
|
|
|
|
server_settings = await utils.get_content('server_settings', key)
|
|
|
|
|
|
|
|
if server_settings is None:
|
|
|
|
await ctx.send("There are no self-assignable roles on this server")
|
|
|
|
return
|
|
|
|
self_assignable_roles = server_settings.get('self_assignable_roles', [])
|
|
|
|
if len(self_assignable_roles) == 0:
|
|
|
|
await ctx.send("There are no self-assignable roles on this server")
|
|
|
|
return
|
|
|
|
|
|
|
|
fmt = ""
|
|
|
|
for r in role:
|
|
|
|
rid = str(r.id)
|
|
|
|
try:
|
|
|
|
self_assignable_roles.remove(rid)
|
|
|
|
except ValueError:
|
|
|
|
fmt += "\n{} is not a self-assignable role".format(r.name)
|
|
|
|
else:
|
|
|
|
fmt += "\n{} is no longer a self-assignable role".format(r.name)
|
|
|
|
|
|
|
|
update = {'self_assignable_roles': self_assignable_roles}
|
|
|
|
await utils.update_content('server_settings', update, key)
|
|
|
|
await ctx.send(fmt)
|
|
|
|
|
|
|
|
@assigns.command(name='me')
|
|
|
|
@commands.guild_only()
|
|
|
|
@commands.check(utils.is_owner)
|
|
|
|
async def _me_assigns(self, ctx, *role: discord.Role):
|
|
|
|
"""Assigns the provided role(s) to you, if they can be assigned
|
|
|
|
|
|
|
|
EXAMPLE: !assign me Member
|
|
|
|
RESULT: You now have the Member role"""
|
|
|
|
if not ctx.message.guild.me.guild_permissions.manage_roles:
|
|
|
|
await ctx.send("I need to have manage roles permissions to assign roles")
|
|
|
|
return
|
|
|
|
|
|
|
|
author = ctx.message.author
|
|
|
|
key = str(ctx.message.guild.id)
|
|
|
|
server_settings = await utils.get_content('server_settings', key)
|
|
|
|
|
|
|
|
if server_settings is None:
|
|
|
|
await ctx.send("There are no self-assignable roles on this server")
|
|
|
|
return
|
|
|
|
self_assignable_roles = server_settings.get('self_assignable_roles', [])
|
|
|
|
if len(self_assignable_roles) == 0:
|
|
|
|
await ctx.send("There are no self-assignable roles on this server")
|
|
|
|
return
|
|
|
|
|
|
|
|
fmt = ""
|
|
|
|
roles = [r for r in role if str(r.id) in self_assignable_roles]
|
|
|
|
fmt += "\n".join(["Successfully added {}".format(r.name)
|
|
|
|
if str(r.id) in self_assignable_roles else
|
|
|
|
"{} is not available to be self-assigned".format(r.name)
|
|
|
|
for r in role])
|
|
|
|
|
|
|
|
try:
|
|
|
|
await author.add_roles(*roles)
|
|
|
|
await ctx.send(fmt)
|
|
|
|
except discord.HTTPException:
|
|
|
|
await ctx.send("I cannot assign roles to you {}".format(author.mention))
|
|
|
|
|
|
|
|
|
2016-08-01 03:25:17 +12:00
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Roles(bot))
|