/[sudobot]/trunk/src/services/Welcomer.ts
ViewVC logotype

Contents of /trunk/src/services/Welcomer.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 199 - (show annotations)
Mon Jul 29 17:28:59 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 2276 byte(s)
feat: random welcome messages (#44)
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) {
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())
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 embeds: [
32 new MessageEmbed({
33 author: {
34 iconURL: member.displayAvatarURL(),
35 name: member.user.tag
36 },
37 description: content,
38 timestamp: new Date()
39 })
40 .setColor('#007bff')
41 ]
42 });
43 }
44 }
45 catch (e) {
46 console.log(e);
47 }
48 }
49 }
50
51 generateMessage() {
52 return this.messages[Math.floor(this.messages.length * Math.random())];
53 }
54 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26