async def attachment_filter(self, message): """When a message is sent by normal users ensure it doesn't have any non-image attachments. Delete it and send a mod message if it does.""" if message.author.bot: return if not message.attachments: return if os.environ.get("PRODUCTION_BOT", False): if message.channel.name.lower() in self.admin_channels: return if message.channel.permissions_for(message.author).manage_messages: return allowed, disallowed = self.categorize_attachments(message) if not allowed and not disallowed: return user_message = ("\n".join(f"> {section}" for section in message.content.split("\n")) if message.content.strip() else "") embed = Embed( title="File Attachments Not Allowed", description= f"For safety reasons we do not allow file and video attachments.", color=YELLOW, ) if allowed: embed.title = f"{message.author.display_name} Uploaded Some Code" embed.description = user_message files = {} name = None for attachment in allowed: content = (await attachment.read()).decode() if len(content) < 1000: file_type = os.path.splitext( attachment.filename)[1].casefold() embed.add_field( name=f"Attachment: {attachment.filename}", value= f"```{self.file_types.get(file_type, '')}\n{content}\n```", ) else: if not name: name = attachment.filename files[attachment.filename] = content if files: gist = self.upload_files(files) embed.add_field( name="Uploaded the file to a Gist", value=f"[{name}]({gist})", ) embed.set_thumbnail( url= "https://cdn.discordapp.com/emojis/711749954837807135.png?v=1") embed.set_footer( text="For safety reasons we do not allow file attachments.") else: embed.set_thumbnail( url= "https://cdn.discordapp.com/emojis/651959497698574338.png?v=1") if user_message: embed.add_field(name=f"{message.author.display_name} Said", value=user_message) embed.add_field( name="Code Formatting", value= f"You can share your code using triple backticks like this:\n\\```\nYOUR CODE\n\\```", inline=False, ) embed.add_field( name="Large Portions of Code", value= f"For longer scripts use [Hastebin](https://hastebin.com/) or " f"[GitHub Gists](https://gist.github.com/) and share the link here", inline=False, ) if disallowed: embed.add_field( name="Ignored these files", value="\n".join(f"- {attachment.filename}" for attachment in disallowed) or "*NO FILES*", ) try: await message.delete() except nextcord.errors.NotFound: pass await message.channel.send(message.author.mention, embed=embed)
def split_embed(embed: nextcord.Embed) -> List[nextcord.Embed]: """Splits embeds that are too long (discord character limit) Arguments: - embed (nextcord.Embed) Returns - embed_list (List[nextcord.Embed]): """ if embed.title == nextcord.Embed.Empty: embed.title = "" EMBED_CHARACTER_LIMIT = 2000 FIELD_CHARACTER_LIMIT = 1024 embed_list = [] character_count = len(embed.title) + len(embed.description) # If the title + description exceeds the character limit, we must break up the description into smaller parts. if character_count > EMBED_CHARACTER_LIMIT: print( f"Title and description are too long with {character_count} characters" ) characters_remaining = character_count description = embed.description while description != "": embed_list.append( nextcord.Embed( title=embed.title + " (continued)" if len(embed_list) > 0 else embed.title, color=embed.color)) # Find the point that is closest to the cutoff but with a space. cutoff_point = description[:(EMBED_CHARACTER_LIMIT - len(embed.title))].rfind(' ') if cutoff_point == -1: cutoff_point = EMBED_CHARACTER_LIMIT - len(embed.title) - 1 embed_list[-1].description = description[:cutoff_point + 1] description = description[cutoff_point + 1:] characters_remaining -= cutoff_point # If the title + description are small, we can just copy them over else: embed_list.append( nextcord.Embed(title=embed.title, description=embed.description, color=embed.color)) character_count = len(embed_list[-1].title) + len( embed_list[-1].description) # Iterate over all the proposed fields in the embed for field in embed.fields: field_description = field.value field_character_count = len(field_description) # Cut down the proposed fields to the appropriate size while field_character_count > FIELD_CHARACTER_LIMIT: # If we can add a full-sized field to the embed, do it if character_count + len( field.name ) + FIELD_CHARACTER_LIMIT <= EMBED_CHARACTER_LIMIT: cutoff_point = field_description[:FIELD_CHARACTER_LIMIT].rfind( ' ') if cutoff_point == -1: cutoff_point = FIELD_CHARACTER_LIMIT - 1 embed_list[-1].add_field( name=field.name, value=field_description[:cutoff_point + 1], inline=False) field_character_count -= cutoff_point field_description = field_description[cutoff_point + 1:] # If we can't add a full field to the embed, add a chopped field and then create a new embed else: cutoff_point = field_description[:EMBED_CHARACTER_LIMIT - character_count - len(field.name)].rfind(' ') if cutoff_point == -1: cutoff_point = EMBED_CHARACTER_LIMIT - character_count - len( field.name) - 1 embed_list[-1].add_field( name=field.name, value=field_description[:cutoff_point + 1], inline=False) field_character_count -= cutoff_point field_description = field_description[cutoff_point + 1:] # We just filled the entire embed up, so now we need to make a new one embed_list.append( nextcord.Embed(title=embed.title + " (continued)", color=embed.color)) character_count = len(embed_list[-1].title) # Once we've gotten to here, we know that the remaining field character count is able to fit in one field. # Since the field character limit is smaller than the embed character limit, we know we'd only need one split. if field_character_count + len( field.name) + character_count > EMBED_CHARACTER_LIMIT: cutoff_point = field_description[:EMBED_CHARACTER_LIMIT - character_count - len(field.name)].rfind(' ') if cutoff_point == -1: cutoff_point = EMBED_CHARACTER_LIMIT - character_count - len( field.name) - 1 embed_list[-1].add_field(name=field.name, value=field_description[:cutoff_point + 1], inline=False) embed_list.append( nextcord.Embed(title=embed.title + " (continued)", color=embed.color)) field_description = field_description[cutoff_point + 1:] character_count = len(embed_list[-1].title) + len(field.name) embed_list[-1].add_field(name=field.name, value=field_description, inline=False) # I believe if we run here then we just don't need to split anything. else: embed_list[-1].add_field(name=field.name, value=field_description, inline=field.inline) return embed_list