-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConfirmBanView.py
More file actions
83 lines (71 loc) · 3.45 KB
/
Copy pathConfirmBanView.py
File metadata and controls
83 lines (71 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Discord View Modal for Confirming Moderation Actions
from Logger import Logger, LogLevel
from BotEnums import BanAction, ModerationAction
from discord import ui, ButtonStyle, Interaction, Member, User, ForumTag, Thread, ForumChannel
from ModalHelpers import SelfDeletingView
from Config import Config
from typing import cast
ConfigData:Config = Config()
class ConfirmBan(SelfDeletingView):
TargetId:int = 0
TargetReason:str|None = None
BotInstance = None
def __init__(self, target:int, bot, reason:str|None=None):
super().__init__(ViewTimeout=90.0)
self.TargetId = target
self.BotInstance = bot
self.TargetReason = reason
async def on_cancel(self, interaction:Interaction):
await interaction.response.send_message("This action was cancelled.", ephemeral=True, delete_after=10.0)
async def AddTag(self, thread: Thread, Action: BanAction):
# Attempt to set the forum handled/duplicate tag because this is really annoying otherwise
try:
TagToApply:ForumTag
TagToFind:str = ""
# What kind of tag are we looking for
if (Action == BanAction.Banned):
TagToFind = ConfigData["ReportHandledTag"]
elif (Action == BanAction.Duplicate):
TagToFind = ConfigData["ReportDuplicateTag"]
else:
return
# Because ScamGuard runs in both the web report and the standard report channel,
# we need to dynamically scan the tags. It would probably be smarter to cache them, maybe later.
for tag in cast(ForumChannel, thread.parent).available_tags:
if (tag.name == TagToFind):
TagToApply = tag
break
# Check to see if the tag is not already applied.
if (TagToApply not in thread.applied_tags):
await thread.add_tags(TagToApply, reason=f"Adding tag '{TagToFind}'")
else:
return
except Exception as ex:
Logger.Log(LogLevel.Warn, f"Could not set the handled tag in {thread.id} {str(ex)}")
@ui.button(label="Confirm Ban", style=ButtonStyle.danger, row=4)
async def confirm(self, interaction: Interaction, button: ui.Button):
# Prevent pressing the button multiple times during asynchronous action.
if (self.HasInteracted):
return
Sender:Member|User = interaction.user
ResponseMsg:str = ""
if (self.BotInstance is None):
Logger.Log(LogLevel.Error, "ConfirmBan view has an invalid bot reference!!")
return
await interaction.response.defer(thinking=True)
self.HasInteracted = True
Result:BanAction = await self.BotInstance.HandleBanAction(self.TargetId, Sender, ModerationAction.Ban,
ThreadId=interaction.channel_id, Reason=self.TargetReason)
if (Result is not BanAction.Banned):
if (Result == BanAction.Duplicate):
ResponseMsg = f"{self.TargetId} already exists in the ban database"
Logger.Log(LogLevel.Log, f"The given id {self.TargetId} is already banned.")
else:
ResponseMsg = f"The given id {self.TargetId} had an error while banning!"
Logger.Log(LogLevel.Warn, f"{Sender} attempted ban on {self.TargetId} with error {str(Result)}")
else:
ResponseMsg = f"{interaction.user.mention}, the ban for {self.TargetId} is now in progress..."
await self.AddTag(cast(Thread, interaction.channel), Result)
# Make this message silent as we may include an @ mention in here and do not want to bother the user with notifications
await interaction.followup.send(ResponseMsg, silent=True)
await self.StopInteractions()