Skip to content

Commit

Permalink
Merge pull request #30 from matthewflegg/feature-clear_blacklist_drop…
Browse files Browse the repository at this point in the history
…down

Added a dropdown for ~blrem
  • Loading branch information
v0idzdev authored Mar 31, 2022
2 parents 3dd4426 + 09d5b37 commit ccc5d77
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 13 deletions.
16 changes: 13 additions & 3 deletions cogs/admin/admin_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import discord
from discord.ext import commands
from utils.buttons import BlacklistClearButton, ClearMessagesView, DropdownView
from utils.buttons import BlacklistClearButton, BlacklistRemoveView, ClearMessagesView, DropdownView
from utils.functions import lift_ban, sanction

FILEPATH = "files/blacklist.json"
Expand Down Expand Up @@ -107,7 +107,7 @@ async def unban(self, ctx: commands.Context, user: discord.User):
@commands.command(aliases=["bladd"])
@commands.has_permissions(manage_messages=True)
@commands.cooldown(1, 2, commands.BucketType.user)
async def blacklist(self, ctx: commands.Context, *, words: str = None):
async def blacklist(self, ctx: commands.Context, *, words: str=None):
"""
⚙️ Bans words from being used.
Expand All @@ -123,6 +123,7 @@ async def blacklist(self, ctx: commands.Context, *, words: str = None):
)
view.message = await ctx.send(embed=embed, view=view)
return

id = str(ctx.guild.id)
words = [word.lower() for word in words.split(" ")]

Expand Down Expand Up @@ -212,7 +213,7 @@ async def showblacklist(self, ctx: commands.Context):

@commands.command(aliases=["blrem"])
@commands.has_permissions(manage_messages=True)
async def blacklistremove(self, ctx: commands.Context, *, words: str):
async def blacklistremove(self, ctx: commands.Context, *, words: str=None):
"""
⚙️ Removes a word from the list of banned words.
Expand All @@ -221,6 +222,15 @@ async def blacklistremove(self, ctx: commands.Context, *, words: str):
~blacklistremove | ~blrem <...words>
```
"""
if not words:
view = BlacklistRemoveView(ctx)
embed = discord.Embed(
title="🛠️ Please enter one or more words to remove from the blacklist."
)

view.message = await ctx.send(embed=embed, view=view)
return

id = str(ctx.guild.id)
words = {word.lower() for word in words.split(" ")}

Expand Down
7 changes: 2 additions & 5 deletions files/blacklist.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"953054451999072276": [
"prick",
"poop",
"cunt",
"retard",
"asshole"
"asshole",
"arse"
]
}
5 changes: 4 additions & 1 deletion handlers/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ async def on_member_remove(self, member: discord.Member):
if channel is not None:
await channel.send(f"👋🏻 Goodbye, **{member.name}**.")

@commands.Cog.listener()
@commands.Cog.listener() # This throws an AttributeError but it isn't really an issue
async def on_message(self, message: discord.Message):
"""
Called when a message is sent.
"""
if not message.guild:
return

blacklist = self.client.cache.blacklist

if (
Expand Down
185 changes: 181 additions & 4 deletions utils/buttons.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional

import black
import discord

from typing import Optional
from discord.ext import commands

FILEPATH = "files/blacklist.json"
Expand Down Expand Up @@ -130,7 +131,7 @@ def __init__(
view: discord.ui.View,
drop: discord.ui.Select,
*,
title: str = "🔑 Enter a word to blacklist:",
title: str = "🔒 Add a Word to the Blacklist",
) -> None:
super().__init__(title=title)
self.view = view
Expand Down Expand Up @@ -234,7 +235,7 @@ async def send_modal(
)
async def submit(self, interaction: discord.Interaction, button: discord.Button):
await interaction.response.defer()
values = self.drop.words
values = [value.lower() for value in self.drop.words]
id = str(interaction.guild_id)
if not values:
print(values)
Expand Down Expand Up @@ -267,3 +268,179 @@ async def abort(self, interaction: discord.Interaction, button: discord.Button):
f"❌ {interaction.user.mention}: Aborting command!", ephemeral=True
)
await self.disable_all_buttons(interaction)


class BlacklistRemoveModal(discord.ui.Modal):
def __init__(
self,
view: discord.ui.View,
drop: discord.ui.Select,
*,
title: str="🔒 Remove a Word From the Blacklist"
) -> None:
super().__init__(title=title)

self.view = view
self.drop = drop
self.text = discord.ui.TextInput(
label="Word", placeholder="🔑 Enter a word to remove from the blacklist:", max_length=100
)

self.add_item(self.text)

async def on_submit(self, interaction: discord.Interaction) -> None:
await interaction.response.defer()
text_value = self.text.value

if not text_value:
return

if self.drop.options[0].label == "\u200b":
self.drop.options.pop(0)

option = discord.SelectOption(label=text_value, default=True)

self.drop.append_option(option)
self.drop.max_values = len(self.drop.options)
self.drop.words.append(text_value)

await interaction.message.edit(view=self.view)
return await super().on_submit(interaction=interaction)


class BlacklistRemoveDropdown(discord.ui.Select):
def __init__(self, ctx: commands.Context):
self.ctx = ctx

options = [discord.SelectOption(label="\u200b", default=False)]
self.words = []

super().__init__(
placeholder="📃 Choose words to remove from the blacklist...",
min_values=1,
max_values=len(options),
options=options
)

async def interaction_check(self, interaction: discord.Interaction) -> bool:
"""
Prevents users who weren't the command sender from using buttons.
"""
if interaction.user.id == self.ctx.author.id:
return True
else:
await interaction.response.send_message(
f"❌ {interaction.user.mention}: This isn't your interaction!",
ephemeral=True,
)
return False

async def callback(self, interaction: discord.Interaction):
await interaction.response.defer()

if not self.values:
return await interaction.followup.send(
f"❌ {interaction.user.mention}: You need to select one or more than one words to remove from the blacklist!",
ephemeral=True,
)

self.words = self.values
return


class BlacklistRemoveView(discord.ui.View):
def __init__(self, ctx: commands.Context, *, timeout: Optional[float] = 180):
super().__init__()
self.ctx = ctx
self.drop = BlacklistRemoveDropdown(ctx)
self.add_item(self.drop)

async def interaction_check(self, interaction: discord.Interaction) -> bool:
"""
Prevents users who weren't the command sender from using buttons.
"""
if interaction.user.id == self.ctx.author.id:
return True
else:
await interaction.response.send_message(
f"❌ {interaction.user.mention}: This isn't your interaction!",
ephemeral=True,
)
return False

async def disable_all_buttons(self, interaction: discord.Interaction = None):
"""
Disables all buttons.
"""
interaction = interaction or self
self.drop.disabled = True
for child in self.children:
child.disabled = True
await interaction.message.edit(view=self)
self.stop()

@discord.ui.button(
label="Enter a New Word", style=discord.ButtonStyle.blurple, emoji="💬"
)
async def send_modal(
self, interaction: discord.Interaction, button: discord.Button
):
modal = BlacklistRemoveModal(self, self.drop)
await interaction.response.send_modal(modal)

@discord.ui.button(
label="Submit Words", style=discord.ButtonStyle.green, emoji="👍🏻"
)
async def submit(self, interaction: discord.Interaction, button: discord.Button):
await interaction.response.defer()

values = [value.lower() for value in self.drop.words]
id = str(interaction.guild.id)

if not values:
print(values)

return await interaction.followup.send(
f"❌ {interaction.user.mention}: You need to have at least one word selected!",
ephemeral=True,
)

blacklist = interaction.client.cache.blacklist

if id not in blacklist.keys():
return await interaction.followup.send(
f"❌ {interaction.user.mention}: This server does not have any words blacklisted.",
ephemeral=True
)

print(values)
words = set(values) & set(blacklist[id])
print(values, words)

if not words:
return await interaction.followup.send(
f"❌ {interaction.user.mention}: Sorry. Those words are not in the blacklist.",
ephemeral=True,
)

for word in words:
blacklist[id].remove(word)

interaction.client.update_json(FILEPATH, blacklist)

embed = discord.Embed(
title=f"🛠️ Words successfully removed.",
description=" ".join(f"`{word}`" for word in words),
)

if len(values) > len(words):
embed.set_footer(text="⚠️ Some words were duplicates and were not added.")

await interaction.followup.send(embed=embed)

@discord.ui.button(label="Abort", style=discord.ButtonStyle.red, emoji="👎🏻")
async def abort(self, interaction: discord.Interaction, button: discord.Button):
await interaction.response.send_message(
f"❌ {interaction.user.mention}: Aborting command!", ephemeral=True
)
await self.disable_all_buttons(interaction)

0 comments on commit ccc5d77

Please sign in to comment.