-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord.service.ts
46 lines (38 loc) · 1.06 KB
/
discord.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { Client as ClientDiscord, TextChannel } from 'discord.js';
import { discordConfig } from '../config/discord';
export class DiscordService {
constructor(private readonly discord: ClientDiscord) {}
public async connect() {
this.listenEvents();
await this.discord.login(discordConfig.token);
}
private async listenEvents() {
this.discord.on('ready', this.onReadyConnect);
}
private async onReadyConnect() {
console.log('Discord ready!');
}
public async sendMessage(
channelName: string,
message: string
): Promise<void> {
const channel = this.discord.channels.cache.find(
(channel: any) => channel.toJSON().name === channelName
) as TextChannel;
if (!channel) {
throw new Error('Channel not found');
}
const messageToSend =
message.length > 2000
? {
files: [
{
name: 'notification.txt',
attachment: Buffer.from(message)
}
]
}
: message;
await channel.send(messageToSend);
}
}