-
Notifications
You must be signed in to change notification settings - Fork 10
/
module_voice.lua
212 lines (169 loc) · 5.9 KB
/
module_voice.lua
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
-- Copyright (C) 2024 Mjöllnir#3515
-- This file is part of the "Not a Bot" application
-- For conditions of distribution and use, see copyright notice in LICENSE
--[[
This module allows a server member to create a private voice channel that cannot be joined by other members by
default.
The channel owner has the ability to invite other members to join.
Administrators and authorized roles can still join this type of channel without an invitation from the owner.
Usage:
When a member joins a specific voice channel, a private voice channel is created with the appropriate
permissions. The member is then moved there and becomes the owner of the channel. The channel owner can
invite other members to join their channel.
The private voice channel is automatically deleted when the channel owner disconnects.
All roles listed in AuthorizedRoles will recieve the same permissions as the channel owner.
]]
--[[
Storage Model
{
"PrivateVoiceChannels": {
"<ChannelId>" : "<UserId>",
...
}
}
]]
Module.Name = 'voice'
local EnumPermission = Discordia.enums.permission
local EnumComponent = Discordia.enums.componentType
local EnumInteractionType = Discordia.enums.interactionResponseType
local EnumInteractionFlag = Discordia.enums.interactionResponseFlag
local selectInteractionId = 'private_voice_invite'
function Module:GetConfigTable()
return {
{
Name = 'TriggerChannel',
Description = 'The voice channel that members must join in order to create a private voice channel',
Type = Bot.ConfigType.Channel,
Default = false,
},
{
Name = 'AuthorizedRoles',
Description = 'Authorized roles to join a private voice channel',
Type = Bot.ConfigType.Role,
Default = {},
Array = true,
Optional = true,
}
}
end
function Module:OnEnable(guild)
local config = self:GetConfig(guild)
if not config.TriggerChannel then
return false, Bot:Format(guild, 'VOICE_MISCONFIG')
end
local persistentData = self:GetPersistentData(guild)
if not persistentData.PrivateVoiceChannels then
persistentData.PrivateVoiceChannels = {}
return true
end
-- cleanup config and unused channels after reboot
for channelId, ownerId in pairs(persistentData.PrivateVoiceChannels) do
local channel = guild.voiceChannels:find(
function(voice) if voice.id == channelId then return voice end end
)
if channel then
local isOwnerConnected = channel.connectedMembers:find(
function(member) return member.id == ownerId end
)
if not isOwnerConnected then
channel:delete()
persistentData.PrivateVoiceChannels[channelId] = nil
Client:info(string.format('[%s][%s] Deleted an unused channel %s', guild.name, Module.Name, channelId))
end
else
persistentData.PrivateVoiceChannels[channelId] = nil
end
end
Bot:Save()
return true
end
function Module:OnvoiceChannelJoin(member, channel)
local guild = channel.guild
local category = channel.category
local config = self:GetConfig(guild)
local triggerChannelId = config.TriggerChannel
if channel.id ~= triggerChannelId then
return
end
local data = self:GetPersistentData(guild)
local privateVoice
local privateVoiceName = Bot:Format(guild, 'VOICE_CHAN_PREFIX') .. member.name
if category then
privateVoice = category:createVoiceChannel(privateVoiceName)
else
privateVoice = guild:createVoiceChannel(privateVoiceName)
end
local everyoneFilter = function(role) if role.name == '@everyone' then return role end end
local everyone = guild.roles:find(everyoneFilter)
local everyonePermissions = privateVoice:getPermissionOverwriteFor(everyone)
everyonePermissions:denyPermissions(EnumPermission.connect)
local ownerPermissions = privateVoice:getPermissionOverwriteFor(member)
ownerPermissions:allowPermissions(
EnumPermission.connect,
EnumPermission.moveMembers,
EnumPermission.setVoiceChannelStatus
)
for _, roleId in ipairs(config.AuthorizedRoles) do
local role = guild:getRole(roleId)
local rolePermissions = privateVoice:getPermissionOverwriteFor(role)
rolePermissions:allowPermissions(
EnumPermission.connect,
EnumPermission.moveMembers,
EnumPermission.setVoiceChannelStatus
)
end
member:setVoiceChannel(privateVoice.id)
local rowComponent = {
type = EnumComponent.actionRow,
components = {
{
type = EnumComponent.userSelect,
custom_id = selectInteractionId,
placeholder = Bot:Format(guild, 'VOICE_INTERAC_PLACEHOLDER'),
}
}
}
local messageData = {
content = Bot:Format(guild, 'VOICE_MSG'),
components = { rowComponent }
}
privateVoice:send(messageData)
data.PrivateVoiceChannels[privateVoice.id] = member.id
Bot:Save()
end
function Module:OnInteractionCreate(interaction)
local interactionId = interaction.data.custom_id
if interactionId ~= selectInteractionId then
return
end
-- cannot use interaction.guild because it's a partial object
local guild = Client:getGuild(interaction.guild)
local channel = interaction.channel
local selectionnedMembers = interaction.data.resolved.members
-- cannot use map values (member) because they are partial objects
for memberId, _ in pairs(selectionnedMembers) do
local member = guild:getMember(memberId)
local memberPermissions = channel:getPermissionOverwriteFor(member)
memberPermissions:allowPermissions(EnumPermission.connect)
end
return interaction:respond({
type = EnumInteractionType.channelMessageWithSource,
data = {
content = Bot:Format(guild, 'VOICE_CONFIRM'),
flags = EnumInteractionFlag.ephemeral
}
})
end
function Module:OnvoiceChannelLeave(member, channel)
local guild = channel.guild
local data = self:GetPersistentData(guild)
if not data.PrivateVoiceChannels[channel.id] then
return
end
if member.id == data.PrivateVoiceChannels[channel.id] then
-- will throw an HTTP error if a member with "manage" permission deletes the channel while connected
channel:delete()
data.PrivateVoiceChannels[channel.id] = nil
Bot:Save()
end
end