-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
executable file
·46 lines (39 loc) · 1.56 KB
/
bot.js
File metadata and controls
executable file
·46 lines (39 loc) · 1.56 KB
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
const fs = require("fs");
const io = require("socket.io-client");
const Discord = require("discord.js");
const YAML = require("yaml");
const client = new Discord.Client();
const configFile = fs.readFileSync("config.yml", "utf8");
config = YAML.parse(configFile);
const socket = io(config.web_socket.address + ":" + config.web_socket.port, {transports: ["websocket", "polling"]});
socket.on("get players", data => updateCount(data));
socket.on("change", data => updateCount(data));
socket.on("online", () => {
changeName(config.discord.channels.voice_status, "Status: Online")
socket.emit("get players");
});
socket.on("offline", () => changeName(config.discord.channels.voice_status, "Status: Offline");
client.on("ready", () => {
console.log("Bot started!");
client.user.setActivity(config.discord.status.message, {type: config.discord.status.type});
socket.emit("is online");
socket.emit("get players");
});
function updateCount(data) {
let playerCount = data.length;
let players = data.join(", ");
let textChannels = config.discord.channels.text;
changeName(config.discord.channels.voice_players, `Players: ${playerCount}/${config.discord.max_players}`);
textChannels.forEach(id => changeDesc(id, `Players: ${playerCount}/${config.discord.max_players} ${players}`));
}
function changeName (id, name) {
client.channels.fetch(id)
.then(channel => channel.setName(name))
.catch(console.error);
}
function changeDesc (id, desc) {
client.channels.fetch(id)
.then(channel => channel.setTopic(desc))
.catch(console.error);
}
client.login(config.discord.token);