-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
184 lines (159 loc) · 6.31 KB
/
index.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
const { token, database, guildId } = require('./config.json');
const { createAudioPlayer, getVoiceConnection, joinVoiceChannel, createAudioResource, VoiceConnectionStatus } = require('@discordjs/voice');
const { inlineCode } = require('@discordjs/builders');
var mysql = require('mysql');
const { connect } = require('http2');
const { scheduleJob } = require('node-schedule');
const youtubedl = require('youtube-dl-exec');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_MESSAGES] });
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
}
client.once('ready', () => {
console.log('Ready!');
});
const pool = mysql.createPool(database);
const player = createAudioPlayer();
/*
let last_played = Date.now(); // should hold when a sound was last played
const leave_after = ; // how long since a sound has been played should we stay
// TODO: move to config? and finish this
*/
var guild_config = [];
const query = pool.query('SELECT guild, announce_channel FROM guild_config', function (error, results, fields) {
results.forEach(function(v) {
guild_config.push({guild: v.guild, announce_channel: v.announce_channel});
});
});
client.on('messageCreate', async (message) => {
if(message.content == '!stop') {
const voiceConnection = getVoiceConnection(message.guild.id);
if(voiceConnection != null) {
voiceConnection.disconnect();
}
}
/*if(message.content.startsWith('https://vm.tiktok.com')) {
var link = youtubedl(message.content, {
output: '/home/oaks/tiktok/toks/%(id).%(ext)'
});
console.log(link);
}*/
const query = pool.query('SELECT command_name FROM sounds', function (error, results, fields) {
results.forEach(function (res) {
if ('!' + res.command_name == message.content) {
const bigQuery = pool.query('SELECT * FROM sounds WHERE command_name = ?', res.command_name, function (error, results, fields) {
if (results.length != 0){
const voiceConnection = joinVoiceChannel({
channelId: message.member.voice.channelId,
guildId: message.guildId,
adapterCreator: message.guild.voiceAdapterCreator,
});
voiceConnection.subscribe(player);
voiceConnection.on(VoiceConnectionStatus.Disconnected, async (oldState, newState) => {
try {
await Promise.race([
entersState(voiceConnection, VoiceConnectionStatus.Signalling, 5_000),
entersState(voiceConnection, VoiceConnectionStatus.Connecting, 5_000),
]);
// Seems to be reconnecting to a new channel - ignore disconnect
} catch (error) {
// Seems to be a real disconnect which SHOULDN'T be recovered from
voiceConnection.destroy();
}
});
const resource = createAudioResource('/srv/the-mega-site/storage/app/public/' + results[0].location);
player.play(resource);
}
});
}
});
});
});
client.on('voiceStateUpdate', async (oldState, newState) => {
if (oldState.channelId == null && newState.channelId != null && newState.member.id != client.user.id) {
// Someone joined a channel
// Do we have a sound for them?
var connection = mysql.createConnection(database);
var query = connection.query('SELECT * FROM join_sounds WHERE discord_id = ? AND checked = 1', newState.id, function (error, results, fields) {
if (results.length != 0){
const randNum = Math.floor(Math.random() * results.length);
const result = results[randNum];
const voiceConnection = joinVoiceChannel({
channelId: newState.channelId,
guildId: newState.guild.id,
adapterCreator: newState.guild.voiceAdapterCreator,
});
voiceConnection.subscribe(player);
voiceConnection.on(VoiceConnectionStatus.Disconnected, async (oldState, newState) => {
try {
await Promise.race([
entersState(voiceConnection, VoiceConnectionStatus.Signalling, 5_000),
entersState(voiceConnection, VoiceConnectionStatus.Connecting, 5_000),
]);
// Seems to be reconnecting to a new channel - ignore disconnect
} catch (error) {
// Seems to be a real disconnect which SHOULDN'T be recovered from
try{
voiceConnection.destroy();
} catch(err) {
}
}
});
const resource = createAudioResource('/srv/the-mega-site/storage/app/public/' + result.location);
player.play(resource);
}
});
}
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
return interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
scheduleJob('*/5 * * * *', function() {
const query = pool.query('SELECT id, request_name, filled, announced, discord_id FROM requests', function (error, results, fields) {
const channel = client.channels.cache.get(guild_config[0].announce_channel);
results.forEach(function(v) {
if(v.filled == 1 && v.announced == 0) {
// announce
channel.send(`<@${v.discord_id}> ${inlineCode(v.request_name)} is on the server.`);
const insertQuery = pool.query('UPDATE requests SET announced = 1 WHERE id = ?', v.id);
}
});
});
//const voiceConnection = getVoiceConnection(message.guild.id);
//guild_config.forEach(function(v) {
//client.
//}
//guild_config.forEach(function(v) {
//const channel = client.channels.cache.get(v.announce_channel); // god i'm an idiot this doesn't actually work need a guild column in requests some day
//channel.send('content');
//});
});
scheduleJob('*/1 * * * *', function() {
// if the bot is the only member, it should leave
const voiceConnection = getVoiceConnection(guild_config[0].guild);
if(!voiceConnection) return;
const channel = client.channels.cache.get(voiceConnection.joinConfig.channelId);
if(!channel) return;
if(channel.members.size === 1) {
voiceConnection.disconnect();
}
});
process.on('beforeExit', () => {
pool.end(function (err) {
console.log(err);
});
});
client.login(token);