From 086972318323fb2e4740933f8a6cdccc8790c9bb Mon Sep 17 00:00:00 2001 From: electro7 Date: Sun, 27 May 2018 09:52:08 +0200 Subject: Initial commit --- main.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 main.py (limited to 'main.py') diff --git a/main.py b/main.py new file mode 100644 index 0000000..c44cb5d --- /dev/null +++ b/main.py @@ -0,0 +1,58 @@ +from typing import List, Set + +from discord import Role, Member, Embed, Color, Guild +from discord.ext import commands +from discord.ext.commands import Bot, Context as CommandContext, Paginator + +from config import Config + +config = Config.load('config.yaml') + +bot = Bot( + command_prefix=commands.when_mentioned_or('!'), +) + + +def check_is_mentor(author: Member, role: Role): + role_name: str = role.name.lower() + if not role_name.endswith(' lernender'): + return False + mentor_role_name = role_name.replace(' lernender', ' mentor') + return len([role for role in author.roles if role.name.lower() == mentor_role_name]) > 0 + + +def get_members_with_role(role: Role): + guild: Guild = role.guild + members: List[Member] = guild.members + return [member for member in members if role in member.roles] + + +@bot.command(name='listusers', pass_context=True) +async def list_users(ctx: CommandContext, *, role: Role): + author: Member = ctx.author + roles: List[Role] = author.roles + role_ids: Set[int] = set([role.id for role in roles]) + admin: bool = len(role_ids.intersection(config.admin_roles)) + if not admin and not check_is_mentor(author, role): + return await ctx.send( + embed=Embed( + title='Missing permissions', + color=Color.red(), + description='You are neither an admin nor a mentor for this role.')) + paginator = Paginator(prefix='', suffix='') + for user in get_members_with_role(role): + paginator.add_line(f'{user} - {user.mention}') + for page in paginator.pages: + await ctx.send( + embed=Embed( + title=f'Users with the role @{role.name}', + description=page)) + if len(paginator.pages) == 0: + await ctx.send( + embed=Embed( + title=f'Users with the role @{role.name}', + description='No users found.')) + + +if __name__ == '__main__': + bot.run(config.token) -- cgit