-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
192 lines (173 loc) · 5.51 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
185
186
187
188
189
190
191
192
const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { Client, Collection, Intents } = require('discord.js');
const configFile = (process.argv[2]) ? process.argv[2] : './config.json';
const { token, clientId, guildIds } = require(configFile); const config = require(configFile);
const MusicPlayer = require("./discord-player.js");
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_VOICE_STATES] });
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);
}
var DATA = {
queue: [],
loop: false,
loopSong: false,
volume: 1,
current: null, // {id: "", title: ""}
guildId: null,
speech: false,
status: "Idle"
};
var players = new Map();
client.once('ready', () => {
console.log('Ready!');
client.user.setActivity("by RedTech", {
type: "PLAYING"
});
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await execute(interaction.commandName, interaction);
//DATA = await command.execute(interaction, client, DATA);
} catch (error) {
console.error(error);
return interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
function deployCommands(guilds) {
const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
}
const rest = new REST({ version: '9' }).setToken(token);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
guilds.forEach(await (async (guild, i) => {
await rest.put(
Routes.applicationGuildCommands(clientId, guild),
{ body: commands },
);
}));
console.log('Successfully reloaded application (/) commands.');
} catch(e) {
console.log(e);
}
})();
}
client.on('guildCreate', (guild) => {
if (config.guildIds.indexOf(guild.id) == -1) {
config.guildIds.push(guild.id);
fs.writeFile(configFile, JSON.stringify(config), (err) => {
if (err) console.log("[GuildCreate][WriteFile][Error]: ", err);
});
}
deployCommands([guild.id]);
});
client.on('guildDelete', (guild) => {
const i = config.guildIds.indexOf(guild.id);
if (i > -1) {
config.guildIds.splice(i, 1);
}
fs.writeFile(configFile, JSON.stringify(config), (err) => {
if (err) console.log("[GuildDelete][WriteFile][Error]: ", err);
});
});
async function execute(name, interaction) {
if (!players.has(interaction.guildId)) {
const musicPlayer = new MusicPlayer(client, configFile, DATA);
musicPlayer.on("log", text => console.log("[" + interaction.guildId + "][Music Player][Log] " + text));
musicPlayer.on("error", error => console.error("[" + interaction.guildId + "][Music Player][Error] " + error));
musicPlayer.on("state", state => console.log("[" + interaction.guildId + "][Music Player][State Update] " + state));
players.set(interaction.guildId, musicPlayer);
}
try {
const musicPlayer = players.get(interaction.guild.id);
switch(name) {
case "join":
await interaction.deferReply();
let s = musicPlayer.join(interaction);
if (!s) {interaction.editReply({ content: "Please join a voice channel first", ephemeral: true }); return;}
interaction.editReply({ content: "Joined." });
break;
case "leave":
await interaction.deferReply();
var left = musicPlayer.leave(interaction);
if (left) {
return interaction.editReply({ content: "Left the voice channel" });
}
return interaction.editReply({ content: "I'm not connected to a voice channel...", ephemeral: true });
break;
case "play":
await musicPlayer.play(interaction);
break;
case "np":
await musicPlayer.nowPlaying(interaction);
break;
case "pause":
musicPlayer.pause();
interaction.reply({ content: "Paused" });
break;
case "resume":
musicPlayer.resume();
interaction.reply({ content: "Resumed" });
break;
case "skip":
musicPlayer.skip();
interaction.reply({ content: "Skipped" });
break;
case "list":
await musicPlayer.list(interaction);
break;
case "loop":
musicPlayer.loop(interaction);
break;
case "remove":
musicPlayer.remove(interaction);
break;
case "statemessage":
musicPlayer.stateDisplay(interaction);
break;
case "shuffle":
musicPlayer.shuffle(interaction);
break;
case "clear-list":
musicPlayer.clear(interaction);
break;
case "toggle-speech":
musicPlayer.toggleSpeech(interaction);
break;
case "lyrics":
if (config.disableLyrics) {interaction.reply({ content: "This function is currently disabled :/", ephemeral: true });return;}
musicPlayer.lyrics(interaction);
break;
default:
break;
}
} catch(e) {
console.error("Error: ", e);
}
}
const tmp = config.guildIds.slice();
const guilds = client.guilds.cache.map(guild => guild.id);;
guilds.forEach((guild, i) => {
if (!config.guildIds.includes(guild)) {
config.guildIds.push(guild);
}
});
if (tmp.some(r=> config.guildIds.indexOf(r) >= 0)) {
fs.writeFile(configFile, JSON.stringify(config), (err) => {
if (err) console.log("[GuildDelete][WriteFile][Error]: ", err);
});
}
deployCommands(guildIds);
client.login(token);