Skip to content

Commit f3a006b

Browse files
authored
Merge pull request #11 from matthewflegg/feature-blclear
Added a clear blacklist command
2 parents a7cab54 + 2618d0d commit f3a006b

File tree

5 files changed

+78
-6
lines changed

5 files changed

+78
-6
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"cSpell.words": [
33
"beepboop",
4+
"bladd",
5+
"blclear",
46
"Boop",
57
"bruv",
68
"cmds",

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ Sends an embed linking to **this page**.
4343
Clears a specified number of messages from a text channel.<br>
4444
**Requires**: `Manage Messages`
4545

46-
> **~blacklist `word`**
46+
> **~blacklist | ~bladd `word`**
4747
4848
Adds the word to a list of words that are disallowed. Any messages containing these words will be deleted.<br>
4949
**Requires**: `Manage Messages`
5050

51+
> **~clearblacklist | ~blclear**
52+
53+
Clears the blacklist for the server.<br>
54+
**Requires**: `Manage Messages`
55+
5156
> **~kick `member` `reason?`**
5257
5358
Kicks a specified user from the server.<br>

modules/admin.py

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Contains commands relating to administrator tasks.
33
"""
44

5+
import black
56
import discord.ext.commands as commands
67
import discord.ui as UI
78
import helpers
@@ -143,7 +144,7 @@ async def unban(ctx: commands.Context, user: discord.User):
143144
await helpers.lift_ban(ctx, "permanent", user)
144145

145146

146-
@commands.command()
147+
@commands.command(aliases=['bladd'])
147148
@commands.has_permissions(manage_messages=True)
148149
@commands.cooldown(1, 2, commands.BucketType.user)
149150
async def blacklist(ctx: commands.Context, *, word: str):
@@ -161,6 +162,7 @@ async def blacklist(ctx: commands.Context, *, word: str):
161162
"""
162163
filepath = 'files/blacklist.json'
163164
id = str(ctx.guild.id)
165+
word = word.lower()
164166

165167
with open(filepath, 'r') as file:
166168
blacklist: dict = json.load(file)
@@ -180,6 +182,61 @@ async def blacklist(ctx: commands.Context, *, word: str):
180182
await ctx.send(f':tools: \'{word}\' has been added to the blacklist.')
181183

182184

185+
@commands.command(aliases=['blclear'])
186+
@commands.has_permissions(manage_messages=True)
187+
async def clearblacklist(ctx: commands.Context):
188+
"""
189+
Clears the blacklist for a server.
190+
191+
Parameters
192+
----------
193+
194+
ctx (Context):
195+
Command invocation context.
196+
"""
197+
filepath = 'files/blacklist.json'
198+
id = str(ctx.guild.id)
199+
200+
with open(filepath) as file:
201+
blacklist: dict = json.load(file)
202+
203+
mention = ctx.author.mention
204+
205+
if id not in blacklist.keys():
206+
return await ctx.send(f':x: {mention}: This server does not have any words blacklisted.')
207+
208+
yes_button = UI.Button(label='Yes', style=discord.ButtonStyle.green, emoji='👍🏻')
209+
no_button = UI.Button(label='No', style=discord.ButtonStyle.red, emoji='👎🏻')
210+
211+
async def yes(interaction: discord.Interaction):
212+
for server_id in blacklist.keys():
213+
if server_id == id:
214+
del blacklist[server_id]
215+
216+
with open(filepath, 'w') as file:
217+
json.dump(blacklist, file, indent=4)
218+
219+
return await interaction.message.channel.send(f':thumbsup: {mention}: The blacklist for this server'
220+
+ f'has successfully been deleted.')
221+
222+
yes_button.callback = yes
223+
224+
async def no(interaction: discord.Interaction):
225+
return await interaction.message.channel.send(f':thumbsup: {mention}: Ok!')
226+
227+
no_button.callback = no
228+
229+
view = UI.View()
230+
view.add_item(yes_button)
231+
view.add_item(no_button)
232+
233+
await ctx.send(
234+
f':warning: {mention}: Are you sure you\'d like to clear your server\'s blacklist?\n'
235+
+ f'This action cannot be undone.',
236+
view=view
237+
)
238+
239+
183240
# |----- REGISTERING MODULE -----|
184241

185242

@@ -193,4 +250,13 @@ def setup(client: commands.Bot):
193250
client (Bot):
194251
Client instance, to add the commands to.
195252
"""
196-
helpers.add_commands(client, clear, kick, ban, softban, unban, blacklist)
253+
helpers.add_commands(
254+
client,
255+
clear,
256+
kick,
257+
ban,
258+
softban,
259+
unban,
260+
blacklist,
261+
clearblacklist
262+
)

modules/roles.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async def clean_json_file(self):
5151
json.dump(data, file, indent=4)
5252

5353
@commands.Cog.listener()
54-
async def on_message_delete(message: discord.Message):
54+
async def on_message_delete(self, message: discord.Message):
5555
"""
5656
Deletes reaction role messages that correspond to a deleted role. Removes the
5757
entry for that reaction role in the JSON file.
@@ -62,7 +62,6 @@ async def on_message_delete(message: discord.Message):
6262
message (Message):
6363
The message that was sent.
6464
"""
65-
6665
with open(FILEPATH) as file:
6766
data: list = json.load(file)
6867

start.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
prefix = "~"
2525
colour = 0x486572
2626
status = itertools.cycle(['~help', '~ai', '~play'])
27-
client = commands.Bot(prefix, intents=intents, help_command=None) # Help command = none so we can override it
27+
client = commands.Bot(prefix, intents=intents, help_command=None, case_insensitive=True) # Help command = none so we can override it
2828

2929
PATH = './modules'
3030
sys.path.append(PATH)

0 commit comments

Comments
 (0)