-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmodule_kick.lua
More file actions
164 lines (144 loc) · 4.84 KB
/
Copy pathmodule_kick.lua
File metadata and controls
164 lines (144 loc) · 4.84 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
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
-- Copyright (C) 2018 Jérôme Leclercq
-- This file is part of the "Not a Bot" application
-- For conditions of distribution and use, see copyright notice in LICENSE
local client = Client
local discordia = Discordia
local bot = Bot
local enums = discordia.enums
Module.Name = "kick"
function Module:CheckPermissions(member)
local config = self:GetConfig(member.guild)
for _,roleId in pairs(config.AuthorizedRoles) do
if (member:hasRole(roleId)) then
return true
end
end
return member:hasPermission(enums.permission.kickMembers)
end
function Module:GetConfigTable()
return {
{
Name = "PrivateMessage",
Description = "If set, the bot will try to send a private message before kicking the user.\nAvailable variables: {guild}, {user}, {reason}.",
Type = bot.ConfigType.String,
Default = "You have been kicked from {guild} by {user}: {reason}",
Optional = true
},
{
Name = "AuthorizedRoles",
Description = "Roles which can use the kick command (not required if user/role has kick member permission)",
Type = bot.ConfigType.Role,
Default = {},
Array = true
}
}
end
function Module:PerformKick(guild, kickedBy, targetMember, reason)
local config = self:GetConfig(guild)
-- Permission check
local kickedByRole = kickedBy.highestRole
local targetRole = targetMember.highestRole
if (targetRole.position >= kickedByRole.position) then
return false, "You cannot kick that user due to your lower permissions."
end
if (config.PrivateMessage) then
local privateChannel = targetMember.user:getPrivateChannel()
if (privateChannel) then
local message = config.PrivateMessage
message = message:gsub("{guild}", guild.name)
message = message:gsub("{user}", kickedBy.mentionString)
message = message:gsub("{reason}", reason or "no reason given")
privateChannel:send(message)
end
end
if (targetMember:kick(string.format("Kicked by %s%s", kickedBy.mentionString, reason and string.format(": %s", reason) or ""))) then
return true, string.format("%s has kicked %s%s", kickedBy.user.tag, targetMember.user.tag, reason and string.format(": %s", reason) or "")
else
return false, string.format("Failed to kick %s", targetMember.user.tag)
end
end
local function BuildKickModal(targetMember)
return {
type = enums.interactionResponseType.modal,
data = {
custom_id = "kick_modal_" .. targetMember.id,
title = "Kick " .. targetMember.tag,
components = {
{
type = enums.componentType.actionRow,
components = {
{
type = enums.componentType.textInput,
custom_id = "reason",
style = enums.textInputStyle.paragraph,
label = "Reason",
required = false
}
}
}
}
}
}
end
function Module:OnLoaded()
self:RegisterCommand({
Name = "kick",
Args = {
{ Name = "target", Type = Bot.ConfigType.Member, Description = "Member to kick" },
{ Name = "reason", Type = Bot.ConfigType.String, Description = "Kick reason", Optional = true }
},
PrivilegeCheck = function (member) return self:CheckPermissions(member) end,
Help = "Kicks a member",
Silent = true,
Func = function (commandMessage, targetMember, reason)
local success, text = self:PerformKick(commandMessage.guild, commandMessage.member, targetMember, reason)
commandMessage:reply(text)
end,
Slash = {
Description = "Kick a member",
DefaultMemberPermissions = enums.permission.kickMembers,
Func = function (interaction, targetMember, reason)
local success, text = self:PerformKick(interaction.guild, interaction.member, targetMember, reason)
interaction:respond({
type = enums.interactionResponseType.channelMessageWithSource,
data = { content = text }
})
end
},
ContextMenu = {
Type = "user",
DefaultMemberPermissions = enums.permission.kickMembers,
Func = function (interaction, targetMember)
interaction:respond(BuildKickModal(targetMember))
end
}
})
return true
end
function Module:OnInteractionCreate(interaction)
if (interaction.type ~= enums.interactionRequestType.modalSubmit) then
return
end
local guild = interaction.guild
if (not guild) then
return
end
local customId = interaction.data.custom_id
local kickTargetId = customId:match("^kick_modal_(%d+)$")
if (not kickTargetId) then
return
end
if (not self:CheckPermissions(interaction.member)) then
return interaction:respond({
type = enums.interactionResponseType.channelMessageWithSource,
data = { content = "You are not authorized to use this command.", flags = enums.interactionResponseFlag.ephemeral }
})
end
local fields = Bot:ParseModalFields(interaction)
local targetMember = guild:getMember(kickTargetId)
local success, text = self:PerformKick(guild, interaction.member, targetMember, fields.reason)
interaction:respond({
type = enums.interactionResponseType.channelMessageWithSource,
data = { content = text }
})
end