Пример #1
0
    async def delete_all(self, ctx):
        """
        Deletes all stored guild licenses.

        You will have to reply with "yes" for confirmation.
        """
        def check(msg):
            return msg.author == ctx.author and msg.channel == ctx.channel and msg.content == "yes"

        await ctx.send(embed=warning("Are you sure? Reply with case sensitive `yes` in the next 15s to proceed."))
        await self.bot.wait_for("message", check=check, timeout=15)
        await self.bot.main_db.remove_all_stored_guild_licenses(ctx.guild.id)
        await ctx.send(embed=success("Done!", ctx.me))
Пример #2
0
    async def revoke_all(self, ctx, member: discord.Member):
        """
        Revoke ALL active subscriptions from member.

        Removes both the database entry and the role from a member.
        """

        member_data = await self.bot.main_db.get_member_data(
            ctx.guild.id, member.id)
        count = 0

        for tple in member_data:
            role_id = int(tple[0])
            role = ctx.guild.get_role(role_id)
            if role is None:
                logger.info(
                    f"'revoke_all' called in guild {ctx.guild} and role that's loaded from database with "
                    f"ID:{role_id} cannot be removed from {member} because it doesn't exist in guild anymore! "
                    f"Continuing to removal from database.")
                await self.bot.main_db.delete_licensed_member(
                    member.id, role_id)
                count += 1
            else:
                try:
                    # First remove the role from member because this can fail in case of changed role hierarchy.
                    await member.remove_roles(role)
                    await self.bot.main_db.delete_licensed_member(
                        member.id, role_id)
                    count += 1
                except Forbidden as e:
                    msg = (
                        f"Can't remove {role.mention} from {member.mention}, no permissions to manage that role as "
                        f" I can only manage role below me in hierarchy. This probably means that {role.mention} "
                        f"was moved up in the hierarchy **after** it was registered in my system "
                        f"(or mine was moved down).\n"
                        f"{e}")
                    await ctx.send(embed=failure(msg))

        if count:
            msg = f"Successfully revoked {count} subscriptions from {member.mention}!"
            await ctx.send(embed=success(msg, ctx.me))
            logger.info(
                f"{ctx.author} has revoked all subscription for member {member} in guild {ctx.guild}"
            )
        else:
            msg = f"Couldn't revoke even a single subscription from member {member.mention}!"
            await ctx.send(embed=warning(msg))
Пример #3
0
    async def generate(self,
                       ctx,
                       num: positive_integer = 3,
                       license_role: discord.Role = None,
                       *,
                       license_duration: license_duration = None):
        """
        Generates new guild licenses.

        Max licenses to generate at once is 25.
        All Arguments are optional, if not passed default guild values are used.

        Arguments are stacked, meaning you can't pass 'license_duration' without the first 2 arguments.
        On the other hand you can pass only 'num'.

        Example usages:
        generate
        generate 10
        generate 5 @role
        generate 7 @role 1w

        License duration is either a number representing hours or a string consisting of words in format:
        each word has to contain [integer][format] , entries are separated by space.

        Formats are:
        years y months m weeks w days d hours h

        License duration examples:
        20
        2y 5months
        1m
        3d 12h
        1w 2m 1w
        1week 1week
        12hours 5d
        ...
        """
        if num > 25:
            await ctx.send(embed=failure(
                "Maximum number of licenses to generate at once is 25."))
            return

        # Check if the role is manageable by bot
        # Needed since bot isn't doing anything with the role, so no exception will occur.
        if license_role is not None and not ctx.me.top_role > license_role:
            await ctx.send(embed=failure(
                "I can only manage roles **below** me in hierarchy."))
            return

        guild_id = ctx.guild.id

        # Maximum number of unused licenses
        max_licenses_per_guild = self.bot.config[
            "maximum_unused_guild_licences"]
        guild_licences_count = await self.bot.main_db.get_guild_license_total_count(
            guild_id)
        if guild_licences_count == max_licenses_per_guild:
            msg = f"You have reached maximum number of unused licenses per guild: {max_licenses_per_guild}!"
            await ctx.send(embed=warning(msg))
            return
        if guild_licences_count + num > max_licenses_per_guild:
            msg = (
                f"I can't generate since you will exceed the limit of {max_licenses_per_guild} licenses!\n"
                f"Remaining licenses to generate: {max_licenses_per_guild-guild_licences_count}."
            )
            await ctx.send(embed=failure(msg))
            return

        if license_duration is None:
            license_duration = await self.bot.main_db.get_default_guild_license_duration_hours(
                guild_id)

        if license_role is None:
            licensed_role_id = await self.bot.main_db.get_default_guild_license_role_id(
                guild_id)
            license_role = ctx.guild.get_role(licensed_role_id)
            if license_role is None:
                await self.handle_missing_default_role(ctx, licensed_role_id)
                return

            generated = await self.bot.main_db.generate_guild_licenses(
                num, guild_id, licensed_role_id, license_duration)
        else:
            generated = await self.bot.main_db.generate_guild_licenses(
                num, guild_id, license_role.id, license_duration)

        count_generated = len(generated)
        ctx_msg = (
            f"Successfully generated {count_generated} licenses for role {license_role.mention}"
            f" in duration of {license_duration}h.\n"
            f"Sending generated licenses in DM for quick use.")
        await ctx.send(embed=success(ctx_msg, ctx.me))

        table = texttable.Texttable(max_width=45)
        table.set_cols_dtype(["t"])
        table.set_cols_align(["c"])
        header = ("License", )
        table.add_row(header)

        for license in generated:
            table.add_row((license, ))

        dm_msg = (
            f"Generated {count_generated} licenses for role '{license_role.name}' in "
            f"guild '{ctx.guild.name}' in duration of {license_duration}h:\n"
            f"{table.draw()}")
        await ctx.author.send(f"```{misc.maximize_size(dm_msg)}```")
Пример #4
0
    async def activate_license(self, ctx, license, guild_id: int, role_id: int,
                               member):
        """
        :param ctx: invoked context
        :param guild_id: guild id tied to license
        :param role_id: role id tied to license
        :param license: license to add
        :param member: who to give role to. Union[User, Member] depending if called in guild or Dm
        """
        guild = self.bot.get_guild(guild_id)
        if guild is None:
            await ctx.send(embed=failure(
                "Guild tied to that license not found in bot guilds."))
            return

        if ctx.guild is not None and ctx.guild.id != guild_id:
            await ctx.send(embed=failure(
                f"That license is not for this guild! "
                f"Either redeem it in correct guild '{guild.name}' or redeem in bot DM."
            ))
            return

        # Decorator won't work in DM so have to manually check
        if not guild.me.guild_permissions.manage_roles:
            await ctx.send(embed=failure(
                f"Guild '{guild.name}' , can't assign roles - no manage roles permission."
            ))
            if ctx.guild is not None:
                # delete message but only if in guild, can't delete dm messages
                await ctx.message.delete()
            return

        # Passed member can be a user if redeem was activated in dm, so get the member
        if ctx.guild is None:
            member = guild.get_member(member.id)
            if member is None:
                await ctx.send(embed=failure(
                    "You are no longer it the guild you're trying to activate license!"
                ))
                return

        if await self.bot.main_db.is_valid_license(license, guild.id):
            # Adding role to the member requires that role object
            # First we get the role linked to the license
            role = guild.get_role(role_id)
            if role is None:
                log_error_msg = (
                    f"Can't find role {role_id} in guild {guild.id} '{guild.name}' "
                    f"from license: '{license}' member to give the role to: {member.id} '{member.name}'"
                    "\n\nProceeding to delete this invalid license from database!"
                )
                logger.critical(log_error_msg)

                msg = (
                    "Well this is awkward...\n\n"
                    "The role that was supposed to be given out by this license has been deleted from this guild!"
                    f"\n\nError message:\n\n{log_error_msg}")
                await ctx.send(embed=failure(msg))
                await self.bot.main_db.delete_license(license)
                return
            # Now before doing anything check if member already has the role
            # Beside for logic (why redeem already existing subscription?) if we don't check this we will get
            # sqlite3.IntegrityError:
            #   UNIQUE constraint failed:LICENSED_MEMBERS.MEMBER_ID,LICENSED_MEMBERS.LICENSED_ROLE_ID
            # when adding new licensed member to table LICENSED_MEMBERS if member already has the role (because in that
            # table the member id and role id is unique aka can only have uniques roles tied to member id)
            if role in member.roles:
                # We notify user that he already has the role, we also show him the expiration date
                try:
                    expiration_date = await self.bot.main_db.get_member_license_expiration_date(
                        member.id, role_id)
                except DatabaseMissingData as e:
                    # TODO print role name instead of ID (from e)
                    msg = e.message
                    msg += (
                        f"\nThe bot did not register {member.mention} in the database with that role but somehow they have it."
                        "\nThis probably means that they were manually assigned this role without using the bot license system."
                        "\nHave someone remove the role from them and call this command again."
                    )
                    await ctx.send(embed=failure(msg))
                    if ctx.guild is not None:
                        # delete message but only if in guild, can't delete dm messages
                        await ctx.message.delete()
                    return

                remaining_time = get_remaining_time(expiration_date)
                msg = (
                    f"{member.mention} already has an active subscription for the '{role.name}' role!"
                    f"\nIt's valid for another {remaining_time}")
                await ctx.send(embed=warning(msg))
                if ctx.guild is not None:
                    # delete message but only if in guild, can't delete dm messages
                    await ctx.message.delete()
                return
            # We add the role to the member, we do this before adding/removing stuff from db
            # just in case the bot doesn't have perms and throws exception (we already
            # checked for bot_has_permissions(manage_roles=True) but it can happen that bot has
            # that permission and check is passed but it's still forbidden to alter role for the
            # member because of it's role hierarchy.) -> will raise Forbidden and be caught by cmd error handler
            await member.add_roles(role, reason="Redeemed license.")
            # We add entry to db table LICENSED_MEMBERS (which we will checked periodically for expiration)
            # First we get the license duration so we can calculate expiration date
            license_duration = await self.bot.main_db.get_license_duration_hours(
                license)
            expiration_date = construct_expiration_date(license_duration)
            # In case where you successfully redeemed the role and it's still in database(not expired)
            # BUT someone manually removed the role, in that case when you try to redeem a valid license
            # for the said role you will get IntegrityError because LICENSED_ROLE_ID and MEMBER_ID have to
            # be unique (and the entry still exists in database).
            # Even when caught by remove role event leave this
            try:
                await self.bot.main_db.add_new_licensed_member(
                    member.id, guild.id, expiration_date, role_id)
            except IntegrityError:
                # We remove the database entry because when role was remove the bot was
                # probably offline and couldn't register the role remove event
                await self.bot.main_db.delete_licensed_member(
                    member.id, role_id)
                await self.bot.main_db.add_new_licensed_member(
                    member.id, guild.id, expiration_date, role_id)
                msg = (
                    f"Someone removed the role manually from {member.mention} but no worries,\n"
                    "since the license is valid we're just gonna reactivate it :)"
                )
                await ctx.send(embed=info(msg, ctx.me))

            # Remove guild license from database, so it can't be redeemed again
            await self.bot.main_db.delete_license(license)
            # Send message notifying user
            msg = f"License valid - guild '{guild.name}' adding role '{role.name}' to {member.mention} in duration of {license_duration}h"
            await ctx.send(embed=success(msg, ctx.me))
        else:
            await ctx.send(embed=failure(
                "The license key you entered is invalid/deactivated."))