Exemplo n.º 1
0
 async def send(
     self,
     ctx: Context,
     channel: Optional[discord.TextChannel] = None,
     *,
     content: Optional[str] = None,
 ) -> None:
     channel = await self.check_channel(ctx, channel)  # Get the channel.
     if channel.guild != ctx.guild:
         raise errors.DifferentServer()
     content = await self.check_content(ctx, content)
     if content[1:4] == "```" and content[-3:] == "```":
         content = content[4:-3]
     msg = await channel.send(content)
     await self.send_message_info_embed(ctx, "Send", ctx.author, content,
                                        msg, channel)
Exemplo n.º 2
0
    async def send_embed(
            self,
            ctx: Context,
            channel: Optional[discord.TextChannel] = None) -> None:
        if ctx.invoked_subcommand is None:
            channel = await self.check_channel(ctx,
                                               channel)  # Get the channel.
            if channel.guild != ctx.guild:
                raise errors.DifferentServer()
            title = await self.check_content(
                ctx, None, ask_message="Enter the title of the embed:")
            if title[0:3] == "```" and title[-3:] == "```":
                title = title[3:-3]
            description = await self.check_content(
                ctx,
                None,
                ask_message="Enter the description (main body) of the embed:")
            if description[0:3] == "```" and description[-3:] == "```":
                description = description[3:-3]
            embed = discord.Embed(title=title, description=description)
            await channel.send(embed=embed)
            log_embed = discord.Embed(
                title="Sent the embed!",
                colour=discord.Colour(0xC387C1),
                description=
                "The full embed is in the attached file in JSON format.",
                timestamp=datetime.now(timezone.utc),
            )
            log_embed.add_field(name="Title", value=title)
            file_name = f"{ctx.author.id}-{datetime.utcnow()}-content.json"
            with open(file_name, "w+") as f:
                message_content_dict = embed.to_dict()

                json.dump(message_content_dict, f)
            await send_log_once(
                guild_id=ctx.guild.id,
                bot=self.bot,
                logger_type="main",
                embeds=[log_embed],
                file=discord.File(file_name, filename="content.json"),
            )
            await ctx.send(embed=discord.Embed(
                title="Sent the message!",
                description=
                "Wondering where the more informative message went? [Click here](https://docs.messagemanager.xyz/features/logging) for more info.",
            ))
            os.remove(file_name)
Exemplo n.º 3
0
    async def edit(
        self,
        ctx: Context,
        channel: Optional[discord.TextChannel] = None,
        message_id: Optional[int] = None,
        *,
        content: Optional[str] = None,
    ) -> None:
        channel = await self.check_channel(ctx, channel)
        if channel.guild != ctx.guild:
            raise errors.DifferentServer()

        msg = await self.check_message_id(ctx, channel, message_id)
        if msg.author != ctx.guild.me:
            raise errors.DifferentAuthor()

        content = await self.check_content(ctx, content)
        if content[1:4] == "```" and content[-3:] == "```":
            content = content[4:-3]

        await self.send_message_info_embed(ctx, "edit", ctx.author, content,
                                           msg, channel)
        await msg.edit(content=content)
Exemplo n.º 4
0
    async def fetch(
        self,
        ctx: Context,
        channel: Optional[discord.TextChannel] = None,
        message_id: Optional[int] = None,
    ) -> None:
        channel = await self.check_channel(ctx, channel)
        if channel.guild != ctx.guild:
            raise errors.DifferentServer(
                "That channel is not in this server, Please re-do the command")

        msg = await self.check_message_id(ctx, channel, message_id)
        file_name = f"{ctx.author.id}-{datetime.utcnow()}-content"
        if len(msg.embeds) == 0:
            file_name = file_name + ".txt"
            with open(file_name, "w+") as f:
                f.write(f"Content:\n\n{msg.content}")
        else:
            file_name = file_name + ".json"
            with open(file_name, "w+") as f:
                embeds_list = []
                for embed in msg.embeds:
                    embeds_list.append(embed.to_dict())
                message_content_dict = {"embeds": embeds_list, "content": ""}
                if msg.content is not None:
                    message_content_dict["content"] = msg.content

                json.dump(message_content_dict, f)
        if file_name[-4:] == "json":
            file_display_name = "content.json"
        else:
            file_display_name = "content.txt"
        await ctx.send(
            content="Fetched the message! Contents in the attached text file.",
            file=discord.File(file_name, filename=file_display_name),
        )
        os.remove(file_name)
Exemplo n.º 5
0
    async def json_edit(
        self,
        ctx: Context,
        channel: Optional[discord.TextChannel] = None,
        message_id: Optional[int] = None,
        *,
        json_content: Optional[str] = None,
    ) -> None:
        print(type(channel))
        channel = await self.check_channel(ctx, channel)
        if channel.guild != ctx.guild:
            raise errors.DifferentServer()

        msg = await self.check_message_id(ctx, channel, message_id)
        if msg.author != ctx.guild.me:
            raise errors.DifferentAuthor()
        if len(msg.embeds) == 0:
            raise errors.InputContentIncorrect(
                f"That message does not have an embed! Try {self.bot.command_with_prefix(ctx, 'edit')} instead"
            )
        elif len(msg.embeds) > 1:
            raise errors.InputContentIncorrect(
                "That message has more than one embed! I don't support that right now 😔"
            )
        old_embed = msg.embeds[0].to_dict()

        new_json_content = await self.check_content(
            ctx,
            json_content,
            ask_message="What is the new JSON content of the embed?")
        if new_json_content[0:7] == "```json" and new_json_content[
                -3:] == "```":
            new_json_content = new_json_content[7:-3]
        elif new_json_content[0:3] == "```" and new_json_content[-3:] == "```":
            new_json_content = new_json_content[3:-3]

        try:
            new_dict_content = json.loads(new_json_content)
        except json.decoder.JSONDecodeError as e:
            raise errors.JSONFailure(
                "The json that you specified was not correct, please check and try again.\n"
                f"Get support from the docs or the support server at {self.bot.command_with_prefix(ctx, 'support')}\n"
                f"Error message: {e}")

        log_embed = discord.Embed(
            title="Edited the embed!",
            colour=discord.Colour(0xC387C1),
            description=
            "The original message and the new message are attached above.",
            timestamp=datetime.now(timezone.utc),
        )
        log_embed.add_field(name="Editor", value=ctx.author.mention)
        log_embed.add_field(name="channel", value=channel.mention)

        if "embeds" in new_dict_content:
            embeds = new_dict_content["embeds"]

            content = new_dict_content.pop("content", None)

            if len(embeds) > 1:
                raise errors.InputContentIncorrect(
                    "You can't edit more than one embed at once!\nTry again with only one embed in the JSON data"
                )
            if content is not None and content != "":
                await ctx.send(
                    "WARNING!\nPlease update the content separately, the content value has been ignored"
                )
            embed = embeds[0]
            try:
                if isinstance(embed["timestamp"], str):
                    embed.pop("timestamp")
            except KeyError:
                pass
            await msg.edit(embed=discord.Embed.from_dict(embed))

        else:
            try:
                if isinstance(new_dict_content["timestamp"], str):
                    new_dict_content.pop("timestamp")
            except KeyError:
                pass
            await msg.edit(embed=discord.Embed.from_dict(new_dict_content))

        old_file_name = f"{ctx.author.id}-{datetime.utcnow()}-old-content.json"
        with open(old_file_name, "w+") as f:
            json.dump(old_embed, f)

        new_file_name = f"{ctx.author.id}-{datetime.utcnow()}-new-content.json"
        with open(new_file_name, "w+") as f:
            json.dump(new_dict_content, f)
        await send_log_once(
            guild_id=ctx.guild.id,
            bot=self.bot,
            logger_type="main",
            embeds=[log_embed],
            files=[
                discord.File(new_file_name, filename="new-content.json"),
                discord.File(old_file_name, "old-content.json"),
            ],
        )
        await ctx.send(embed=discord.Embed(
            title="Edited the message!",
            description=
            "Wondering where the more informative message went? [Click here](https://docs.messagemanager.xyz/features/logging) for more info.",
        ))
        os.remove(new_file_name)
        os.remove(old_file_name)
Exemplo n.º 6
0
    async def edit_embed(
        self,
        ctx: Context,
        channel: Optional[discord.TextChannel] = None,
        message_id: Optional[int] = None,
    ) -> None:
        channel = await self.check_channel(ctx, channel)
        if channel.guild != ctx.guild:
            raise errors.DifferentServer()

        msg = await self.check_message_id(ctx, channel, message_id)
        if msg.author != ctx.guild.me:
            raise errors.DifferentAuthor()
        if len(msg.embeds) == 0:
            raise errors.InputContentIncorrect(
                f"That message does not have an embed! Try {self.bot.command_with_prefix(ctx, 'edit')} instead"
            )
        elif len(msg.embeds) > 1:
            raise errors.InputContentIncorrect(
                "That message has more than one embed! I don't support that right now 😔"
            )

        title = await self.check_content(
            ctx, None, ask_message="Enter the new title of the embed:")
        if title[0:3] == "```" and title[-3:] == "```":
            title = title[3:-3]
        description = await self.check_content(
            ctx,
            None,
            ask_message="Enter the new description (main body) of the embed:",
        )
        if description[0:3] == "```" and description[-3:] == "```":
            description = description[3:-3]
        old_embed = msg.embeds[0].to_dict()
        new_embed = msg.embeds[0]
        new_embed.description = description
        new_embed.title = title
        log_embed = discord.Embed(
            title="Edited the embed!",
            colour=discord.Colour(0xC387C1),
            description=
            "The original message and the new message are attached above.",
            timestamp=datetime.now(timezone.utc),
        )
        log_embed.add_field(name="Editor", value=ctx.author.mention)
        log_embed.add_field(name="Channel", value=channel.mention)
        old_file_name = f"{ctx.author.id}-{datetime.utcnow()}-old-content.json"
        with open(old_file_name, "w+") as f:
            json.dump(old_embed, f)

        new_file_name = f"{ctx.author.id}-{datetime.utcnow()}-new-content.json"
        with open(new_file_name, "w+") as f:
            json.dump(new_embed.to_dict(), f)
        await send_log_once(
            guild_id=ctx.guild.id,
            bot=self.bot,
            logger_type="main",
            embeds=[log_embed],
            files=[
                discord.File(new_file_name, filename="new-content.json"),
                discord.File(old_file_name, "old-content.json"),
            ],
        )
        await ctx.send(embed=discord.Embed(
            title="Edited the message!",
            description=
            "Wondering where the more informative message went? [Click here](https://docs.messagemanager.xyz/features/logging) for more info.",
        ))
        os.remove(new_file_name)
        os.remove(old_file_name)
        await msg.edit(embed=new_embed)
Exemplo n.º 7
0
    async def send_json_embed(
        self,
        ctx: Context,
        channel: Optional[discord.TextChannel] = None,
        *,
        json_content: Optional[str] = None,
    ) -> None:
        log_embed = discord.Embed(
            title="Sent the embed!",
            colour=discord.Colour(0xC387C1),
            description=
            "The full embed is in the attached text file in JSON format.",
            timestamp=datetime.now(timezone.utc),
        )
        log_embed.add_field(name="Author", value=ctx.author.mention)
        channel = await self.check_channel(ctx, channel)  # Get the channel.
        if channel.guild != ctx.guild:
            raise errors.DifferentServer()
        log_embed.add_field(name="Channel", value=channel.mention)
        json_content = await self.check_content(
            ctx,
            json_content,
            ask_message="What is the JSON content of the embed?")
        if json_content[0:7] == "```json" and json_content[-3:]:
            json_content = json_content[7:-3]
        elif json_content[0:3] == "```" and json_content[-3:] == "```":
            json_content = json_content[3:-3]
        try:
            dict_content = json.loads(json_content)
        except json.decoder.JSONDecodeError as e:
            raise errors.JSONFailure(
                "The json that you specified was not correct, please check and try again.\n"
                f"Get support from the docs or the support server at {self.bot.command_with_prefix(ctx, 'support')}\n"
                f"Error message: {e}")

        if "embeds" in dict_content:
            embeds = dict_content["embeds"]
            content = dict_content.pop("content", None)
            if content is not None and content != "":
                await channel.send(content)
            n = 1
            for embed in embeds:
                try:
                    if isinstance(embed["timestamp"], str):
                        embed.pop("timestamp")
                except KeyError:
                    pass
                if "title" in embed:
                    log_embed.add_field(name=f"Embed {n}",
                                        value=embed["title"])
                await channel.send(embed=discord.Embed.from_dict(embed))
                n = n + 1

        else:
            try:
                if isinstance(dict_content["timestamp"], str):
                    dict_content.pop("timestamp")
            except KeyError:
                pass
            await channel.send(embed=discord.Embed.from_dict(dict_content))
            if "title" in dict_content:
                log_embed.add_field(name="Embed title",
                                    value=dict_content["title"])
        file_name = f"{ctx.author.id}-{datetime.utcnow()}-content.json"
        with open(file_name, "w+") as f:
            json.dump(dict_content, f)
        await send_log_once(
            guild_id=ctx.guild.id,
            bot=self.bot,
            logger_type="main",
            embeds=[log_embed],
            file=discord.File(file_name, filename="content.json"),
        )
        await ctx.send(embed=discord.Embed(
            title="Sent the message!",
            description=
            "Wondering where the more informative message went? [Click here](https://docs.messagemanager.xyz/features/logging) for more info.",
        ))
        os.remove(file_name)
Exemplo n.º 8
0
    async def delete(
        self,
        ctx: Context,
        channel: Optional[discord.TextChannel] = None,
        message_id: Optional[int] = None,
    ) -> None:
        def is_correct(m: discord.Message) -> bool:
            return m.author == ctx.author

        channel = await self.check_channel(ctx, channel)

        if channel.guild != ctx.guild:
            raise errors.DifferentServer(
                "That channel is not in this server, Please re-do the command")

        msg = await self.check_message_id(ctx, channel, message_id)
        if msg.author.id != ctx.me.id:
            raise errors.DifferentAuthor(
                "That message was not from me! I cannot delete messages that are not from me"
            )
        embed = discord.Embed(
            title="Are you sure you want to delete this message?",
            colour=discord.Colour.red(),
            timestamp=datetime.now(timezone.utc),
        )
        embed.add_field(name="Channel", value=channel.mention, inline=False)
        if msg.content:
            message_content = msg.content
        elif isinstance(msg.embeds[0].title, str):
            message_content = msg.embeds[0].title
        else:
            message_content = "No title"
        embed.add_field(name="Content", value=message_content, inline=False)
        await ctx.send(embed=embed)

        try:
            choice = await self.bot.wait_for("message",
                                             check=is_correct,
                                             timeout=100.0)
        except asyncio.TimeoutError:
            await ctx.send("Timedout, Please re-do the command.")
            return
        if choice.content.lower() == "yes":

            if len(msg.embeds) > 0:
                log_embed = discord.Embed(
                    title="Deleted the message!",
                    colour=discord.Colour(0xC387C1),
                    description=
                    "The full embed(s) are in the attached file in JSON format.",
                    timestamp=datetime.now(timezone.utc),
                )
                log_embed.add_field(name="Deleter", value=ctx.author.mention)
                log_embed.add_field(name="Channel", value=channel.mention)
                file_name = f"{ctx.author.id}-{datetime.utcnow()}-content.json"
                with open(file_name, "w+") as f:
                    embeds_list = []
                    for embed in msg.embeds:
                        embeds_list.append(embed.to_dict())
                    message_content_dict = {
                        "embeds": embeds_list,
                        "content": ""
                    }
                    if msg.content is not None:
                        message_content_dict["content"] = msg.content

                    json.dump(message_content_dict, f)
                await send_log_once(
                    guild_id=ctx.guild.id,
                    bot=self.bot,
                    logger_type="main",
                    embeds=[log_embed],
                    file=discord.File(file_name, filename="content.json"),
                )
                await ctx.send(embed=discord.Embed(
                    title="Deleted the message!",
                    description=
                    "Wondering where the more informative message went? [Click here](https://docs.messagemanager.xyz/features/logging) for more info.",
                ))
                os.remove(file_name)

            else:
                if msg.content:
                    message_content = msg.content
                elif isinstance(msg.embeds[0].title, str):
                    message_content = msg.embeds[0].title
                else:
                    message_content = "No title"
                await self.send_message_info_embed(ctx, "delete", ctx.author,
                                                   message_content, msg,
                                                   channel)
            try:
                await msg.delete()
            except discord.errors.Forbidden:
                raise errors.ContentError("There was an unknown error!")

        else:
            embed = discord.Embed(
                title="Message deletion exited",
                colour=discord.Colour.red(),
                description=
                f"{ctx.author.mention}chose not to delete the message",
                timestamp=datetime.now(timezone.utc),
            )
            await ctx.send(embed=embed)