forked from DigitalPulseSoftware/NotaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_pin.lua
146 lines (127 loc) · 3.64 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
-- 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 (channel.type ~= enums.channelType.text) 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 (channel.type ~= enums.channelType.text) 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