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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 226 - (show annotations)
Mon Jul 29 17:29:06 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 2384 byte(s)
refactor: make all services inherit a base class
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 async start(member: GuildMember, index?: number) {
11 if (this.client.config.props[member.guild.id].welcomer.enabled) {
12 const { message, channel: channelID } = this.client.config.props[member.guild.id].welcomer;
13 const content = (message ?? this.generateMessage(index))
14 .replace(':name:', member.displayName)
15 .replace(':tag:', member.user.tag)
16 .replace(':username:', member.user.username)
17 .replace(':discrim:', member.user.discriminator)
18 .replace(':avatar:', member.displayAvatarURL())
19 .replace(':date:', `<t:${member.joinedAt?.getTime()}>`)
20 .replace(':guild:', member.guild.name)
21 .replace(':mention:', member.toString());
22
23 try {
24 const channel = (await member.guild.channels.fetch(channelID)) as TextChannel;
25
26 if (channel) {
27 await channel.send({
28 content: member.toString(),
29 embeds: [
30 new MessageEmbed({
31 author: {
32 iconURL: member.displayAvatarURL(),
33 name: member.user.tag
34 },
35 description: content,
36 timestamp: new Date()
37 })
38 .setColor('#007bff')
39 ]
40 });
41 }
42 }
43 catch (e) {
44 console.log(e);
45 }
46 }
47 }
48
49 generateMessage(index?: number) {
50 return this.messages[index ?? Math.floor(this.messages.length * Math.random())];
51 }
52 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26