1 |
import { roleMention } from '@discordjs/builders'; |
2 |
import { GuildBan, GuildMember, Message, MessageEmbed, TextChannel, User } from 'discord.js'; |
3 |
import DiscordClient from '../client/Client'; |
4 |
import { timeProcess, timeSince } from '../utils/util'; |
5 |
|
6 |
class Logger { |
7 |
client: DiscordClient; |
8 |
|
9 |
constructor(client: DiscordClient) { |
10 |
this.client = client; |
11 |
} |
12 |
|
13 |
channel(callback: (channel: TextChannel) => any, msg: Message | GuildBan) { |
14 |
let channelID = this.client.config.props[msg.guild!.id].logging_channel; |
15 |
let channel = msg.guild!.channels.cache.find(c => c.id === channelID) as TextChannel; |
16 |
|
17 |
if (channel) { |
18 |
return callback(channel); |
19 |
} |
20 |
} |
21 |
|
22 |
channelJoinLeft(callback: (channel: TextChannel) => any, msg: Message | GuildBan) { |
23 |
let channelID = this.client.config.props[msg.guild!.id].logging_channel_join_leave; |
24 |
let channel = msg.guild!.channels.cache.find(c => c.id === channelID) as TextChannel; |
25 |
|
26 |
if (channel) { |
27 |
return callback(channel); |
28 |
} |
29 |
} |
30 |
|
31 |
logEdit(oldMsg: Message, newMsg: Message) { |
32 |
this.channel(async (channel) => { |
33 |
await channel.send({ |
34 |
embeds: [ |
35 |
new MessageEmbed() |
36 |
.setColor('#007bff') |
37 |
.setTitle('Message Edited in #' + (newMsg.channel as TextChannel).name + " (" + newMsg.channel.id + ")") |
38 |
.setDescription('**-+-+Before**\n' + oldMsg.content + '\n\n**-+-+After**\n' + newMsg.content) |
39 |
.addField('ID', newMsg.id) |
40 |
.setAuthor({ |
41 |
name: newMsg.author.tag, |
42 |
iconURL: newMsg.author.displayAvatarURL(), |
43 |
}) |
44 |
.setFooter({ |
45 |
text: "Edited", |
46 |
}) |
47 |
.setTimestamp() |
48 |
] |
49 |
}); |
50 |
}, newMsg); |
51 |
} |
52 |
|
53 |
logDelete(msg: Message) { |
54 |
this.channel(async (channel) => { |
55 |
const embed = new MessageEmbed() |
56 |
.setColor('#f14a60') |
57 |
.setTitle('Message Deleted in #' + (msg.channel as TextChannel).name + " (" + msg.channel.id + ")") |
58 |
.setDescription(msg.content) |
59 |
.setAuthor({ |
60 |
name: msg.author.tag, |
61 |
iconURL: msg.author.displayAvatarURL(), |
62 |
}) |
63 |
.addField('ID', msg.id) |
64 |
.setFooter({ |
65 |
text: "Deleted", |
66 |
}) |
67 |
.setTimestamp(); |
68 |
|
69 |
if (msg.attachments.size > 0) { |
70 |
let str = ''; |
71 |
|
72 |
msg.attachments.forEach(a => { |
73 |
str += `**${a.name}** ${a.url}\n`; |
74 |
}); |
75 |
|
76 |
embed.addField('Attachments', str); |
77 |
} |
78 |
|
79 |
await channel.send({ |
80 |
embeds: [ |
81 |
embed |
82 |
] |
83 |
}); |
84 |
}, msg); |
85 |
} |
86 |
|
87 |
logBanned(ban: GuildBan) { |
88 |
this.channel(async (channel) => { |
89 |
let r = '*No reason provided*'; |
90 |
|
91 |
if (ban.reason) { |
92 |
r = ban.reason; |
93 |
} |
94 |
|
95 |
await channel.send({ |
96 |
embeds: [ |
97 |
new MessageEmbed() |
98 |
.setColor('#f14a60') |
99 |
.setTitle("A user was banned") |
100 |
.setAuthor({ |
101 |
name: ban.user.tag, |
102 |
iconURL: ban.user.displayAvatarURL(), |
103 |
}) |
104 |
.addField('Reason', r) |
105 |
.addField('User ID', ban.user.id) |
106 |
.setFooter({ |
107 |
text: "Banned", |
108 |
}) |
109 |
.setTimestamp() |
110 |
] |
111 |
}); |
112 |
}, ban); |
113 |
} |
114 |
|
115 |
logUnbanned(ban: GuildBan) { |
116 |
this.channel(async (channel) => { |
117 |
await channel.send({ |
118 |
embeds: [ |
119 |
new MessageEmbed() |
120 |
.setColor('#f14a60') |
121 |
.setTitle("A user was unbanned") |
122 |
.setAuthor({ |
123 |
name: ban.user.tag, |
124 |
iconURL: ban.user.displayAvatarURL(), |
125 |
}) |
126 |
.addField('User ID', ban.user.id) |
127 |
.setFooter({ |
128 |
text: "Unbanned", |
129 |
}) |
130 |
.setTimestamp() |
131 |
] |
132 |
}); |
133 |
}, ban); |
134 |
} |
135 |
|
136 |
logJoined(member: GuildMember) { |
137 |
this.channelJoinLeft(async (channel) => { |
138 |
await channel.send({ |
139 |
embeds: [ |
140 |
new MessageEmbed() |
141 |
.setColor('#007bff') |
142 |
.setTitle("New member joined") |
143 |
.setAuthor({ |
144 |
name: member.user.tag, |
145 |
iconURL: member.user.displayAvatarURL(), |
146 |
}) |
147 |
.setDescription(`<@${member.user.id}> just joined the server!`) |
148 |
.addField('Account Created', `${member.user.createdAt.toLocaleString()} (${timeSince(member.user.createdAt.getTime())})`) |
149 |
.addField('New Account?', (new Date().getTime() - member.user.createdAt.getTime()) <= 3 * 24 * 60 * 60 * 1000 ? ":warning: Yes :warning:" : "No") |
150 |
.addField('Bot?', member.user.bot === true ? 'Yes' : 'No') |
151 |
.addField('User ID', member.user.id) |
152 |
.setFooter({ |
153 |
text: "Joined", |
154 |
}) |
155 |
.setTimestamp() |
156 |
] |
157 |
}); |
158 |
}, member); |
159 |
} |
160 |
|
161 |
logLeft(member: GuildMember) { |
162 |
this.channelJoinLeft(async (channel) => { |
163 |
const roles = await member.roles.cache.filter(role => role.id !== member.guild.id).reduce((acc, val) => ` ${acc} ${roleMention(val.id)}`, ''); |
164 |
|
165 |
await channel.send({ |
166 |
embeds: [ |
167 |
new MessageEmbed() |
168 |
.setColor('#f14a60') |
169 |
.setTitle("Member left") |
170 |
.setAuthor({ |
171 |
name: member.user.tag, |
172 |
iconURL: member.user.displayAvatarURL(), |
173 |
}) |
174 |
.setDescription(`**Roles**\n${roles}`) |
175 |
.addField('Joined at', `${member.joinedAt!.toLocaleString()} (${timeSince(member.joinedAt!.getTime())})`) |
176 |
.addField('User ID', member.user.id) |
177 |
.addField('Bot?', member.user.bot === true ? 'Yes' : 'No') |
178 |
.setFooter({ |
179 |
text: "Left", |
180 |
}) |
181 |
.setTimestamp() |
182 |
] |
183 |
}); |
184 |
}, member); |
185 |
} |
186 |
|
187 |
logBeaned(member: GuildMember, r: string, d: User) { |
188 |
this.channel(async (channel) => { |
189 |
await channel.send({ |
190 |
embeds: [ |
191 |
new MessageEmbed() |
192 |
.setColor('#007bff') |
193 |
.setTitle("Member beaned") |
194 |
.setAuthor({ |
195 |
name: member.user.tag, |
196 |
iconURL: member.user.displayAvatarURL(), |
197 |
}) |
198 |
.addField('Reason', r) |
199 |
.addField('Beaned by', d.tag) |
200 |
.addField('User ID', member.user.id) |
201 |
.setFooter({ |
202 |
text: "Beaned", |
203 |
}) |
204 |
.setTimestamp() |
205 |
] |
206 |
}); |
207 |
}, member); |
208 |
} |
209 |
|
210 |
logMute(member: GuildMember, reason: string, timeMs: number | null | undefined, d: User) { |
211 |
this.channel(async (channel) => { |
212 |
await channel.send({ |
213 |
embeds: [ |
214 |
new MessageEmbed() |
215 |
.setColor('#f14a60') |
216 |
.setTitle("Member muted") |
217 |
.setAuthor({ |
218 |
name: member.user.tag, |
219 |
iconURL: member.user.displayAvatarURL(), |
220 |
}) |
221 |
.addField('Reason', reason) |
222 |
.addField('Muted by', d.tag) |
223 |
.addField('Duration Until', typeof timeMs === 'number' ? `${new Date((timeMs / 1000) + Date.now()).toLocaleString()} (${timeProcess(timeMs / 1000)})` : "*No duration set*") |
224 |
.addField('User ID', member.user.id) |
225 |
.setFooter({ |
226 |
text: "Muted", |
227 |
}) |
228 |
.setTimestamp() |
229 |
] |
230 |
}); |
231 |
}, member); |
232 |
} |
233 |
|
234 |
logUnmute(member: GuildMember, d: User) { |
235 |
this.channel(async (channel) => { |
236 |
await channel.send({ |
237 |
embeds: [ |
238 |
new MessageEmbed() |
239 |
.setColor('#007bff') |
240 |
.setTitle("Member unmuted") |
241 |
.setAuthor({ |
242 |
name: member.user.tag, |
243 |
iconURL: member.user.displayAvatarURL(), |
244 |
}) |
245 |
.addField('Unmuted by', d.tag) |
246 |
.addField('User ID', member.user.id) |
247 |
.setFooter({ |
248 |
text: "Unmuted", |
249 |
}) |
250 |
.setTimestamp() |
251 |
] |
252 |
}); |
253 |
}, member); |
254 |
} |
255 |
|
256 |
logWarn(msg: Message, member: GuildMember | User, d: User, reason: string, id: number | string) { |
257 |
if ((member as GuildMember).user) |
258 |
member = (member as GuildMember).user; |
259 |
|
260 |
this.channel(async (channel) => { |
261 |
await channel.send({ |
262 |
embeds: [ |
263 |
new MessageEmbed() |
264 |
.setColor('GOLD') |
265 |
.setTitle("Member warned") |
266 |
.setAuthor({ |
267 |
name: (member as User).tag, |
268 |
iconURL: member.displayAvatarURL(), |
269 |
}) |
270 |
.addField('Reason', reason) |
271 |
.addField('Warned by', d.tag) |
272 |
.addField('User ID', member.id) |
273 |
.addField('Warning ID', id + '') |
274 |
.setFooter({ |
275 |
text: "Warned", |
276 |
}) |
277 |
.setTimestamp() |
278 |
] |
279 |
}); |
280 |
}, msg); |
281 |
} |
282 |
|
283 |
logWarndel(msg: Message, member: GuildMember, warn: any, d: User) { |
284 |
this.channel(async (channel) => { |
285 |
await channel.send({ |
286 |
embeds: [ |
287 |
new MessageEmbed() |
288 |
.setColor('GOLD') |
289 |
.setTitle("Warning deleted") |
290 |
.setAuthor({ |
291 |
name: member.user.tag, |
292 |
iconURL: member.user.displayAvatarURL(), |
293 |
}) |
294 |
.addField('Warned by', d.tag + '') |
295 |
.addField('Warning ID', warn.id + '') |
296 |
.addField('User ID', member.user.id) |
297 |
.setFooter({ |
298 |
text: "Warning Deleted", |
299 |
}) |
300 |
.setTimestamp() |
301 |
] |
302 |
}); |
303 |
}, msg); |
304 |
} |
305 |
} |
306 |
|
307 |
export default Logger; |