Added blacklist, whitelist, banned words, and made embeds function

This commit is contained in:
Stijn De Clercq
2020-09-30 15:56:28 +02:00
parent d08fa2b721
commit 13548f10c6
7 changed files with 160 additions and 9 deletions

5
bot.py
View File

@@ -8,8 +8,11 @@ prefixes = ["/"]
client = commands.Bot(command_prefix=prefixes, case_insensitive=True)
client.prefixes = prefixes
# Load this cog first so the other cogs can use properties initialized here
client.load_extension("cogs.events")
for file in os.listdir("./cogs"):
if file.endswith(".py"):
if file.endswith(".py") and not (file.startswith(("events",))):
client.load_extension(f"cogs.{file[:-3]}")
client.run("NzYwNTI5NDY2MDI3MDE2MjUy.X3NYQg.Nz3bwxgltlayMStfT7F-OCbx9pE")

59
cogs/events.py Normal file
View File

@@ -0,0 +1,59 @@
import discord
from discord.ext import commands
from data import constants
from functions import checks, bannedWords
class Events(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_connect(self):
print("Connected")
@commands.Cog.listener()
async def on_ready(self):
print("Ready")
self.client.banned_words = bannedWords.load()
@commands.Cog.listener()
async def on_message(self, message):
if message.guild is None:
return
# The bot sends a confirmation message after banning a word,
# allow the bot to send this message.
# This also makes it possible for the bot to send an embed
# with that message in #ModLogs.
#
# Allow mods to use these words as well to whitelist them.
if message.author.id == constants.wcbID or checks.isModPlus(message):
return
# Check for banned words
for word in self.client.banned_words:
if word in message.content.lower():
await message.delete()
return await self.bannedWordEmbed(message, word)
async def bannedWordEmbed(self, message, word):
modLogs = message.guild.get_channel(constants.ModLogs)
embed = discord.Embed(colour=discord.Colour.red())
iconFile = discord.File("files/images/server_icon.png", "icon.png")
embed.set_author(name="Bad Word Usage", icon_url="attachment://icon.png")
embed.add_field(name="Author", value="{} #{}".format(message.author.display_name, message.author.discriminator))
embed.add_field(name="Channel", value=message.channel.mention)
embed.add_field(name="Word", value=word)
embed.add_field(name="Zin", value=message.content, inline=False)
embed.set_footer(text=embed.timestamp)
await modLogs.send(embed=embed, file=iconFile)
def setup(client):
client.add_cog(Events(client))

View File

@@ -1,6 +1,6 @@
import discord
from discord.ext import commands
from functions import checks
from functions import checks, bannedWords, embeds
class ModCommands(commands.Cog):
@@ -15,15 +15,51 @@ class ModCommands(commands.Cog):
# Delete the original message
await ctx.message.delete()
# Attach the server icon as a file
file = discord.File("files/images/server_icon.png", filename="icon.png")
# Set up the embed
embed = discord.Embed(colour=discord.Colour.orange())
embed.set_author(name=ctx.author.display_name, icon_url="attachment://icon.png")
embed, file = embeds.modEmbed(ctx.author.display_name)
embed.description = arg
await ctx.send(embed=embed, file=file)
@commands.command(name="Blacklist", aliases=["Bl"])
@commands.check(checks.isModPlus)
async def blacklist(self, ctx, word=None):
if word is None:
return await ctx.send("You did not provide a word to blacklist.")
if not bannedWords.ban_word(word):
await ctx.message.add_reaction("")
return await ctx.send("This word has already been blacklisted.")
await ctx.message.add_reaction("")
self.client.banned_words.append(word.lower())
return await ctx.send("Blacklisted **{}**.".format(word))
@commands.command(name="Whitelist", aliases=["Wl"])
async def whitelist(self, ctx, word=None):
if word is None:
return await ctx.send("You did not provide a word to whitelist.")
if not bannedWords.remove_word(word):
await ctx.message.add_reaction("")
return await ctx.send("This word has not been blacklisted.")
await ctx.message.add_reaction("")
self.client.banned_words.remove(word.lower())
return await ctx.send("Whitelisted **{}**.".format(word))
@commands.command(name="Blacklisted", aliases=["Words"])
async def bannedwords(self, ctx):
embed, file = embeds.modEmbed("Blacklisted Words")
banned = sorted(self.client.banned_words)
# Can't send empty embed
if not banned:
embed.description = "No words have been blacklisted yet."
else:
embed.description = " - ".join(banned)
return await ctx.send(embed=embed, file=file)
def setup(client):
client.add_cog(ModCommands(client))
client.add_cog(ModCommands(client))

View File

@@ -11,3 +11,6 @@ roleOwner = 687996070986383436
adminRoles = [roleAdmin, roleOwner]
modPlusRoles = [roleAdmin, roleMod, roleOwner]
# Channels
ModLogs = 760807882899193867

1
files/banned_words.json Normal file
View File

@@ -0,0 +1 @@
[]

37
functions/bannedWords.py Normal file
View File

@@ -0,0 +1,37 @@
import json
def load():
with open("files/banned_words.json", "r") as fp:
return json.load(fp)
def write(words):
with open("files/banned_words.json", "w") as fp:
json.dump(words, fp)
def ban_word(word: str):
words = load()
# Already been banned
if word.lower() in words:
return False
# Add to the list of words
words.append(word.lower())
write(words)
return True
def remove_word(word: str):
words = load()
# Not banned
if word.lower() not in words:
return False
# Unban word
words.remove(word.lower())
write(words)
return True

12
functions/embeds.py Normal file
View File

@@ -0,0 +1,12 @@
import discord
def modEmbed(author):
embed = discord.Embed(colour=discord.Colour.orange())
# Attach the server icon as a file
file = discord.File("files/images/server_icon.png", filename="icon.png")
embed.set_author(name=author, icon_url="attachment://icon.png")
return embed, file