-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmodule_quote.lua
153 lines (130 loc) · 4.1 KB
/
module_quote.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
-- 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 prefix = Config.Prefix
Module.Name = "quote"
function Module:GetConfigTable()
return {
{
Name = "AutoQuote",
Description = "Should quote messages when an user posts a message link (when not using quote command)",
Type = bot.ConfigType.Boolean,
Default = true
},
{
Name = "BigAvatar",
Description = "Should quote messages include big avatars",
Type = bot.ConfigType.Boolean,
Default = false
},
{
Name = "DeleteInvokationOnAutoQuote",
Description = "Deletes the message that invoked the quote when auto-quoting",
Type = bot.ConfigType.Boolean,
Default = false
},
{
Name = "DeleteInvokationOnManualQuote",
Description = "Deletes the message that invoked the quote when quoting via command",
Type = bot.ConfigType.Boolean,
Default = false
}
}
end
function Module:OnLoaded()
self:RegisterCommand({
Name = "quote",
Args = {
{
Name = "message", Type = bot.ConfigType.String
},
{
Name = "deleteInvokation",
Type = bot.ConfigType.Boolean,
Optional = true
}
},
Help = "Quote message",
Func = function (commandMessage, message, deleteInvokation)
local messageId = message:match("^(%d+)$")
local quotedMessage, err
local includesLink = false
local config = self:GetConfig(commandMessage.guild)
if (messageId) then
quotedMessage = commandMessage.channel:getMessage(messageId)
if (not quotedMessage) then
commandMessage:reply("Message not found in this channel")
return
end
includesLink = true
else
quotedMessage, err = bot:DecodeMessage(message, false)
if (not quotedMessage) then
commandMessage:reply(string.format("Invalid message link: %s", err))
return
end
-- Checks if user has permission to see this message
if (not self:CheckReadPermission(commandMessage.author, quotedMessage)) then
commandMessage:reply("You can only quote messages you are able to see yourself")
return
end
end
-- Only delete the message that invoked the quote if no argument to the command is passed
-- and auto deletion is set true, or if command argument is specifically set to true
local shouldDeleteInvokation = deleteInvokation == nil and config.DeleteInvokationOnManualQuote or deleteInvokation
self:QuoteMessage(commandMessage, quotedMessage, includesLink, shouldDeleteInvokation)
end
})
return true
end
function Module:CheckReadPermission(user, message)
local quotedGuild = message.guild
local member = quotedGuild:getMember(user.id)
if (not member) then
return false
end
if (not member:hasPermission(message.channel, enums.permission.viewChannel)) then
return false
end
return true
end
function Module:QuoteMessage(triggeringMessage, message, includesLink, deleteInvokation)
local config = self:GetConfig(triggeringMessage.guild)
local embed = Bot:BuildQuoteEmbed(message, { bigAvatar = config.BigAvatar })
embed.footer = {
text = string.format("Quoted by %s | in #%s at %s", triggeringMessage.author.tag, message.channel.name, message.guild.name)
}
triggeringMessage:reply({
content = includesLink and "Message link: " .. Bot:GenerateMessageLink(message) or nil,
embed = embed,
reference = not deleteInvokation and { message = triggeringMessage } or nil
})
if (deleteInvokation) then
triggeringMessage:delete()
end
end
function Module:OnMessageCreate(message)
if (not bot:IsPublicChannel(message.channel)) then
return
end
if (message.author.bot) then
return
end
if (message.content:startswith(prefix, true)) then
return
end
local config = self:GetConfig(message.guild)
if (config.AutoQuote) then
local quotedMessage = bot:DecodeMessage(message.content, true)
if (quotedMessage) then
if (not self:CheckReadPermission(message.author, quotedMessage)) then
return
end
self:QuoteMessage(message, quotedMessage, false, config.DeleteInvokationOnAutoQuote)
end
end
end