From eab9af487680fdf596d6ccb339254682b7a0be7a Mon Sep 17 00:00:00 2001 From: sgoudham Date: Wed, 9 Sep 2020 23:49:56 +0100 Subject: [PATCH] Added functions for content filter and notifications Minor logging for guild updates (Still need to do roles/emojis/features etc etc) --- cogs/libs/functions.py | 92 +++++++++++++++++++++++++++++++----------- cogs/moderation.py | 65 +++++++++++++++-------------- 2 files changed, 103 insertions(+), 54 deletions(-) diff --git a/cogs/libs/functions.py b/cogs/libs/functions.py index 684a0806..e3b50992 100644 --- a/cogs/libs/functions.py +++ b/cogs/libs/functions.py @@ -1,29 +1,18 @@ -def string_list(types, n, instance): - """Return objects in nicely formatted strings""" +# Ensō~Chan - A Multi Purpose Discord Bot That Has Everything Your Server Needs! +# Copyright (C) 2020 Hamothy - if len(types) > n: - # Retrieve the length of the remaining roles - length = len(types) - n +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. - if instance == "Emoji": - # Store the first 20 emojis in a string - string = f"{' '.join(map(str, (types[:n])))} and **{length}** more..." - else: - # Store the first n roles/members in a string called "roles" (highest to lowest) - string = f"{' **|** '.join(_type.mention for _type in list(reversed(types))[:n])} and **{length}** more" +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. - else: - if instance == "Role": - # Display all roles as it is lower than n provided - string = f"{' **|** '.join(role.mention for role in list(reversed(types[1:])))}" - elif instance == "Emoji": - # Display all the emojis in the server as it is less than 20 - string = " ".join(map(str, types)) - else: - # Display all members as it is lower than n provided - string = f"{' **|** '.join(role.mention for role in list(reversed(types)))}" - - return string +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . # List of regions mapped to emojis @@ -48,6 +37,47 @@ region = { "brazil": ":flag_br: Brazil" } +# List of content filters and their descriptions +filters = { + "disabled": "<:xMark:746834944629932032>", + "no_role": "<:greenTick:746834932936212570> For Members Without Roles", + "all_members": "<:greenTick:746834932936212570> For All Members" +} + +# List of default notifications settings for guild +notifs = { + "all_messages": "<:greenTick:746834932936212570> For All Messages", + "only_mentions": "<:greenTick:746834932936212570> For All Mentions" +} + + +def string_list(types, n, instance): + """Return objects in nicely formatted strings""" + + if len(types) > n: + # Retrieve the length of the remaining roles + length = len(types) - n + + if instance == "Emoji": + # Store the first 20 emojis in a string + string = f"{' '.join(map(str, (types[:n])))} and **{length}** more..." + else: + # Store the first n roles/members in a string called "roles" (highest to lowest) + string = f"{' **|** '.join(_type.mention for _type in list(reversed(types))[:n])} and **{length}** more" + + else: + if instance == "Role": + # Display all roles as it is lower than n provided + string = f"{' **|** '.join(role.mention for role in list(reversed(types[1:])))}" + elif instance == "Emoji": + # Display all the emojis in the server as it is less than 20 + string = " ".join(map(str, types)) + else: + # Display all members as it is lower than n provided + string = f"{' **|** '.join(role.mention for role in list(reversed(types)))}" + + return string + def get_region(disc_region): """Return Nicer Looking Region String""" @@ -55,3 +85,19 @@ def get_region(disc_region): for key in region: if key == disc_region: return region[key] + + +def get_content_filter(content_filter): + """Return nicer looking content filter string""" + + for key in filters: + if key == content_filter: + return filters[key] + + +def get_notifs(notif): + """Return nicer looking notification string""" + + for key in notifs: + if key == notif: + return notifs[key] diff --git a/cogs/moderation.py b/cogs/moderation.py index c1e34247..69d57c56 100644 --- a/cogs/moderation.py +++ b/cogs/moderation.py @@ -23,7 +23,7 @@ from discord import Member, Embed, DMChannel, NotFound from discord.ext.commands import command, guild_only, has_guild_permissions, bot_has_guild_permissions, Greedy, \ cooldown, BucketType, Cog -from cogs.libs.functions import string_list, get_region +from cogs.libs.functions import string_list, get_region, get_content_filter, get_notifs # TODO: CREATE A BITARRAY SO THAT THE MODLOG EVENTS ARE TOGGLEABLE # TODO: MAKE SURE THAT THE BITARRAY IS ONLY IMPLEMENTED AFTER ALL EVENTS ARE CODED @@ -874,29 +874,21 @@ class Moderation(Cog): new_roles = [roles for roles in after.changed_roles] old_roles = [roles for roles in before.changed_roles] - def role_string(roles): - # Check if the amount of roles is above 20 - if len(roles) > 20: - # Retrieve the length of the remaining roles - length = len(roles) - 20 + # Get total new_roles in the channel + new_role_string = string_list(new_roles, 20, "Role") + # Get total old_roles in the channel + old_role_string = string_list(old_roles, 20, "Role") - # Store the first 20 roles in a string (highest to lowest) - string = f"{' **|** '.join(role.mention for role in list(reversed(roles))[:20])} and **{length}** more" - return string - - else: - # Display all roles as it is lower than 20 - string = f"{' **|** '.join(role.mention for role in list(reversed(roles[1:])))}" - return string - - embed = Embed(description=f"**{after.mention} Role Overrides Updated**", + embed = Embed(title="Role Overrides Updated", + description=f"**Channel -->** {after.mention}\n" + f"**ID -->** {after.id}", colour=self.bot.admin_colour, timestamp=datetime.datetime.utcnow()) embed.set_author(name=after.guild, icon_url=after.guild.icon_url) embed.add_field(name="Before", - value=role_string(old_roles) or after.guild.default_role.mention, inline=False) + value=old_role_string or after.guild.default_role.mention, inline=False) embed.add_field(name="After", - value=role_string(new_roles) or after.guild.default_role.mention, inline=False) + value=new_role_string or after.guild.default_role.mention, inline=False) embed.set_footer(text="Role Overrides Updated") await modlogs_channel.send(embed=embed) @@ -910,24 +902,35 @@ class Moderation(Cog): if modlogs := self.bot.get_modlog_for_guild(after.id): modlogs_channel = self.bot.get_channel(modlogs) - # Logging guild updates - if before.name != after.name or before.verification_level != after.verification_level \ - or before.afk_channel != after.afk_channel or before.mfa_level != after.mfa_level \ - or before.icon_url != after.icon_url or before.default_notifications != after.default_notifications \ - or before.region != after.region: + attributes = ["name", "verification_level", "afk_channel", "mfa_level", "icon_url", + "default_notifications", "region", "explicit_content_filter"] + + if any(getattr(before, x) != getattr(after, x) for x in attributes): # TODO: ADD LOGGING FOR THE ABOVE IF STATEMENTS. fields = [("Before", - f"**Guild Name -->** {before.name}\n" - f"**Region -->** {get_region(str(before.region))}\n" - f"**Verification Level -->** {before.verification_level.name.capitalize()}\n" - f"**AFK Channel -->** #{before.afk_channel or 'N/A'} **|** {before.afk_timeout}s\n", False), + f"**Guild Name -->** {before}\n" + f"**Region -->** {get_region(str(before.region))}\n\n" + + f"**2-Factor Authentication -->** {self.bot.tick if before.mfa_level == 1 else self.bot.cross}\n" + f"**Explicit Content Filter -->** {get_content_filter(before.explicit_content_filter.name)}\n" + f"**Verification Level -->** {before.verification_level.name.capitalize()}\n\n" + + f"**Default Notifications -->** {get_notifs(before.default_notifications)}\n" + f"**AFK Channel -->** {before.afk_channel.mention if before.afk_channel else '#N/A'} **|** {before.afk_timeout}s\n", + False), ("After", - f"**Guild Name -->** {after.name}\n" - f"**Region -->** {get_region(str(after.region))}\n" - f"**Verification Level -->** {after.verification_level.name.capitalize()}\n" - f"**AFK Channel -->** #{after.afk_channel or 'N/A'} **|** {after.afk_timeout}s\n", False)] + f"**Guild Name -->** {after}\n" + f"**Region -->** {get_region(str(after.region))}\n\n" + + f"**2-Factor Authentication -->** {self.bot.tick if after.mfa_level == 1 else self.bot.cross}\n" + f"**Explicit Content Filter -->** {get_content_filter(after.explicit_content_filter.name)}\n" + f"**Verification Level -->** {after.verification_level.name.capitalize()}\n\n" + + f"**Default Notifications -->** {get_notifs(after.default_notifications)}\n" + f"**AFK Channel -->** {after.afk_channel.mention if after.afk_channel else '#N/A'} **|** {after.afk_timeout}s\n", + False)] embed = Embed(title="Guild Updated", description=f"**Owner --> {after.owner.mention} |** {after.owner}\n"