This repository was archived by the owner on Feb 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
85 lines (74 loc) · 2.16 KB
/
Copy pathbot.js
File metadata and controls
85 lines (74 loc) · 2.16 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
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
//setup help: https://www.howtogeek.com/364225/how-to-make-your-own-discord-bot/
const Discord = require('discord.js');
const fs = require('fs');
var auth = require('./auth.json');
var style = require('./style.json');
const client = new Discord.Client();
var players = [];
var speaking = [];
var botInChannel = false;
client.login(auth.token);
client.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.channel;
let oldUserChannel = oldMember.channel;
if(oldUserChannel === null && newUserChannel !== null && newUserChannel.toString() === style.channel) {
if (!botInChannel) {botjoin(newUserChannel)};
// User Joins a voice channel
if (oldMember.member.displayName !== style.botname) {
players.push(oldMember.member.displayName);
if (oldMember.speaking) {
speaking.push(true);
debug("speaking true: " + speaking[0]);
} else {
speaking.push(false);
debug("speaking false: " + speaking[0]);
}
writeOut();
}
} else if(newUserChannel === null && oldUserChannel.toString() === style.channel){
if (oldUserChannel.members.size <= 1) {botleave(oldUserChannel)};
// User leaves a voice channel
var index = players.indexOf(oldMember.member.displayName);
players.splice(index, 1);
speaking.splice(index, 1);
writeOut();
}
})
client.on('guildMemberSpeaking', (member, speaking) => {
var index = players.indexOf(member.displayName);
if (speaking) {
speaking[index] = true;
} else {
speaking[index] = false;
}
debug("speaking changed");
writeOut();
});
function botjoin(channel) {
botInChannel = true;
channel.join()
.then(connection => {
connection.play('join.mp3')});
};
function botleave(channel) {
botInChannel = false;
channel.leave();
};
function writeOut() {
try {
var text = "";
for (var i = 0; i < players.length; i++) {
text += "<font face='"+style.font+"' p2 style='color: #"+(speaking[i] ? style.coloron : style.coloroff)+";'>"+players[i]+"</p2><br>";
}
const data = fs.writeFileSync('\discord.html', text)
} catch (err) {
console.error(err)
}
}
function debug(text) {
try {
fs.writeFileSync('\debug.txt', text)
} catch (err) {
console.error(err)
}
}