Skip to content
This repository has been archived by the owner on Nov 30, 2020. It is now read-only.

Add role management command with show, add, and remove sub-commands #8

Merged
merged 6 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions source/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

Written by Tiger Sachse.
"""

from plugins import (
user_count,
help_menu,
poll,
aerospace,
garage_status,
sponge,
roles,
corgi
)

Expand All @@ -20,9 +22,9 @@
help_menu.COMMAND : help_menu.command_help_menu,
poll.COMMAND : poll.command_poll,
garage_status.COMMAND : garage_status.command_garage_status,
sponge.COMMAND : sponge.command_sponge,
corgi.COMMAND : corgi.command_corgi,

sponge.COMMAND : sponge.command_sponge,
roles.COMMAND : roles.command_roles,
corgi.COMMAND : corgi.command_corgi,
}

INLINES = {
Expand Down
10 changes: 8 additions & 2 deletions source/plugins/help_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ async def command_help_menu(client, message):
)

embedded_message.add_field(
name="!corgi",
value="Posts a corgi picture FOR FREE :)",
name="!roles [sub-command]",
value="Manage your roles. Run `!roles help` for more info",
inline=False
)

embedded_message.add_field(
name="!corgi",
value="Posts a corgi picture FOR FREE :)",
inline=False
)

await client.send_message(message.channel, response, embed=embedded_message)
119 changes: 119 additions & 0 deletions source/plugins/roles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
"""Command to show the available roles of a user"""

from discord.utils import get

COMMAND = "roles"
EXCLUDED_ROLES = ['Admin', 'Moderator',
'Teaching Assistant', 'TA', '@everyone', 'Suspended', 'suspended']


async def command_roles(client, message):
try:
cmd = parse_command(message.content)
await handle_command(client, message, cmd)
except Exception as error:
await client.send_message(message.channel, str(error))


async def handle_command(client, message, command):
sub_command = command['subcommand']
if sub_command == 'help':
await command_roles_help(client, message)
elif sub_command == 'show':
await show_all_server_roles(client, message)
elif sub_command == 'add':
await add_roles_to_user(client, message, command['roles'])
elif sub_command == 'remove':
await remove_roles_from_user(client, message, command['roles'])
else:
raise Exception(
'Invalid sub-command given. Run `!roles help` for usage details')


async def show_all_server_roles(client, message):
role_names = get_all_server_role_names(client)
resp = '\n'.join(role_names)
await client.send_message(message.channel, resp)


async def add_roles_to_user(client, message, role_names):
check_if_role_names_are_available(client, role_names)

roles = get_roles_from_role_names(client, role_names)

await client.add_roles(message.author, *roles)

resp = 'Added roles `{}` to user {}'.format(
' '.join(role_names), create_user_mention(message.author))
await client.send_message(message.channel, resp)


async def remove_roles_from_user(client, message, role_names):
check_if_role_names_are_available(client, role_names)

roles = get_roles_from_role_names(client, role_names)

await client.remove_roles(message.author, *roles)

resp = 'Removed roles `{}` from user {}'.format(
' '.join(role_names), create_user_mention(message.author))
await client.send_message(message.channel, resp)


async def command_roles_help(client, message):
help_message = """
Available sub-commands for roles:
**show**:
displays the available roles in the server
**add [roles]**:
gives the user the roles specified
**remove [roles]**:
removes the user from the roles specified
**help**:
displays this message
"""
await client.send_message(message.channel, help_message)


def check_if_role_names_are_available(client, role_names):
available_roles = get_all_server_role_names(client)

for rn in role_names:
if rn not in available_roles:
raise Exception(
'{} is not an available role. Run `!roles show` to view available roles'.format(rn))


def create_user_mention(user):
return '<@{}>'.format(user.id)


def get_roles_from_role_names(client, role_names):
return [get_server_role_by_name(client, rn) for rn in role_names]


def parse_command(message):
parts = message.split(' ')
if len(parts) <= 1:
raise Exception(
'Roles sub-command not given. Run `!roles help` for usage details')

return {
'subcommand': parts[1],
'roles': parts[2:]
}


def get_server_role_by_name(client, role_name):
server = get_server(client)
return get(server.roles, name=role_name)


def get_all_server_role_names(client):
server = get_server(client)

return [r.name for r in server.roles if r.name not in EXCLUDED_ROLES]


def get_server(client):
return next(iter(client.servers))