Esempio n. 1
0
    async def remove_role(self, member_id, guild_id, licensed_role_id):
        """
        Removes the specified role from member based on @params

        :param member_id: unique member id
        :param guild_id: guild ID from where the member is from. Needed because member can be in
                         multiple guilds at the same time.
        :param licensed_role_id: ID of a role to remove from member
        :raise RoleNotFound: if roles to be removed isn't in member roles (case when in db it's saved but someone
                manually removed their role so when db role expires and needs to be removed there is nothing to be
                removed)
        """
        guild = self.bot.get_guild(guild_id)
        if guild is None:
            raise GuildNotFound(
                f"Fatal exception. "
                f"Guild **{guild_id}** loaded from database cannot be found in bot guilds!"
            )

        member = guild.get_member(member_id)
        # For some reason this can be NONE, tried to recreate it but failed.
        # Short explanation is that member is indeed inside of the guild but at the time this is
        # called member can be NONE for unknown reason. Log logs below line "Can't remove licensed role..because he
        # has left the guild" , later when I tried to recreate it I couldn't. In test guild it was working and members
        # in question were all later been confirmed that indeed they are in the guild cache, so dunno
        # why this sometimes acts as if the member is not in the guild? Thus right now if it's None we will
        # force a API call
        if member is None:
            member = await guild.fetch_member(member_id)

        # If member is still None in this case then either he indeed has left the guild
        # or the bug has not been fixed.
        if member is None:
            logger.warning(
                f"Can't remove licensed role {licensed_role_id} from member {member_id} "
                f"because he has left the guild {licensed_role_id} ({guild.name})."
            )
            return

        member_role = discord.utils.get(member.roles, id=licensed_role_id)
        if member_role is None:
            raise RoleNotFound(
                f"Can't remove licensed role {member_role} for {member.mention}."
                f"Role not found ")
        else:
            await member.remove_roles(member_role)
            try:
                expired = f"Your license in guild **{guild}** has expired for the following role: **{member_role}** "
                await member.send(embed=simple_embed(expired, "Notification",
                                                     discord.Colour.blue()))
            except Forbidden:
                # Ignore if user has blocked DM
                pass
Esempio n. 2
0
    async def remove_role(self, member_id, guild_id, licensed_role_id):
        """
        Removes the specified role from member based on @params

        :param member_id: unique member id
        :param guild_id: guild ID from where the member is from. Needed because member can be in
                         multiple guilds at the same time.
        :param licensed_role_id: ID of a role to remove from member
        :raise RoleNotFound: if roles to be removed isn't in member roles (case when in db it's saved but someone
                manually removed their role so when db role expires and needs to be removed there is nothing to be
                removed)
        """
        guild = self.bot.get_guild(guild_id)
        if guild is None:
            raise GuildNotFound(
                f"Fatal exception. "
                f"Guild **{guild_id}** loaded from database cannot be found in bot guilds!"
            )

        # Temporal fix for intents, waiting for Discord to approve them so the bot can cache members as usual
        # member = guild.get_member(member_id)
        try:
            member = await guild.fetch_member(member_id)
        except (Forbidden, discord.HTTPException):
            member = None

        # If member has left the guild just return
        if member is None:
            logger.warning(
                f"Can't remove licensed role {licensed_role_id} from member {member_id} "
                f"because he has left the guild {licensed_role_id} ({guild.name})."
            )
            return

        member_role = discord.utils.get(member.roles, id=licensed_role_id)
        if member_role is None:
            raise RoleNotFound(
                f"Can't remove licensed role {member_role} for {member.mention}."
                f"Role not found ")
        else:
            await member.remove_roles(member_role)
            try:
                expired = f"Your license in guild **{guild}** has expired for the following role: **{member_role}** "
                await member.send(embed=simple_embed(expired, "Notification",
                                                     discord.Colour.blue()))
            except Forbidden:
                # Ignore if user has blocked DM
                pass
Esempio n. 3
0
 async def send_to_log_channel(self, message: str, *, title: str, ctx=None):
     """
     Logs passed message to logger as critical and sends the said message to bot log channel, if one is found.
     :param message: Message with error/traceback
     :param title: Title for message
     :param ctx: optional, if passed will be used to add additional info to message embed footer
     """
     root_logger.critical(f"{title}\n{message}")
     if self.is_ready():
         log_channel = self.get_channel(self.config["developer_log_channel_id"])
         embed = embed_handler.simple_embed(maximize_size(message), title, discord.Colour.red())
         if ctx is not None:
             guild_id = "DM" if ctx.guild is None else ctx.guild.id
             footer = f"Guild: {guild_id}    Author: {ctx.author}    Channel: {ctx.channel.id}"
             embed.set_footer(text=footer)
         if log_channel is not None:
             await log_channel.send(embed=embed)
Esempio n. 4
0
    async def remove_role(self, member_id, guild_id, licensed_role_id):
        """
        Removes the specified role from member based on @params

        :param member_id: unique member id
        :param guild_id: guild ID from where the member is from. Needed because member can be in
                         multiple guilds at the same time.
        :param licensed_role_id: ID of a role to remove from member
        :raise RoleNotFound: if roles to be removed isn't in member roles (case when in db it's saved but someone
                manually removed their role so when db role expires and needs to be removed there is nothing to be
                removed)

        """
        guild = self.bot.get_guild(guild_id)
        if guild is None:
            raise GuildNotFound(
                f"Fatal exception. "
                f"Guild **{guild_id}** loaded from database cannot be found in bot guilds!"
            )

        member = guild.get_member(member_id)

        # If member has left the guild just return
        if member is None:
            logger.warning(
                f"Can't remove licensed role {licensed_role_id} from member {member_id} "
                f"because he has left the guild {licensed_role_id} ({guild.name})."
            )
            return

        member_role = discord.utils.get(member.roles, id=licensed_role_id)
        if member_role is None:
            raise RoleNotFound(
                f"Can't remove licensed role {member_role} for {member.mention}."
                f"Role not found ")
        else:
            await member.remove_roles(member_role)
            try:
                expired = f"A sua licença **{member_role}** no servidor **{guild}** expirou, contate algum administrador para renovar e continuar tendo beneficios "
                await member.send(embed=simple_embed(expired, "Notificação",
                                                     discord.Colour.blue()))
            except Forbidden:
                # Ignore if user has blocked DM
                pass