Ejemplo n.º 1
0
async def self_roles(bot, message, user_id, reaction, **kwargs):
    user = message.channel.guild.get_member(user_id)
    if user is None:
        if user_id is not 0:
            await remove_reaction(message, reaction, await bot.fetch_user(user_id))
            return kwargs
    bot.loop.create_task(remove_reaction(message, reaction, user))
    left = Emoji.get_chat_emoji('LEFT')
    right = Emoji.get_chat_emoji('RIGHT')
    refresh = Emoji.get_chat_emoji('REFRESH')
    r2 = "🔁"
    page_num = int(kwargs.get("page_num", 0))
    add = reaction not in [left, right, refresh, r2]
    if str(reaction) == left:
        page_num -= 1
        add = False
    elif str(reaction) == right:
        page_num += 1
        add = False
    elif str(reaction) in [refresh, r2]:
        if not message.channel.permissions_for(message.channel.guild.me).manage_messages:
            return kwargs
        await message.clear_reactions()
        await asyncio.sleep(0.2)
        add = False
    if add:
        for i in range(10):
            if str(reaction) == str(Emoji.get_emoji(str(i+1))):
                roles = Configuration.get_var(message.guild.id, "SELF_ROLES")
                role = message.channel.guild.get_role(roles[page_num*10 + i])
                if role is None:
                    await Selfroles.validate_self_roles(bot, message.channel.guild)
                    return
                add_role = role not in user.roles
                try:
                    await (user.add_roles if add_role else user.remove_roles)(role)
                except Forbidden:
                    if message.channel.permissions_for(message.channel.guild.me).send_messages:
                        await MessageUtils.send_to(message.channel, "NO", "role_too_high_add", role=role.name)
                        return kwargs
                else:
                    if message.channel.permissions_for(message.channel.guild.me).send_messages:
                        await MessageUtils.send_to(message.channel, "YES", "role_joined" if add_role else "role_left", role_name=await Utils.clean(role.name), delete_after=10)
                        bot.loop.create_task(remove_reaction(message, reaction, user))
                        return kwargs

        return kwargs
    pages = Selfroles.gen_role_pages(message.channel.guild)

    if page_num >= len(pages):
        page_num = 0
    elif page_num < 0:
        page_num = len(pages) - 1
    kwargs["page_num"] = page_num
    embed = Embed(title=Translator.translate("assignable_roles", message.channel, server_name=message.channel.guild.name, page_num=page_num+1,
                                             page_count=len(pages)), colour=Colour(0xbffdd), description=pages[page_num])
    await message.edit(embed=embed)
    await Selfroles.update_reactions(message, pages[page_num], len(pages) > 1)
    bot.loop.create_task(bot.redis_pool.expire(f"self_role:{message.channel.guild.id}", int(kwargs.get("duration", 60 * 60 * 24 * 7))))
    return kwargs
Ejemplo n.º 2
0
 async def remove(self, ctx:commands.Context, *, role:discord.Role):
     await remove_item(ctx, role, 'self')
     Selfroles.validate_self_roles(self.bot, ctx.guild)
     self.bot.dispatch("self_roles_update", ctx.guild.id)
Ejemplo n.º 3
0
 async def add(self, ctx:commands.Context, *, role:discord.Role):
     await add_item(ctx, role, 'self', config_section="ROLES")
     Selfroles.validate_self_roles(self.bot, ctx.guild)
     self.bot.dispatch("self_roles_update", ctx.guild.id)