-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
BotServerSettings.py
188 lines (148 loc) · 8.38 KB
/
BotServerSettings.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from discord import ui, Guild, ButtonStyle, Interaction, Member, TextChannel, Permissions
from ModalHelpers import YesNoSelector, SelfDeletingView, ModChannelSelector
from BotDatabaseSchema import Server
from Logger import Logger, LogLevel
from Config import Config
class BotSettingsPayload:
User:Member = None
WebHookRequired:bool = False
KickSusRequired:bool = False
# These settings should get pulled from the db
Server:Guild = None
MessageChannel:TextChannel = None
WantsWebhooks:bool = False
KickSusUsers:bool = False
def GetServerID(self) -> int:
if (self.Server is None):
return 0
return self.Server.id
def GetUserID(self) -> int:
if (self.User is None):
return 0
return self.User.id
def HasMessageChannel(self) -> bool:
return self.MessageChannel is not None
def GetMessageID(self) -> int:
if (not self.HasMessageChannel()):
return 0
return self.MessageChannel.id
def LoadFromDB(self, BotInstance):
DB = BotInstance.Database
ServerInfo:Server = DB.GetServerInfo(self.Server.id)
if (ServerInfo.activation_state == 0):
self.KickSusRequired = self.WebHookRequired = True
else:
self.WantsWebhooks = ServerInfo.has_webhooks
self.KickSusUsers = ServerInfo.kick_sus_users
# Check to see what the setting is for messaging channel, if it's 0, leave MessageChannel as None
# else load up the text channel value
if (ServerInfo.message_channel != 0):
self.MessageChannel = BotInstance.get_channel(ServerInfo.message_channel)
class InstallWebhookSelector(YesNoSelector):
def GetYesDescription(self) -> str:
return "Yes, install the ban notification webhook"
def GetNoDescription(self) -> str:
return "No, do not install the ban notification webhook"
def GetPlaceholder(self) -> str:
return "ScamGuard Ban Notifications"
def SetNotRequiredIfValueSet(self) -> bool:
return True
class KickSuspiciousUsersSelector(YesNoSelector):
def GetYesDescription(self) -> str:
return "Yes, kick any suspicious users. These are usually users that have sent numerous friend requests upon joining servers, or mass DMs"
def GetNoDescription(self) -> str:
return "No, do not kick any suspicious users automatically. This may have false positives!"
def GetPlaceholder(self) -> str:
return "Kick Suspicious Accounts"
def SetNotRequiredIfValueSet(self) -> bool:
return True
class ServerSettingsView(SelfDeletingView):
ChannelSelect:ModChannelSelector = None
WebhookSelector:InstallWebhookSelector = None
SuspiciousUserKicks:KickSuspiciousUsersSelector = None
CallbackFunction = None
Payload:BotSettingsPayload = None
def __init__(self, InCB, interaction: Interaction):
super().__init__()
ConfigData:Config = Config()
# Pull current data
self.Payload = BotSettingsPayload()
self.Payload.Server = interaction.guild
self.Payload.User = interaction.user
self.Payload.LoadFromDB(interaction.client)
self.ChannelSelect = ModChannelSelector(RowPos=0)
# If we don't have a message channel selected, force this setting here.
if (not self.Payload.HasMessageChannel()):
self.ChannelSelect.SetRequired()
self.add_item(self.ChannelSelect)
if (ConfigData["AllowWebhookInstall"]):
self.WebhookSelector = InstallWebhookSelector(RowPos=1)
if (not self.Payload.WebHookRequired):
self.WebhookSelector.SetCurrentValue(self.Payload.WantsWebhooks)
self.add_item(self.WebhookSelector)
if (ConfigData["AllowSuspiciousUserKicks"]):
self.SuspiciousUserKicks = KickSuspiciousUsersSelector(RowPos=2)
if (not self.Payload.KickSusRequired):
self.SuspiciousUserKicks.SetCurrentValue(self.Payload.KickSusUsers)
self.add_item(self.SuspiciousUserKicks)
self.CallbackFunction = InCB
@ui.button(label="Confirm Settings", style=ButtonStyle.success, row=4)
async def setup(self, interaction: Interaction, button: ui.Button):
# Couple of quick reference settings
DB = interaction.client.Database
ServerId:int = self.Payload.GetServerID()
ConfigData:Config = Config()
# State settings
MadeWebhookSelection:bool = self.WebhookSelector.HasValue()
ChannelSelectRequired:bool = self.ChannelSelect.min_values == 1
ChannelSelectChanged:bool = False
# Check if we can install webhooks
if (ConfigData["AllowWebhookInstall"]):
if (MadeWebhookSelection):
self.Payload.WantsWebhooks = self.WebhookSelector.GetValue()
elif self.WebhookSelector.IsRequired():
await interaction.response.send_message("Please choose an option for ban notifications!", ephemeral=True, delete_after=10.0)
return
# Check to see if the channel option has changed. This code specifically will allow it for the user to not change the setting and still
# use the old values
if (not ChannelSelectRequired):
CurrentChannelSetting:int|None = DB.GetChannelIdForServer(ServerId)
# Grab what the user selected if they have any selections
NewChannelSetting:int|None = self.ChannelSelect.values[0].id if self.ChannelSelect.values else None
# If this is not required, and the user has made a selection and the selection is not the current setting, then do an update.
if (NewChannelSetting is not None and CurrentChannelSetting != NewChannelSetting):
Logger.Log(LogLevel.Debug, f"Channel Selection has changed from {CurrentChannelSetting} to {NewChannelSetting}")
ChannelSelectRequired = True
ChannelSelectChanged = True
# Resolve the selected channel to send messages into
if (ChannelSelectRequired):
if (await self.ChannelSelect.IsValid(interaction, True) == False):
return
ChannelToHookInto:TextChannel = self.ChannelSelect.values[0].resolve()
if (self.Payload.WantsWebhooks):
# If the channel selection option has changed from the original setting, delete the original webhook
if (ChannelSelectChanged):
Logger.Log(LogLevel.Debug, "Deleting old webhook reference")
await interaction.client.DeleteWebhook(ServerId)
BotMember:Member = interaction.guild.get_member(interaction.client.user.id)
PermissionsObj:Permissions = ChannelToHookInto.permissions_for(BotMember)
# Check to see if we can manage webhooks in that channel, if the user wants us to add ban notifications
if (not PermissionsObj.manage_webhooks):
await interaction.response.send_message(f"ScamGuard needs permissions to add a webhook into the channel {ChannelToHookInto.mention}, please give it manage webhook permissions",
ephemeral=True, delete_after=80.0)
return
# The user wanted webhooks but doesn't want them any more, delete the webhook from the channel.
elif (self.WebhookSelector.HasValueChanged() and self.Payload.HasMessageChannel()):
await interaction.client.DeleteWebhook(ServerId)
self.Payload.MessageChannel = ChannelToHookInto
self.HasInteracted = True
# Push a message to the activation request channel
await self.CallbackFunction(self.Payload)
# Respond to the user and kill the interactions
MessageResponse:str = ""
if (not interaction.client.Database.IsActivatedInServer(ServerId)):
MessageResponse = "Enqueued your server for activation. This will take a few minutes to import all the bans."
else:
MessageResponse = "Settings changes enqueued for application!"
await interaction.response.send_message(MessageResponse, ephemeral=True, delete_after=30.0)
await self.StopInteractions()