Ejemplo n.º 1
0
    async def default_expiration(self, ctx, expiration: license_duration):
        """
        Sets default guild role expiration time.

        When creating new license, and expiration time is not passed, this is the default time the license will use.
        Newly created licenses will expire after this much time.

        License duration is either a number representing hours or a string consisting of words in format:
        each word has to contain [integer][type 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

        """
        await self.bot.main_db.change_default_license_expiration(
            ctx.guild.id, expiration)
        await ctx.send(embed=success(
            f"Default license expiration set to **{expiration}h**!", ctx.me))
Ejemplo n.º 2
0
 async def force_remove_all_guild_data(self, ctx, guild_id: int, guild_too: int = 0):
     """
     :param guild_id: guild in question
     :param guild_too: default 0. Pass 1 to delete guild table too.
     """
     await self.bot.main_db.remove_all_guild_data(guild_id, guild_too)
     await ctx.send(embed=success("Done", ctx.me))
Ejemplo n.º 3
0
 async def force_new_licensed_member(
         self, ctx, member: discord.Member, role: discord.Role,
         *, license_dur: license_duration
 ):
     expiration_date = construct_expiration_date(license_dur)
     await self.bot.main_db.add_new_licensed_member(member.id, ctx.guild.id, expiration_date, role.id)
     await ctx.send(embed=success("Done", ctx.me))
Ejemplo n.º 4
0
 async def load(self, ctx, extension_path):
     """
     Loads an extension.
     :param extension_path: full path, dotted access.
     """
     self.bot.load_extension(extension_path)
     await ctx.send(embed=success(f"{extension_path} loaded.", ctx.me))
Ejemplo n.º 5
0
 async def delete_license(self, ctx, license):
     """Deletes specified stored license."""
     if await self.bot.main_db.is_valid_license(license, ctx.guild.id):
         await self.bot.main_db.delete_license(license)
         await ctx.send(embed=success("License deleted.", ctx.me))
         logger.info(f"{ctx.author} is deleting license {license} from guild {ctx.guild}")
     else:
         await ctx.send(embed=failure("License not valid."))
Ejemplo n.º 6
0
 async def guilds_diagnostic(self, ctx):
     loaded_guilds = tuple(guild.id for guild in self.bot.guilds)
     db_guilds = await self.bot.main_db.get_all_guild_ids()
     difference = set(loaded_guilds).symmetric_difference(set(db_guilds))
     difference = None if len(difference) == 0 else difference
     message = (f"Loaded guilds: {len(loaded_guilds)}\n"
                f"Database guilds: {len(db_guilds)}\n"
                f"Difference: {difference}")
     await ctx.send(embed=success(message, ctx.me))
Ejemplo n.º 7
0
    async def valid(self, ctx, license):
        """
        Checks if passed license is valid

        """
        if await self.bot.main_db.is_valid_license(license, ctx.guild.id):
            await ctx.send(embed=success("License is valid", ctx.me))
        else:
            await ctx.send(embed=failure(f"License is not valid."))
Ejemplo n.º 8
0
    async def reload_config(self, ctx):
        """
        Reloads json config.

        """
        self.bot.config.reload_config()
        msg = "Successfully reloaded config."
        logger.info(msg)
        await ctx.send(embed=success(msg, ctx.me))
Ejemplo n.º 9
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))
Ejemplo n.º 10
0
    async def licenses(self, ctx, license_role: discord.Role = None):
        """
        Shows all licenses for a role in DM.

        Shows licenses linked to license_role and your guild.
        If license_role is not passed then default guild role is used.

        Sends results in DM to the user who invoked the command.

        """
        num = self.bot.config["maximum_unused_guild_licences"]

        guild_id = ctx.guild.id
        if license_role is None:
            # If license role is not passed just use the guild default license role
            # We load it from database
            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
            to_show = await self.bot.main_db.get_guild_licenses(
                num, guild_id, licensed_role_id)
        else:
            to_show = await self.bot.main_db.get_guild_licenses(
                num, guild_id, license_role.id)

        if len(to_show) == 0:
            await ctx.send(
                embed=failure("No available licenses for that role."))
            return

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

        for tple in to_show:
            table.add_row((tple[0], tple[1]))

        dm_title = f"Showing {len(to_show)} licenses for role '{license_role.name}' in guild '{ctx.guild.name}':\n\n"

        await ctx.send(embed=success("Sent to DM!", ctx.me), delete_after=5)
        await Paginator.paginate(self.bot,
                                 ctx.author,
                                 ctx.author,
                                 table.draw(),
                                 title=dm_title)
Ejemplo n.º 11
0
    async def default_role(self, ctx, role: discord.Role):
        """
        Changes default guild license role.

        When creating new license, and role is not passed, this is the default role the license will use.
        Role tied to license is the role that the member will get when he redeems it.
        """
        # Check if the role is manageable by bot
        if not ctx.me.top_role > role:
            await ctx.send(embed=failure(
                "I can only manage roles **below** me in hierarchy."))
            return
        await self.bot.main_db.change_default_guild_role(ctx.guild.id, role.id)
        await ctx.send(embed=success(f"{role.mention} set as default!", ctx.me)
                       )
Ejemplo n.º 12
0
    async def guild_diagnostic(self, ctx, guild_id: int = None):
        """
        A shortened version of guild_info command without any checks and
        including additional data from the guild object.

        minus DRY :(
        """
        if guild_id is None:
            guild_id = ctx.guild.id

        guild = self.bot.get_guild(guild_id)
        if guild is None:
            loaded_msg = "Guild ID not found in loaded guilds."
        else:
            loaded_msg = (
                f"Guild info:\n"
                f"Name: **{guild.name}**\n"
                f"Description: **{guild.description}**\n"
                f"Owner ID: **{guild.owner_id}**\n"
                f"Member count: **{guild.member_count}**\n"
                f"Role count: **{len(guild.roles)}**\n"
                f"Verification level: **{guild.verification_level}**\n"
                f"Premium tier: **{guild.premium_tier}**\n"
                f"System channel: **{guild.system_channel.id}**\n"
                f"Region: **{guild.region}**\n"
                f"Unavailable: **{guild.unavailable}**\n"
                f"Created date: **{guild.created_at}**\n"
                f"Features: **{guild.features}**")

        prefix, role_id, expiration = await self.bot.main_db.get_guild_info(
            guild_id)
        stored_license_count = await self.bot.main_db.get_guild_license_total_count(
            guild_id)
        active_license_count = await self.bot.main_db.get_guild_licensed_roles_total_count(
            guild_id)

        if role_id is None:
            role_id = "**Not set!**"

        db_msg = (f"Database guild info:\n"
                  f"Prefix: **{prefix}**\n"
                  f"Default license role: {role_id}\n"
                  f"Default license expiration time: **{expiration}h**\n"
                  f"Stored licenses: **{stored_license_count}**\n"
                  f"Active role subscriptions: **{active_license_count}**")

        await ctx.send(embed=success(f"{db_msg}\n\n{loaded_msg}", ctx.me))
Ejemplo n.º 13
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))
Ejemplo n.º 14
0
    async def random_license(self, ctx, number: int = 10):
        """
        Shows random guild licenses in DM.

        If number is not passed default value is 10.
        Maximum licenses to show is 100.

        Sends results in DM to the user who invoked the command.

        """
        maximum_number = self.bot.config["maximum_unused_guild_licences"]

        if number > maximum_number:
            await ctx.send(
                embed=failure(f"Number can't be larger than {maximum_number}!")
            )
            return
        to_show = await self.bot.main_db.get_random_licenses(
            ctx.guild.id, number)
        if not to_show:
            await ctx.send(embed=failure("No licenses saved in db."))
            return

        table = texttable.Texttable(max_width=90)
        table.set_cols_dtype(["t", "t", "t"])
        table.set_cols_align(["c", "c", "c"])
        header = ("License", "Role", "Duration (h)")
        table.add_row(header)

        for entry in to_show:
            # Entry is in form ('I0QSZeyPJTy3H8tNsmUihKsn8JH48y', '617484493296631839', 720)
            try:
                role = ctx.guild.get_role(int(entry[1]))
                table.add_row((entry[0], role.name, entry[2]))
            except (ValueError, AttributeError):
                # Just in case if error in case role is None (deleted from guild) just show IDs from database
                table.add_row(entry)

        title = f"Showing {number} random licenses from guild '{ctx.guild.name}':\n\n"
        await ctx.send(embed=success("Sent to DM!", ctx.me), delete_after=5)
        await Paginator.paginate(self.bot,
                                 ctx.author,
                                 ctx.author,
                                 table.draw(),
                                 title=title)
Ejemplo n.º 15
0
    async def prefix(self, ctx, *, prefix):
        """
        Changes guild prefix.
        Maximum prefix size is 5 characters.
        """
        if ctx.prefix == prefix:
            await ctx.send(embed=failure(f"Already using prefix **{prefix}**"))
            return

        try:
            await self.bot.main_db.change_guild_prefix(ctx.guild.id, prefix)
        except IntegrityError:
            await ctx.send(embed=failure(
                "Prefix is too long! Maximum of 5 characters please."))
            return

        await ctx.send(embed=success(
            f"Successfully changed prefix to **{prefix}**", ctx.me))
Ejemplo n.º 16
0
    async def guild_info(self, ctx):
        """
        Shows database data for the guild.

        """
        prefix, role_id, expiration = await self.bot.main_db.get_guild_info(
            ctx.guild.id)
        stored_license_count = await self.bot.main_db.get_guild_license_total_count(
            ctx.guild.id)
        active_license_count = await self.bot.main_db.get_guild_licensed_roles_total_count(
            ctx.guild.id)

        # If the bot just joined the guild it can happen that the default license role is not set.
        if role_id is not None:
            default_license_role = discord.utils.get(ctx.guild.roles,
                                                     id=int(role_id))
            # In case it is set in db but was deleted from the guild.
            # This is needed in case the bot was offline and role was deleted
            # because on_guild_role_delete will not fire (we delete it from db in that event if that deleted role
            # is set as default guild role).
            # TODO: ABOVE EVENT
            if default_license_role is None:
                default_license_role = role_id
                log = f"Can't find default license role {role_id} from guild {ctx.guild.name},{ctx.guild.id}"
                msg = (
                    f"Can't find default role {role_id} in this guild!\n"
                    f"It's saved in the database but it looks like it was deleted from the guild.\n"
                    f"Please update it.")
                logger.critical(log)
                await ctx.send(embed=failure(msg))
            else:
                default_license_role = default_license_role.mention
        else:
            default_license_role = "**Not set!**"

        msg = (f"Database guild info:\n"
               f"Prefix: **{prefix}**\n"
               f"Default license role: {default_license_role}\n"
               f"Default license expiration time: **{expiration}h**\n\n"
               f"Stored licenses: **{stored_license_count}**\n"
               f"Active role subscriptions: **{active_license_count}**")

        await ctx.send(embed=success(msg, ctx.me))
Ejemplo n.º 17
0
    async def revoke(self, ctx, member: discord.Member, role: discord.Role):
        """
        Revoke active subscription from member.

        Removes both the database entry and the role from a member.
        """
        try:
            # DRY
            await self.bot.main_db.get_member_license_expiration_date(member.id, role.id)
        except DatabaseMissingData:
            msg = f"{member.mention} doesn't have a subscription for {role.mention} saved in the database!"
            await ctx.send(embed=failure(msg))
            return

        # 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)
        msg = f"Successfully revoked subscription for {role.mention} from {member.mention}"
        await ctx.send(embed=success(msg, ctx.me))
        logger.info(f"{ctx.author} is revoking subscription for role {role} from member {member} in guild {ctx.guild}")
Ejemplo n.º 18
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)}```")
Ejemplo n.º 19
0
 async def can_manage(self, ctx, role: discord.Role):
     if ctx.me.top_role > role:
         await ctx.send(embed=success("I can manage that role.", None))
     else:
         await ctx.send(embed=failure("No I cannot manage that role."))
Ejemplo n.º 20
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."))
Ejemplo n.º 21
0
 async def unload(self, ctx, extension_path: str):
     """Unloads an extension.
     :param extension_path: full path, dotted access
     """
     self.bot.unload_extension(extension_path)
     await ctx.send(embed=success(f"{extension_path} unloaded.", ctx.me))