aboutsummaryrefslogtreecommitdiff
path: root/main.py
blob: c44cb5d4550382e275574782478b195740aa89f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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)