1 |
import { GuildMember, MessageEmbed, TextChannel } from "discord.js"; |
2 |
import DiscordClient from "../client/Client"; |
3 |
import fs from 'fs'; |
4 |
import path from "path"; |
5 |
|
6 |
export default class Welcomer { |
7 |
messages: string[] = JSON.parse(fs.readFileSync(path.resolve(__dirname, '..', '..', 'resources', 'welcome_messages.json')).toString()); |
8 |
|
9 |
constructor(protected client: DiscordClient) { |
10 |
|
11 |
} |
12 |
|
13 |
async start(member: GuildMember, index?: number) { |
14 |
if (this.client.config.props[member.guild.id].welcomer.enabled) { |
15 |
const { message, channel: channelID } = this.client.config.props[member.guild.id].welcomer; |
16 |
const content = (message ?? this.generateMessage(index)) |
17 |
.replace(':name:', member.displayName) |
18 |
.replace(':tag:', member.user.tag) |
19 |
.replace(':username:', member.user.username) |
20 |
.replace(':discrim:', member.user.discriminator) |
21 |
.replace(':avatar:', member.displayAvatarURL()) |
22 |
.replace(':date:', `<t:${member.joinedAt?.getTime()}>`) |
23 |
.replace(':guild:', member.guild.name) |
24 |
.replace(':mention:', member.toString()); |
25 |
|
26 |
try { |
27 |
const channel = (await member.guild.channels.fetch(channelID)) as TextChannel; |
28 |
|
29 |
if (channel) { |
30 |
await channel.send({ |
31 |
content: member.toString(), |
32 |
embeds: [ |
33 |
new MessageEmbed({ |
34 |
author: { |
35 |
iconURL: member.displayAvatarURL(), |
36 |
name: member.user.tag |
37 |
}, |
38 |
description: content, |
39 |
timestamp: new Date() |
40 |
}) |
41 |
.setColor('#007bff') |
42 |
] |
43 |
}); |
44 |
} |
45 |
} |
46 |
catch (e) { |
47 |
console.log(e); |
48 |
} |
49 |
} |
50 |
} |
51 |
|
52 |
generateMessage(index?: number) { |
53 |
return this.messages[index ?? Math.floor(this.messages.length * Math.random())]; |
54 |
} |
55 |
} |