-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVoiceChannelHelper.ts
80 lines (57 loc) · 3.17 KB
/
VoiceChannelHelper.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { Client, PermissionOverwrites, VoiceChannel, TextChannel, VoiceState } from 'discord.js';
import * as DB from '../DB';
import { Module } from '../Structures/Classes';
export class VoiceChannelHelper extends Module {
config: VoiceChannelHelperConfig;
constructor(client: Client, config: VoiceChannelHelperConfig) {
super(client, "Voice Channel Helper");
this.config = config;
this.handlers.push({ event: 'voiceStateUpdate', handler: { func: this.handleVoiceStateUpdate.bind(this) }});
this.handlers.push({ event: 'ready', handler: { func: this.handleReady.bind(this), once: true }});
}
private async purgeAll() {
const result = await DB.query('SELECT text_id FROM channels WHERE set_to_purge = 1');
for (const channel of result.rows) {
await this.purge(this.client.channels.cache.get(channel.text_id) as TextChannel);
}
}
private async purge(channel: TextChannel) {
while (true) {
const messages = await channel.messages.fetch({ limit: 100 });
if (messages.size > 0) {
await channel.bulkDelete(messages);
} else {
await DB.query('UPDATE channels SET set_to_purge = 0 WHERE text_id = $1', [channel.id]);
break;
}
}
}
handleReady() {
setInterval(this.purgeAll.bind(this), this.config.purgeInterval);
}
async handleVoiceStateUpdate(oldState: VoiceState, newState: VoiceState) {
if (oldState.channelID === newState.channelID) return;
if (!newState.member || !oldState.member) return;
const channels = (await DB.query('SELECT voice_id, text_id FROM channels')).rows;
if (oldState.channelID && channels.find((v: DB.Channels) => v.voice_id == oldState.channelID)) {
const voiceChannel = this.client.channels.cache.get(oldState.channelID) as VoiceChannel;
const textChannel = this.client.channels.cache.get(channels.find((v: DB.Channels) => v.voice_id == oldState.channelID).text_id) as TextChannel;
const permissions = textChannel.permissionOverwrites.find((v: PermissionOverwrites) => v.id === oldState.member!.id);
if (voiceChannel.members.size < 1) {
await DB.query('UPDATE channels SET set_to_purge = 1 WHERE voice_id = $1', [oldState.channelID]);
}
if (permissions) permissions.delete();
if (this.config.emitLog) textChannel.send(`${oldState.member} has left the voice channel.`);
}
if (channels.find((v: DB.Channels) => v.voice_id == newState.channelID)) {
const textChannel = this.client.channels.cache.get(channels.find((v: DB.Channels) => v.voice_id === newState.channelID).text_id) as TextChannel;
await DB.query('UPDATE channels SET set_to_purge = 0 WHERE voice_id = $1', [newState.channelID!]);
textChannel.updateOverwrite(newState.member, { "VIEW_CHANNEL": true });
if (this.config.emitLog) textChannel.send(`${newState.member} has joined the voice channel.`);
}
}
}
export interface VoiceChannelHelperConfig {
emitLog: string;
purgeInterval: number;
}