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