forked from RichardMcSorley/korean-dictionary-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord-server.js
177 lines (159 loc) · 5.15 KB
/
discord-server.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
const Discord = require("discord.js");
const db = require("./firebase/index");
const ytdl = require("ytdl-core");
const webhooks = require("./socket.io");
const loadFiles = require("./utils/loader");
const socketIO = require("socket.io-client")(
"http://localhost:" + process.env.PORT
); // connect as a client
const bot = new Discord.Client({
disabledEvents: ["TYPING_START"]
});
bot.lastKLPmessage = null;
bot.commands = new Discord.Collection();
bot.socketIOEVENTS = new Discord.Collection();
bot.selfEVENTS = new Discord.Collection();
bot.login(process.env.KOREAN_DICT_BOT_DISCORD_TOKEN);
const broadcastState = {
broadcast: bot.createVoiceBroadcast(),
paused: false,
currentAudio: null,
currentInfo: null,
time: null
};
broadcastState.broadcast.on("subscribe", dispatcher => {
dispatcher.on("error", e => console.log("dispatcher error", e));
dispatcher.on("debug", info => console.info("dispatcher debug", info));
console.log("New broadcast subscriber!");
});
broadcastState.broadcast.on("unsubscribe", dispatcher => {
console.log("Channel unsubscribed from broadcast :(");
});
broadcastState.broadcast.on("end", () => bot.playNextSong());
broadcastState.broadcast.on("error", () => {
broadcastState.broadcast.destroy();
broadcastState.broadcast = bot.createVoiceBroadcast();
prepareKLPChannel();
});
// use fire for admin purposes, in discord use !admin eval bot.fire('restart') to restart the server
bot.fire = action => {
const { spawn } = require("child_process");
switch (action) {
case "restart":
const subprocess = spawn(process.argv[0], process.argv.slice(1), {
// spawn new process
detached: true,
stdio: ["ignore"]
});
subprocess.unref();
bot.fire("shutdown"); // close this process
return "restarting";
case "shutdown":
process.exit();
return "shutdown";
case "skip": // run end on broadcast should playNextSong()
broadcastState.broadcast.end();
return "skipping";
case "pause": // pause broadcast
broadcastState.broadcast.pause();
broadcastState.paused = true;
return "pausing";
case "resume": // resume broadcast
broadcastState.broadcast.resume();
broadcastState.paused = false;
return "resuming";
default:
return "please provide an action";
}
};
bot.run = bot.fire; // sometimes I forget which one ;)
bot.prepareKLPChannel = () => {
bot.playNextSong();
const channel = bot.channels.get(process.env.INITIAL_VOICE_CHANNEL);
bot.connectToBroadcast(channel);
};
bot.connectToBroadcast = channel => {
if (!channel) return console.error("The channel does not exist!");
// join channel
channel
.join()
.then(connection => {
connection.playBroadcast(broadcastState.broadcast);
connection.on("error", err => {
console.log("error with voice channel");
connection.disconnect();
bot.connectToBroadcast(channel);
});
})
.catch(err => console.log("could not join audio channel"));
};
bot.playNextSong = async () => {
const audio = await bot.getNextAudio();
if (audio === null) {
bot.playNextSong(); // try again
} else {
bot.playAudioToBroadcast(audio);
}
};
bot.getNextAudio = async () => {
let queue = await db.getPlaylistFromDB();
if (queue === null) {
return null;
}
// add currentAudio to end of queue
const currentAudio = queue.shift(); // remove from list
queue.push(currentAudio); // add currnetAudio to end of queue
await webhooks.updatePlaylist(queue); // update list
return currentAudio;
};
bot.playAudioToBroadcast = audio => {
const stream = ytdl(audio, { filter: "audioonly" });
broadcastState.broadcast.playStream(stream);
};
// pause if no one is listening
setInterval(() => {
bot.pauseBroadcast();
}, 3000);
bot.pauseBroadcast = () => {
let totalMembers = -1; // subtract ourself
const connections = bot.voiceConnections.array();
connections.forEach(({ channel }) => {
let { members = null } = channel;
if (members) {
totalMembers = members.array().length;
}
});
if (totalMembers === 0 && !broadcastState.paused) {
bot.fire("pause");
console.log("Pausing broadcast", total, "users connected");
} else if (totalMembers > 0 && broadcastState.paused) {
bot.fire("resume");
console.log("Resuming broadcast", total, "users connected");
}
};
// Load needed libraries and attach event listeners
loadFiles({ path: "./commands/" }, ({ library }) => {
const possibleCommands = Object.keys(library.prefix);
// add all prefixes of each command
possibleCommands.forEach(prefix => {
bot.commands.set(prefix, {
...library,
name: prefix,
...library.prefix[prefix]
});
});
});
socketIO.on("connect", () => {
loadFiles({ path: "./events/discord.socket.io/" }, ({ library }) => {
socketIO.on(library.name, message => library.handle({ message, bot, db }));
});
});
loadFiles({ path: "./events/discord.bot/" }, ({ library }) => {
bot.selfEVENTS.set(library.name, library);
bot.on(library.name, message => library.handle({ message, bot, db }));
});
loadFiles({ path: "./events/firebase/" }, ({ library }) => {
if(library.needsBot){
library.handle({ bot, db })
}
});