-
Notifications
You must be signed in to change notification settings - Fork 10
/
module_welcome.lua
141 lines (129 loc) · 4.01 KB
/
module_welcome.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
local client = Client
local discordia = Discordia
local bot = Bot
Module.Name = "welcome"
function Module:GetConfigTable()
return {
{
Name = "WelcomeChannel",
Description = "Channel where join/leave will be posted",
Type = bot.ConfigType.Channel,
Optional = true
},
{
Name = "BanChannel",
Description = "Channel where ban/unban will be posted",
Type = bot.ConfigType.Channel,
Optional = true
},
{
Name = "JoinMessage",
Description = "Message to be posted when a user joins the server (`{userMention}` will be replaced by the user mention string)",
Type = bot.ConfigType.String,
Default = "Welcome to {userMention}!",
Optional = true
},
{
Name = "JoinRoles",
Description = "Roles to give to new members (up to 10)",
Type = bot.ConfigType.Role,
Optional = true,
Array = true,
ArrayMaxSize = 10
},
{
Name = "LeaveMessage",
Description = "Message to be posted when a user leaves the server (`{userTag}` will be replaced by the user name)",
Type = bot.ConfigType.String,
Default = "Farewell {userTag}. :wave:",
Optional = true
},
{
Name = "BanMessage",
Description = "Message to be posted when a user is banned from the server (`{userTag}` will be replaced by the user name)",
Type = bot.ConfigType.String,
Default = "{userTag} has been banned. :hammer:",
Optional = true
},
{
Name = "UnbanMessage",
Description = "Message to be posted when a user is unbanned from the server (`{userTag}` will be replaced by the user name)",
Type = bot.ConfigType.String,
Default = "{userTag} has been unbanned.",
Optional = true
}
}
end
function Module:OnMemberJoin(member)
local guild = member.guild
local config = self:GetConfig(guild)
if (config.WelcomeChannel) then
local channel = client:getChannel(config.WelcomeChannel)
local message = config.JoinMessage
if (channel and message) then
message = self:CommonMessageGsub(message, member.user)
message = message:gsub("{user}", member.user.mentionString)
channel:send(message)
end
if (config.JoinRoles) then
for _, roleId in ipairs(config.JoinRoles) do
local role = guild:getRole(roleId)
if (role) then
local success, err = member:addRole(role, true)
if (not success) then
self:LogError(guild, "Failed to add role %s to member %s: %s", role.name, member.user.tag or "<invalid>", err)
end
else
self:LogError(guild, "Invalid role %s", roleId)
end
end
end
end
end
function Module:OnMemberLeave(member)
local config = self:GetConfig(member.guild)
if (config.WelcomeChannel) then
local channel = client:getChannel(config.WelcomeChannel)
local message = config.LeaveMessage
if (channel and message) then
message = self:CommonMessageGsub(message, member.user)
message = message:gsub("{user}", member.user.tag)
if (member.joinedAt) then
local duration = Discordia.Date() - Discordia.Date.fromISO(member.joinedAt)
message = message:gsub("{duration}", bot:FormatDuration(member.guild, duration:toSeconds(), 2))
else
message = message:gsub("{duration}", "<unavailable>")
end
channel:send(message)
end
end
end
function Module:OnUserBan(user, guild)
local config = self:GetConfig(guild)
if (config.BanChannel) then
local channel = client:getChannel(config.BanChannel)
local message = config.BanMessage
if (channel and message) then
message = self:CommonMessageGsub(message, user)
message = message:gsub("{user}", user.tag)
channel:send(message)
end
end
end
function Module:OnUserUnban(user, guild)
local config = self:GetConfig(guild)
if (config.BanChannel) then
local channel = client:getChannel(config.BanChannel)
local message = config.UnbanMessage
if (channel and message) then
message = self:CommonMessageGsub(message, user)
message = message:gsub("{user}", user.tag)
channel:send(message)
end
end
end
function Module:CommonMessageGsub(message, user)
message = message:gsub("{userTag}", user.tag)
message = message:gsub("{userMention}", user.mentionString)
return message
end