"Alias name contains reserved word. Please use a different alias."
                )
        if data := await only_command_exists(self.bot, command, ctx.guild):
            pass
        else:
            return await ctx.send(
                f"A command with name `{command}` doesn't exists.")

        if await only_alias_exists(self.bot, alias, ctx.guild):
            return await ctx.send(
                f"A command/alias with name `{alias}` already exists .")

        if await only_command_exists(self.bot, alias, ctx.guild):
            return await ctx.send(
                f"A command with name `{alias}` already exists .")

        session_check = check_sessions(self.bot, ctx, alias)
        if session_check[0]:
            return await ctx.send(session_check[1])

        await self.bot.db.execute(
            "INSERT INTO aliases(name, cmd_id, user_id, guild) VALUES($1, $2, $3, $4)",
            alias, data["id"], ctx.author.id, ctx.guild.id)
        invalidate(self.bot, ctx.guild, ctx.author, alias)

        await ctx.send(f"Command `{command}` has been aliased to `{alias}`.")


def setup(bot):
    bot.add_cog(Maker(bot))
    async def text(self, ctx, name: str, *, content: str = None):
        """Make a command that replies with a text"""

        cmds = [command.name for command in self.bot.commands]

        for cmd in cmds:
            if name.startswith(cmd):
                return await ctx.send(
                    "Command name contains reserved word. Please use a different name."
                )

        if await command_exists(self.bot, name, ctx.guild):
            return await ctx.send(
                f"A command with name `{name}` already exists.")

        # Stop conflicting with other commands
        def check(message):
            if (message.author == ctx.author and message.channel
                    == ctx.channel) and message.content != "":
                if message.content.lower().startswith(f"{ctx.prefix}"):
                    return False
                else:
                    return True
            else:
                return False

        session_check = check_sessions(self.bot, ctx, name)
        if session_check[0]:
            return await ctx.send(session_check[1])

        if not content:
            text_step = await ctx.send(
                f"Neat, the command name will be `{name}`. \nNow write some content for this command. "
            )

            try:
                text_input = await self.bot.wait_for("message",
                                                     timeout=180,
                                                     check=check)
            except:
                invalidate(self.bot, ctx.guild, ctx.author, name)
                return await ctx.send(
                    "Command creation cancelled and session destroyed.")

            content = text_input.content

            if content.lower() == "stop":
                invalidate(self.bot, ctx.guild, ctx.author, name)
                return await ctx.send(
                    "Command creation cancelled and session destroyed.")
        else:
            content = content

        if ctx.author.guild_permissions.manage_guild:
            isapproved = "yes"
            note = ""
        else:
            isapproved = "no"
            note = "Wait for server managers to approve this command"

        await self.bot.db.execute(
            "INSERT INTO commands(userid, guild, name, type, approved) VALUES($1, $2, $3, 'text', $4)",
            ctx.author.id,
            ctx.guild.id,
            name,
            isapproved,
        )

        data = await get_command(self.bot, name, ctx.guild.id)

        id_ = data["id"]
        await self.bot.db.execute(
            "INSERT INTO text(command_id, content) VALUES($1, $2)", id_,
            content)

        invalidate(self.bot, ctx.guild, ctx.author, name)
        await ctx.send(f"Command created `{name}`. {note}")

        runner = command_handler.Runner(self.bot)

        await runner.run_command(ctx,
                                 name=name,
                                 indm=False,
                                 bypass_check=False)
class Editor(commands.Cog):
    """Command editor"""
    def __init__(self, bot):
        self.bot = bot

    @commands.group(invoke_without_command=True)
    async def edit(self, ctx):
        await ctx.send(
            "Command type is missing. Use `**help edit` to see available commands."
        )

    @edit.command(name="command")
    async def command_(
        self,
        ctx,
        component: str,
        command: str,
        *,
        value: typing.Union[discord.Member, str],
    ):
        if data := await only_command_exists(self.bot, command,
                                             ctx.guild) is False:
            return await ctx.send(
                f"A command with name `{command}` doesnt exists.")

        if await only_alias_exists(self.bot, command, ctx.guild):
            return await ctx.send(
                f'`{value}` is an alias. Cant be edited. Please edit the actual command.'
            )

        if not data["userid"] == ctx.author.id:
            return await ctx.send("You are not the owner of this command.")

        if component == "help":
            if value.lower() == "reset":
                await self.bot.db.execute(
                    "UPDATE commands SET help=$1 WHERE id=$2", None,
                    data["id"])
                return await ctx.send(f"Help reseted of command `{command}`.")

            await self.bot.db.execute(
                "UPDATE commands SET help=$1 WHERE id=$2", value, data["id"])
            return await ctx.send(f"Help editted of command `{command}`.")

        if component.lower() == "name":
            # checks if someone in the same server is in the middle of making a command with that name or not
            session = check_sessions(self.bot, ctx, value)
            if session[0]:
                return await ctx.send(session[1])
            else:
                pass

            cmds = [command.name for command in self.bot.commands]

            for cmd in cmds:
                if value.startswith(cmd + " "):
                    return await ctx.send(
                        "Command name contains reserved word. Please use a different name."
                    )

            if await only_command_exists(self.bot, value, ctx.guild):
                invalidate(self.bot, ctx.guild, ctx.author, value)
                return await ctx.send(
                    f"A command with name `{value}` already exists. Please use different name."
                )

            if await only_alias_exists(self.bot, value, ctx.guild):
                invalidate(self.bot, ctx.guild, ctx.author, value)
                return await ctx.send(
                    f'An alias with name `{value}` exists. Please use a different name.'
                )

            await self.bot.db.execute(
                "UPDATE commands SET name=$1 WHERE id=$2", value, data["id"])

            # After command making finish invalidate the session so no already making error occur
            invalidate(self.bot, ctx.guild, ctx.author, value)
            return await ctx.send(
                f"Name editted of command from `{command}` to `{value}`.")

        if component == "owner":
            if not isinstance(value, discord.Member):
                return await ctx.send("Please mention a valid user.")

            await self.bot.db.execute(
                "UPDATE commands SET userid=$1 WHERE id=$2", value.id,
                data["id"])
            return await ctx.send(
                f"Ownership transferred of command `{command}` to {value.mention}."
            )
Exemple #4
0
    async def embed(self, ctx, name: str):
        cmds = [command.name for command in self.bot.commands]
        for cmd in cmds:
            if name.startswith(cmd):
                return await ctx.send(
                    "Command name contains reserved word. Please use a different name."
                )

        if await command_exists(self.bot, name, ctx.guild):
            return await ctx.send(
                f"A command with name `{name}` already exists.")

        session_check = check_sessions(self.bot, ctx, name)
        if session_check[0]:
            return await ctx.send(session_check[1])

        def check(message):
            if (message.author == ctx.author and message.channel
                    == ctx.channel) and (message.content != ""
                                         or len(message.attachments) > 0):
                if message.content.lower().startswith(f"{ctx.prefix}"):
                    return False
                else:
                    return True
            else:
                return False

        embed_step = await ctx.send(
            f"Neat, the command name will be `{name}`. \nWrite embed title below."
        )

        try:
            embed_input = await self.bot.wait_for("message",
                                                  timeout=180,
                                                  check=check)
        except:
            invalidate(self.bot, ctx.guild, ctx.author, name)
            return await ctx.send(
                "Command creation cancelled and session destroyed.")

        title = embed_input.content
        if title.lower() == "stop":
            invalidate(self.bot, ctx.guild, ctx.author, name)
            return await ctx.send(
                "Command creation cancelled and session destroyed.")

        # Ask description
        embed_step = await ctx.send("Now type description.")

        try:
            embed_input = await self.bot.wait_for("message",
                                                  timeout=180,
                                                  check=check)
        except:
            invalidate(self.bot, ctx.guild, ctx.author, name)
            return await ctx.send(
                "Command creation cancelled and session destroyed.")

        description = embed_input.content

        if description.lower() == "stop":
            invalidate(self.bot, ctx.guild, ctx.author, name)
            return await ctx.send(
                "Command creation cancelled and session destroyed.")

        # Ask Thumbnail
        embed_step = await ctx.send(
            "What will be the thumbnail url?  `Be sure to put a valid image url. If you dont want any, type 'pass'`"
        )

        try:
            embed_input = await self.bot.wait_for("message",
                                                  timeout=180,
                                                  check=check)
        except:
            invalidate(self.bot, ctx.guild, ctx.author, name)
            return await ctx.send(
                "Command creation cancelled and session destroyed.")

        thumb_url = embed_input.content

        if thumb_url.lower() == "stop":
            invalidate(self.bot, ctx.guild, ctx.author, name)
            return await ctx.send(
                "Command creation cancelled and session destroyed.")

        if thumb_url == "":
            if len(embed_input.attachments) > 0:
                thumb_url = embed_input.attachments[0].url
                exts = [
                    "jpg", "gif", "png", "webph", "jpeg", "JPEG", "JPG", "PNG",
                    "WEBPH", "GIF"
                ]
                if thumb_url.split(".")[-1] in exts:
                    pass
                else:
                    thumb_url = None
            else:
                pass

        if thumb_url.lower() == "pass":
            thumb_url = None

        # Ask Image
        embed_step = await ctx.send(
            "What will be the image url?  `Be sure to put a valid image url. If you dont want any, type 'pass'`"
        )

        try:
            embed_input = await self.bot.wait_for("message",
                                                  timeout=180,
                                                  check=check)
        except:
            invalidate(self.bot, ctx.guild, ctx.author, name)
            return await ctx.send(
                "Command creation cancelled and session destroyed.")

        image_url = embed_input.content

        if image_url.lower() == "stop":
            invalidate(self.bot, ctx.guild, ctx.author, name)
            return await ctx.send("Command creation cancelled.")

        if not image_url:
            if len(embed_input.attachments) > 0:
                image_url = embed_input.attachments[0].url
                exts = [
                    "jpg", "gif", "png", "webph", "jpeg", "JPEG", "JPG", "PNG",
                    "WEBPH", "GIF"
                ]
                if image_url.split(".")[-1] in exts:
                    pass
                else:
                    image_url = None
            else:
                pass
        if image_url.lower() == "pass":
            image_url = None

        if ctx.author.guild_permissions.manage_guild:
            isapproved = "yes"
            note = ""
        else:
            isapproved = "no"
            note = "Wait for server managers to approve this command"

        await self.bot.db.execute(
            "INSERT INTO commands(userid, guild, name, type, approved) VALUES($1, $2, $3, 'embed', $4)",
            ctx.author.id,
            ctx.guild.id,
            name,
            isapproved,
        )

        data = await get_command(self.bot, name, ctx.guild.id)

        id_ = data["id"]
        await self.bot.db.execute(
            "INSERT INTO embed(command_id, title, description, thumbnail, image) VALUES($1, $2, $3, $4, $5)",
            id_,
            title,
            description,
            thumb_url,
            image_url,
        )

        invalidate(self.bot, ctx.guild, ctx.author, name)
        await ctx.send(f"Command created `{name}`. {note}")

        runner = command_handler.Runner(self.bot)

        await runner.run_command(ctx,
                                 name=name,
                                 indm=False,
                                 bypass_check=False)
    async def giverole(self, ctx, name: str,
                       roles: commands.Greedy[discord.Role]):
        cmds = [command.name for command in self.bot.commands]

        if len(roles) < 1:
            return await ctx.send(
                "Roles are missing. Please see help for info.")

        for cmd in cmds:
            if name.startswith(cmd):
                return await ctx.send(
                    "Command name contains reserved word. Please use a different name."
                )

        if await command_exists(self.bot, name, ctx.guild):
            return await ctx.send(
                f"A command with name `{name}` already exists.")

        session_check = check_sessions(self.bot, ctx, name)
        if session_check[0]:
            return await ctx.send(session_check[1])

        bot_ = ctx.guild.get_member(self.bot.user.id)

        valids = []
        reasons = []

        for role in roles:
            if not bot_.top_role < role:
                if not ctx.author.top_role < role:
                    if not role.managed:
                        valids.append(role.id)
                    else:
                        reasons.append(
                            f"{role.name} is automatically managed.")
                else:
                    if ctx.author.guild_permissions.administrator:
                        valids.append(role.id)
                    else:
                        reasons.append(
                            f"Your top role is lower than {role.name}.")
            else:
                reasons.append(f"Bots top role is lower than {role.name} role")

        if len(valids) < 1:
            invalidate(self.bot, ctx.guild, ctx.author, name)
            return await ctx.send(
                f'Command creation failed. Reasons -> `{", ".join(reasons)}`.')

        await self.bot.db.execute(
            "INSERT INTO commands(userid, guild, name,type, approved) VALUES($1, $2, $3, 'role', 'yes')",
            ctx.author.id,
            ctx.guild.id,
            name,
        )

        data = await get_command(self.bot, name, ctx.guild.id)

        id_ = data["id"]
        await self.bot.db.execute(
            "INSERT INTO role(command_id, role, action) VALUES($1, $2, 'give')",
            id_,
            valids,
        )

        note = ""
        if len(reasons) > 0:
            note = "`Some roles where declined reasons below. \n" + "\n".join(
                reasons) + "`"

        invalidate(self.bot, ctx.guild, ctx.author, name)
        await ctx.send(f"Command created `{name}`\n\n{note}")
Exemple #6
0
    async def giveandremove(self, ctx, name: str):
        cmds = [command.name for command in self.bot.commands]

        for cmd in cmds:
            if name.startswith(cmd):
                return await ctx.send(
                    "Command name contains reserved word. Please use a different name."
                )

        if await command_exists(self.bot, name, ctx.guild):
            return await ctx.send(
                f"A command with name `{name}` already exists.")

        session_check = check_sessions(self.bot, ctx, name)
        if session_check[0]:
            return await ctx.send(session_check[1])

        bot_ = ctx.guild.get_member(self.bot.user.id)

        gives = []
        removes = []

        def check(message):
            return len(message.role_mentions) > 0

        def can_add(role, bot):
            if bot.top_role < role:
                return [
                    False, "Bot's top role is lower than " + role.mention +
                    " role. Cant add."
                ]
            if role.managed:
                return [
                    False, role.mention + " role is a managed role. Cant add."
                ]
            if ctx.author.top_role < role and ctx.author.guild_permissions(
                    manage_roles=False):
                return [False, role.mention + " is higher than your top role"]
            else:
                return [True]

        await ctx.send("[ + ]Mention the roles you want to add.")
        note = ""
        try:
            msg: discord.Message = await self.bot.wait_for("message",
                                                           timeout=60,
                                                           check=check)
            for role in msg.role_mentions:
                x = can_add(role, ctx.guild.get_member(self.bot.user.id))
                if x[0]:
                    gives.append(role)
                else:
                    note += x[1] + "\n"
            if len(gives) < 1:
                invalidate(self.bot, ctx.guild, ctx.author, name)
                return await ctx.send(
                    "None of the mentioned role can be managed by bot or you. Command creation cancelled, try again."
                )
            else:
                added = ""
                for role in set(gives):
                    added += role.mention + " "

                added += " roles are added."

                await ctx.send(note + "\n" + added)
        except Exception as e:
            print(e)
            invalidate(self.bot, ctx.guild, ctx.author, name)
            return await ctx.send("Timeout")

        # ----------------------------------------------------------------------------
        await ctx.send("[ + ]Mention the roles you want to Remove.")
        note = ""
        try:
            msg: discord.Message = await self.bot.wait_for("message",
                                                           timeout=60,
                                                           check=check)
            for role in msg.role_mentions:
                x = can_add(role, ctx.guild.get_member(self.bot.user.id))
                if x[0]:
                    removes.append(role)
                else:
                    note += x[1] + "\n"
            if len(gives) < 1:
                invalidate(self.bot, ctx.guild, ctx.author, name)
                return await ctx.send(
                    "None of the mentioned role can be managed by bot or you. Command creation cancelled, try again."
                )
            else:
                added = ""
                for role in set(removes):
                    added += role.mention + " "

                added += " roles are added."

                await ctx.send(note + "\n" + added)
        except Exception as e:
            print(e)
            invalidate(self.bot, ctx.guild, ctx.author, name)
            return await ctx.send("Timeout")

        gives = [role.id for role in gives]
        removes = [role.id for role in removes]

        await self.bot.db.execute(
            "INSERT INTO commands(userid, guild, name, type, approved) VALUES($1, $2, $3, 'mrl', 'yes')",
            ctx.author.id,
            ctx.guild.id,
            name,
        )

        data = await get_command(self.bot, name, ctx.guild.id)

        id_ = data["id"]
        await self.bot.db.execute(
            "INSERT INTO multirole(command_id, gives, removes) VALUES ($1, $2, $3)",
            id_,
            gives,
            removes,
        )

        invalidate(self.bot, ctx.guild, ctx.author, name)
        await ctx.send(f"Command created `{name}`.")