async def send_mail(self, message, guild):
        self.bot.prom.tickets_message.inc({})

        if not guild:
            await message.channel.send(ErrorEmbed("The server was not found."))
            return

        try:
            member = await guild.fetch_member(message.author.id)
        except discord.NotFound:
            await message.channel.send(
                ErrorEmbed(
                    "You are not in that server, and the message is not sent.")
            )
            return

        data = await tools.get_data(self.bot, guild.id)

        category = await guild.get_channel(data[2])
        if not category:
            await message.channel.send(
                ErrorEmbed(
                    "A ModMail category is not found. The bot is not set up properly in the server."
                ))
            return

        if message.author.id in data[9]:
            await message.channel.send(
                ErrorEmbed(
                    "That server has blacklisted you from sending a message there."
                ))
            return

        channel = None
        for text_channel in await guild.text_channels():
            if tools.is_modmail_channel(text_channel, message.author.id):
                channel = text_channel
                break

        if channel is None:
            self.bot.prom.tickets.inc({})

            name = "".join([
                x for x in message.author.name.lower()
                if x not in string.punctuation and x.isprintable()
            ])

            if name:
                name = name + f"-{message.author.discriminator}"
            else:
                name = message.author.id

            try:
                channel = await guild.create_text_channel(
                    name=name,
                    category=category,
                    topic=
                    f"ModMail Channel {message.author.id} {message.channel.id} (Please do "
                    "not change this)",
                )
            except discord.HTTPException as e:
                await message.channel.send(
                    ErrorEmbed(
                        "An HTTPException error occurred. Please contact an admin on the server "
                        f"with the following information: {e.text} ({e.code})."
                    ))
                return

            log_channel = await guild.get_channel(data[4])
            if log_channel:
                embed = Embed(
                    title="New Ticket",
                    colour=0x00FF00,
                    timestamp=True,
                )
                embed.set_footer(
                    f"{message.author.name}#{message.author.discriminator} | "
                    f"{message.author.id}",
                    message.author.avatar_url,
                )

                try:
                    await log_channel.send(embed)
                except discord.Forbidden:
                    pass

            prefix = await tools.get_guild_prefix(self.bot, guild)

            embed = Embed(
                "New Ticket",
                "Type a message in this channel to reply. Messages starting with the server prefix "
                f"`{prefix}` are ignored, and can be used for staff discussion. Use the command "
                f"`{prefix}close [reason]` to close this ticket.",
                timestamp=True,
            )
            embed.add_field("User",
                            f"<@{message.author.id}> ({message.author.id})")
            embed.add_field(
                "Roles",
                "*None*" if len(member._roles) == 0 else
                " ".join([f"<@&{x}>" for x in member._roles])
                if len(" ".join([f"<@&{x}>" for x in member._roles])) <= 1024
                else f"*{len(member._roles)} roles*",
            )
            embed.set_footer(f"{message.author} | {message.author.id}",
                             message.author.avatar_url)

            roles = []
            for role in data[8]:
                if role == guild.id:
                    roles.append("@everyone")
                elif role == -1:
                    roles.append("@here")
                else:
                    roles.append(f"<@&{role}>")

            try:
                await channel.send(" ".join(roles), embed=embed)
            except discord.HTTPException:
                await message.channel.send(
                    ErrorEmbed(
                        "The bot is missing permissions. Please contact an admin on the server."
                    ))
                return

            if data[5]:
                embed = Embed(
                    "Greeting Message",
                    tools.tag_format(data[5], message.author),
                    colour=0xFF4500,
                    timestamp=True,
                )
                embed.set_footer(f"{guild.name} | {guild.id}", guild.icon_url)

                await message.channel.send(embed)

        embed = Embed("Message Sent",
                      message.content,
                      colour=0x00FF00,
                      timestamp=True)
        embed.set_footer(f"{guild.name} | {guild.id}", guild.icon_url)

        files = []
        for file in message.attachments:
            saved_file = io.BytesIO()
            await file.save(saved_file)
            files.append(discord.File(saved_file, file.filename))

        dm_message = await message.channel.send(embed, files=files)

        embed.title = "Message Received"
        embed.set_footer(
            f"{message.author.name}#{message.author.discriminator} | {message.author.id}",
            message.author.avatar_url,
        )

        for count, attachment in enumerate(
            [attachment.url for attachment in dm_message.attachments],
                start=1):
            embed.add_field(f"Attachment {count}", attachment, False)

        for file in files:
            file.reset()

        try:
            await channel.send(embed, files=files)
        except discord.Forbidden:
            await dm_message.delete()
            await message.channel.send(
                ErrorEmbed(
                    "The bot is missing permissions. Please contact an admin on the server."
                ))
Example #2
0
    async def send_mail_mod(self, message, prefix, anon=False, snippet=False):
        self.bot.prom.tickets_message.inc({})

        data = await tools.get_data(self.bot, message.guild.id)
        user = tools.get_modmail_user(message.channel)

        if user.id in data[9]:
            await message.channel.send(
                ErrorEmbed(
                    "That user is blacklisted from sending a message here. You need to whitelist "
                    "them before you can send them a message."))
            return

        try:
            member = await message.guild.fetch_member(user.id)
        except discord.NotFound:
            await message.channel.send(
                ErrorEmbed(
                    f"The user was not found. Use `{prefix}close [reason]` to close this channel."
                ))
            return

        if snippet is True:
            message.content = tools.tag_format(message.content, member)

        embed = Embed("Message Received",
                      message.content,
                      colour=0xFF4500,
                      timestamp=True)
        embed.set_author(
            str(message.author) if anon is False else "Anonymous#0000",
            message.author.avatar_url if anon is False else
            "https://cdn.discordapp.com/embed/avatars/0.png",
        )
        embed.set_footer(f"{message.guild.name} | {message.guild.id}",
                         message.guild.icon_url)

        files = []
        for file in message.attachments:
            saved_file = io.BytesIO()
            await file.save(saved_file)
            files.append(discord.File(saved_file, file.filename))

        dm_channel = tools.get_modmail_channel(self.bot, message.channel)

        try:
            dm_message = await dm_channel.send(embed, files=files)
        except discord.Forbidden:
            await message.channel.send(
                ErrorEmbed(
                    "The message could not be sent. The user might have disabled Direct Messages."
                ))
            return

        embed.title = "Message Sent"
        embed.set_author(
            str(message.author)
            if anon is False else f"{message.author} (Anonymous)",
            message.author.avatar_url,
        )
        embed.set_footer(f"{member} | {member.id}", member.avatar_url)

        for count, attachment in enumerate(
            [attachment.url for attachment in dm_message.attachments],
                start=1):
            embed.add_field(f"Attachment {count}", attachment, False)

        for file in files:
            file.reset()

        await message.channel.send(embed, files=files)

        try:
            await message.delete()
        except (discord.Forbidden, discord.NotFound):
            pass