From 086972318323fb2e4740933f8a6cdccc8790c9bb Mon Sep 17 00:00:00 2001 From: electro7 Date: Sun, 27 May 2018 09:52:08 +0200 Subject: Initial commit --- .gitignore | 2 ++ config.py | 16 ++++++++++++++++ main.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ 4 files changed, 78 insertions(+) create mode 100644 .gitignore create mode 100644 config.py create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..643bf99 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv/ +config.yaml diff --git a/config.py b/config.py new file mode 100644 index 0000000..6686a06 --- /dev/null +++ b/config.py @@ -0,0 +1,16 @@ +from typing import List + +import yaml + + +class Config(object): + token: str + admin_roles: List[int] + + def __init__(self, **args): + self.__dict__.update(args) + + @classmethod + def load(cls, file): + with open(file) as f: + return cls(**yaml.load(f)) 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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1dd0371 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +https://github.com/Rapptz/discord.py/archive/rewrite.zip +PyYAML -- cgit