1 |
import { GuildMember, MessageEmbed, TextChannel } from "discord.js"; |
2 |
import fs from 'fs'; |
3 |
import path from "path"; |
4 |
import Service from "../utils/structures/Service"; |
5 |
|
6 |
export default class Welcomer extends Service { |
7 |
messages: string[] = JSON.parse(fs.readFileSync(path.resolve(__dirname, '..', '..', 'resources', 'welcome_messages.json')).toString()); |
8 |
|
9 |
generateEmbed(member: GuildMember, index?: number) { |
10 |
const { message, randomize } = this.client.config.props[member.guild.id].welcomer; |
11 |
let content: string = message ?? ''; |
12 |
|
13 |
if (randomize) { |
14 |
content = this.generateMessage(index) + (message ? "\n" + content : ''); |
15 |
} |
16 |
|
17 |
if (content.trim() === '') { |
18 |
return false; |
19 |
} |
20 |
|
21 |
content = content |
22 |
.replace(/:name:/g, member.displayName) |
23 |
.replace(/:tag:/g, member.user.tag) |
24 |
.replace(/:username:/g, member.user.username) |
25 |
.replace(/:discrim:/g, member.user.discriminator) |
26 |
.replace(/:avatar:/g, member.displayAvatarURL()) |
27 |
.replace(/:date:/g, `<t:${member.joinedAt?.getTime()}>`) |
28 |
.replace(/:guild:/g, member.guild.name) |
29 |
.replace(/:mention:/g, member.toString()); |
30 |
|
31 |
return { |
32 |
content: member.toString(), |
33 |
embeds: [ |
34 |
new MessageEmbed({ |
35 |
author: { |
36 |
iconURL: member.displayAvatarURL(), |
37 |
name: member.user.tag |
38 |
}, |
39 |
description: content, |
40 |
footer: { |
41 |
text: 'Welcome' |
42 |
} |
43 |
}) |
44 |
.setColor('#007bff') |
45 |
.setTimestamp() |
46 |
] |
47 |
}; |
48 |
} |
49 |
|
50 |
async start(member: GuildMember, index?: number) { |
51 |
if (this.client.config.props[member.guild.id].welcomer.enabled) { |
52 |
const { channel: channelID } = this.client.config.props[member.guild.id].welcomer; |
53 |
|
54 |
try { |
55 |
const channel = (await member.guild.channels.fetch(channelID)) as TextChannel; |
56 |
const options = this.generateEmbed(member, index); |
57 |
|
58 |
if (!options) { |
59 |
return; |
60 |
} |
61 |
|
62 |
if (channel) { |
63 |
await channel.send(options); |
64 |
} |
65 |
} |
66 |
catch (e) { |
67 |
console.log(e); |
68 |
} |
69 |
} |
70 |
} |
71 |
|
72 |
generateMessage(index?: number) { |
73 |
return this.messages[index ?? Math.floor(this.messages.length * Math.random())]; |
74 |
} |
75 |
} |