1
- from typing import Optional
2
-
1
+ import black
3
2
import discord
3
+
4
+ from typing import Optional
4
5
from discord .ext import commands
5
6
6
7
FILEPATH = "files/blacklist.json"
@@ -130,7 +131,7 @@ def __init__(
130
131
view : discord .ui .View ,
131
132
drop : discord .ui .Select ,
132
133
* ,
133
- title : str = "🔑 Enter a word to blacklist: " ,
134
+ title : str = "🔒 Add a Word to the Blacklist " ,
134
135
) -> None :
135
136
super ().__init__ (title = title )
136
137
self .view = view
@@ -234,7 +235,7 @@ async def send_modal(
234
235
)
235
236
async def submit (self , interaction : discord .Interaction , button : discord .Button ):
236
237
await interaction .response .defer ()
237
- values = self .drop .words
238
+ values = [ value . lower () for value in self .drop .words ]
238
239
id = str (interaction .guild_id )
239
240
if not values :
240
241
print (values )
@@ -267,3 +268,179 @@ async def abort(self, interaction: discord.Interaction, button: discord.Button):
267
268
f"❌ { interaction .user .mention } : Aborting command!" , ephemeral = True
268
269
)
269
270
await self .disable_all_buttons (interaction )
271
+
272
+
273
+ class BlacklistRemoveModal (discord .ui .Modal ):
274
+ def __init__ (
275
+ self ,
276
+ view : discord .ui .View ,
277
+ drop : discord .ui .Select ,
278
+ * ,
279
+ title : str = "🔒 Remove a Word From the Blacklist"
280
+ ) -> None :
281
+ super ().__init__ (title = title )
282
+
283
+ self .view = view
284
+ self .drop = drop
285
+ self .text = discord .ui .TextInput (
286
+ label = "Word" , placeholder = "🔑 Enter a word to remove from the blacklist:" , max_length = 100
287
+ )
288
+
289
+ self .add_item (self .text )
290
+
291
+ async def on_submit (self , interaction : discord .Interaction ) -> None :
292
+ await interaction .response .defer ()
293
+ text_value = self .text .value
294
+
295
+ if not text_value :
296
+ return
297
+
298
+ if self .drop .options [0 ].label == "\u200b " :
299
+ self .drop .options .pop (0 )
300
+
301
+ option = discord .SelectOption (label = text_value , default = True )
302
+
303
+ self .drop .append_option (option )
304
+ self .drop .max_values = len (self .drop .options )
305
+ self .drop .words .append (text_value )
306
+
307
+ await interaction .message .edit (view = self .view )
308
+ return await super ().on_submit (interaction = interaction )
309
+
310
+
311
+ class BlacklistRemoveDropdown (discord .ui .Select ):
312
+ def __init__ (self , ctx : commands .Context ):
313
+ self .ctx = ctx
314
+
315
+ options = [discord .SelectOption (label = "\u200b " , default = False )]
316
+ self .words = []
317
+
318
+ super ().__init__ (
319
+ placeholder = "📃 Choose words to remove from the blacklist..." ,
320
+ min_values = 1 ,
321
+ max_values = len (options ),
322
+ options = options
323
+ )
324
+
325
+ async def interaction_check (self , interaction : discord .Interaction ) -> bool :
326
+ """
327
+ Prevents users who weren't the command sender from using buttons.
328
+ """
329
+ if interaction .user .id == self .ctx .author .id :
330
+ return True
331
+ else :
332
+ await interaction .response .send_message (
333
+ f"❌ { interaction .user .mention } : This isn't your interaction!" ,
334
+ ephemeral = True ,
335
+ )
336
+ return False
337
+
338
+ async def callback (self , interaction : discord .Interaction ):
339
+ await interaction .response .defer ()
340
+
341
+ if not self .values :
342
+ return await interaction .followup .send (
343
+ f"❌ { interaction .user .mention } : You need to select one or more than one words to remove from the blacklist!" ,
344
+ ephemeral = True ,
345
+ )
346
+
347
+ self .words = self .values
348
+ return
349
+
350
+
351
+ class BlacklistRemoveView (discord .ui .View ):
352
+ def __init__ (self , ctx : commands .Context , * , timeout : Optional [float ] = 180 ):
353
+ super ().__init__ ()
354
+ self .ctx = ctx
355
+ self .drop = BlacklistRemoveDropdown (ctx )
356
+ self .add_item (self .drop )
357
+
358
+ async def interaction_check (self , interaction : discord .Interaction ) -> bool :
359
+ """
360
+ Prevents users who weren't the command sender from using buttons.
361
+ """
362
+ if interaction .user .id == self .ctx .author .id :
363
+ return True
364
+ else :
365
+ await interaction .response .send_message (
366
+ f"❌ { interaction .user .mention } : This isn't your interaction!" ,
367
+ ephemeral = True ,
368
+ )
369
+ return False
370
+
371
+ async def disable_all_buttons (self , interaction : discord .Interaction = None ):
372
+ """
373
+ Disables all buttons.
374
+ """
375
+ interaction = interaction or self
376
+ self .drop .disabled = True
377
+ for child in self .children :
378
+ child .disabled = True
379
+ await interaction .message .edit (view = self )
380
+ self .stop ()
381
+
382
+ @discord .ui .button (
383
+ label = "Enter a New Word" , style = discord .ButtonStyle .blurple , emoji = "💬"
384
+ )
385
+ async def send_modal (
386
+ self , interaction : discord .Interaction , button : discord .Button
387
+ ):
388
+ modal = BlacklistRemoveModal (self , self .drop )
389
+ await interaction .response .send_modal (modal )
390
+
391
+ @discord .ui .button (
392
+ label = "Submit Words" , style = discord .ButtonStyle .green , emoji = "👍🏻"
393
+ )
394
+ async def submit (self , interaction : discord .Interaction , button : discord .Button ):
395
+ await interaction .response .defer ()
396
+
397
+ values = [value .lower () for value in self .drop .words ]
398
+ id = str (interaction .guild .id )
399
+
400
+ if not values :
401
+ print (values )
402
+
403
+ return await interaction .followup .send (
404
+ f"❌ { interaction .user .mention } : You need to have at least one word selected!" ,
405
+ ephemeral = True ,
406
+ )
407
+
408
+ blacklist = interaction .client .cache .blacklist
409
+
410
+ if id not in blacklist .keys ():
411
+ return await interaction .followup .send (
412
+ f"❌ { interaction .user .mention } : This server does not have any words blacklisted." ,
413
+ ephemeral = True
414
+ )
415
+
416
+ print (values )
417
+ words = set (values ) & set (blacklist [id ])
418
+ print (values , words )
419
+
420
+ if not words :
421
+ return await interaction .followup .send (
422
+ f"❌ { interaction .user .mention } : Sorry. Those words are not in the blacklist." ,
423
+ ephemeral = True ,
424
+ )
425
+
426
+ for word in words :
427
+ blacklist [id ].remove (word )
428
+
429
+ interaction .client .update_json (FILEPATH , blacklist )
430
+
431
+ embed = discord .Embed (
432
+ title = f"🛠️ Words successfully removed." ,
433
+ description = " " .join (f"`{ word } `" for word in words ),
434
+ )
435
+
436
+ if len (values ) > len (words ):
437
+ embed .set_footer (text = "⚠️ Some words were duplicates and were not added." )
438
+
439
+ await interaction .followup .send (embed = embed )
440
+
441
+ @discord .ui .button (label = "Abort" , style = discord .ButtonStyle .red , emoji = "👎🏻" )
442
+ async def abort (self , interaction : discord .Interaction , button : discord .Button ):
443
+ await interaction .response .send_message (
444
+ f"❌ { interaction .user .mention } : Aborting command!" , ephemeral = True
445
+ )
446
+ await self .disable_all_buttons (interaction )
0 commit comments