-
Notifications
You must be signed in to change notification settings - Fork 10
/
module_pin.lua
218 lines (186 loc) · 5.4 KB
/
module_pin.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
213
214
215
216
217
218
-- 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
local bit = require("bit")
Module.Name = "pin"
function Module:GetConfigTable()
return {
{
Name = "Trigger",
Description = "Triggering emoji",
Type = bot.ConfigType.Emoji,
Default = "pushpin"
},
{
Array = true,
Name = "DisabledChannels",
Description = "Channels where emoji pin is disabled",
Type = bot.ConfigType.Channel,
Default = {}
},
{
Name = "PinThreshold",
Description = "How many pin emoji are required to pin a message",
Type = bot.ConfigType.Integer,
Default = 10
},
{
Name = "AlertChannel",
Description = "Channel where a message will be posted (if set) once a message is auto-pinned",
Type = bot.ConfigType.Channel,
Optional = true
}
}
end
function Module:HandleEmojiAdd(config, reaction)
if (reaction.count >= config.PinThreshold) then
local message = reaction.message
if (not message.pinned) then
if (not message:pin()) then
self:LogError(message.guild, "Failed to pin message %s in channel %s", message.id, message.channel.id)
return
end
if (config.AlertChannel) then
local alertChannel = message.guild:getChannel(config.AlertChannel)
if (not alertChannel) then
self:LogWarning(message.guild, "Invalid alert channel")
return
end
local author = message.author
local content = message.content
if (#content > 1800) then
content = content:sub(1, 1800) .. "... <truncated>"
end
alertChannel:send({
content = string.format("A message has been auto-pinned in %s:\n%s", message.channel.mentionString, Bot:GenerateMessageLink(message)),
embed = {
author = {
name = author.tag,
icon_url = author.avatarURL
},
thumbnail = {
url = author.avatarURL
},
description = content,
footer = {
text = string.format("in #%s at %s", message.channel.name, message.guild.name)
},
timestamp = message.timestamp
}
})
end
end
end
end
function Module:OnReactionAdd(reaction, userId)
local channel = reaction.message.channel
if (not bot:IsPublicChannel(channel)) then
return
end
local guild = reaction.message.guild
local config = self:GetConfig(guild)
local emojiData = bot:GetEmojiData(guild, reaction.emojiId or reaction.emojiName)
if (not emojiData) then
return
end
if (emojiData.Name ~= config.Trigger or (emojiData.Custom and emojiData.FromGuild ~= guild)) then
return
end
for _, disabledChannelId in pairs(config.DisabledChannels) do
if (channel.id == disabledChannelId) then
return
end
end
self:HandleEmojiAdd(config, reaction)
end
function Module:OnReactionAddUncached(channel, messageId, reactionIdorName, userId)
if (not bot:IsPublicChannel(channel)) then
return
end
local guild = channel.guild
local config = self:GetConfig(guild)
local emojiData = bot:GetEmojiData(guild, reactionIdorName)
if (not emojiData) then
return
end
if (emojiData.Name ~= config.Trigger or (emojiData.Custom and emojiData.FromGuild ~= guild)) then
return
end
for _, disabledChannelId in pairs(config.DisabledChannels) do
if (channel.id == disabledChannelId) then
return
end
end
local message = channel:getMessage(messageId)
if (not message) then
return -- Maybe message has been deleted
end
local reaction = message.reactions:get(reactionIdorName)
if (not reaction) then
return -- Maybe reaction has been removed
end
self:HandleEmojiAdd(config, reaction)
end
function Module:OnLoaded()
self:RegisterCommand({
Name = "pin",
Args = {
{ Name = "<messageId>", Type = bot.ConfigType.Message },
},
Help = function (guild) return Bot:Format(guild, "PIN_PIN_HELP") end,
Silent = true,
Func = function (commandMessage, targetMessage)
local guild = commandMessage.guild
local sender = commandMessage.member
local senderId = sender.id
local channelOwnerId = commandMessage.channel._owner_id
if (targetMessage.pinned) then
return
end
if (not sender:hasPermission(enums.permission.manageMessages) and senderId ~= channelOwnerId) then
return
end
if (targetMessage.channel.id ~= commandMessage.channel.id) then
commandMessage:reply(Bot:Format(guild, "PIN_PIN_ERROR"))
return
end
local res = targetMessage:pin()
if not res then
commandMessage:reply(Bot:Format(guild, "PIN_PIN_ERROR"))
end
end
})
self:RegisterCommand({
Name = "unpin",
Args = {
{ Name = "<messageId>", Type = bot.ConfigType.Message },
},
Help = function (guild) return Bot:Format(guild, "PIN_UNPIN_HELP") end,
Silent = true,
Func = function (commandMessage, targetMessage)
local guild = commandMessage.guild
local sender = commandMessage.member
local senderId = sender.id
local channelOwnerId = commandMessage.channel._owner_id
if (not targetMessage.pinned) then
return
end
if (not sender:hasPermission(enums.permission.manageMessages) and senderId ~= channelOwnerId) then
return
end
if (targetMessage.channel.id ~= commandMessage.channel.id) then
commandMessage:reply(Bot:Format(guild, "PIN_UNPIN_ERROR"))
return
end
local res = targetMessage:unpin()
if not res then
commandMessage:reply(Bot:Format(guild, "PIN_UNPIN_ERROR"))
end
end
})
return true
end