Skip to content

Commit 00ce0fe

Browse files
committed
working now on multiple servers
1 parent d847519 commit 00ce0fe

10 files changed

+113
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
test-config.json
2+
public-test-config.json
23
node_modules/

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,29 @@ This is a basic music bot that also has the ability to listen to voice commands.
1919
- Head over to the [genius api site](https://genius.com/api-clients) and create a client. Generate an access token and copy it. Put it in the config.json
2020
- You're ready! :D
2121

22+
## Updating
23+
24+
*This section requires you to have bash installed. It comes bundled with git if you're on Windows, and is preinstalled on most Linux distributions.*
25+
26+
#### For Windows:
27+
28+
- Double-click the update.sh file. If the installation of git went right it should start for itself. If it does not start "git bash" and navigate into the folder. Then run `bash update.sh`
29+
30+
#### For Linux:
31+
32+
- Navigate into the folder through your desired terminal
33+
- enter `bash update.sh`
34+
2235
## Running
2336

2437
- Execute `node index.js`
2538
- Have fun :)
2639

40+
#### For Linux/Git Bash users:
41+
42+
- Execute the start.sh file. If you want to see the logs, execute the logs.sh file
43+
- This will create a process using `nohup` that stays online even if you close the terminal/shh session. To terminate the process run `bash stop.sh`
44+
2745
## Usage
2846

2947
- All commands are listen when you enter a "/"

command-deployer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { REST } = require('@discordjs/rest');
22
const { Routes } = require('discord-api-types/v9');
3-
const { token, clientId, guildId } = require('./config.json');
3+
const { token, clientId, guildIds } = require('./config.json');
44
const fs = require('fs');
55

66
const commands = [];

config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"token": "your-token-goes-here",
33
"witAi": "your-wit.ai-client-token",
44
"clientId": "your-bots-application-id",
5-
"guildId": "you-server-id",
5+
"guildIds": ["you-server-id"],
66
"ytApiKey": "your-youtube-key",
77
"geniusToken": "your-genius-api-access-token"
88
}

index.js

+71-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const fs = require('fs');
2+
const { REST } = require('@discordjs/rest');
3+
const { Routes } = require('discord-api-types/v9');
24
const { Client, Collection, Intents } = require('discord.js');
35
const configFile = (process.argv[2]) ? process.argv[2] : './config.json';
4-
const { token } = require(configFile);
6+
const { token, clientId, guildIds } = require(configFile); const config = require(configFile);
57
const MusicPlayer = require("./discord-player.js");
68

79
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_VOICE_STATES] });
@@ -25,10 +27,7 @@ var DATA = {
2527
status: "Idle"
2628
};
2729

28-
const musicPlayer = new MusicPlayer(client, configFile, DATA);
29-
musicPlayer.events.on("log", text => console.log("[Music Player][Log] " + text));
30-
musicPlayer.events.on("error", error => console.error("[Music Player][Error] " + error));
31-
musicPlayer.events.on("state", state => console.log("[Music Player][State Update] " + state));
30+
var players = new Map();
3231

3332
client.once('ready', () => {
3433
console.log('Ready!');
@@ -53,10 +52,61 @@ client.on('interactionCreate', async interaction => {
5352
}
5453
});
5554

56-
const { joinVoiceChannel, getVoiceConnection } = require('@discordjs/voice');
55+
function deployCommands(guilds) {
56+
const commands = [];
57+
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
58+
59+
for (const file of commandFiles) {
60+
const command = require(`./commands/${file}`);
61+
commands.push(command.data.toJSON());
62+
}
63+
64+
const rest = new REST({ version: '9' }).setToken(token);
65+
(async () => {
66+
try {
67+
console.log('Started refreshing application (/) commands.');
68+
guilds.forEach(await (async (guild, i) => {
69+
await rest.put(
70+
Routes.applicationGuildCommands(clientId, guild),
71+
{ body: commands },
72+
);
73+
}));
74+
console.log('Successfully reloaded application (/) commands.');
75+
} catch(e) {
76+
console.log(e);
77+
}
78+
})();
79+
}
80+
81+
client.on('guildCreate', (guild) => {
82+
if (config.guildIds.indexOf(guild.id) == -1) {
83+
config.guildIds.push(guild.id);
84+
fs.writeFile(configFile, JSON.stringify(config), (err) => {
85+
if (err) console.log("[GuildCreate][WriteFile][Error]: ", err);
86+
});
87+
}
88+
deployCommands([guild.id]);
89+
});
90+
client.on('guildDelete', (guild) => {
91+
const i = config.guildIds.indexOf(guild.id);
92+
if (i > -1) {
93+
config.guildIds.splice(i, 1);
94+
}
95+
fs.writeFile(configFile, JSON.stringify(config), (err) => {
96+
if (err) console.log("[GuildDelete][WriteFile][Error]: ", err);
97+
});
98+
});
5799

58100
async function execute(name, interaction) {
101+
if (!players.has(interaction.guildId)) {
102+
const musicPlayer = new MusicPlayer(client, configFile, DATA);
103+
musicPlayer.events.on("log", text => console.log("[" + interaction.guildId + "][Music Player][Log] " + text));
104+
musicPlayer.events.on("error", error => console.error("[" + interaction.guildId + "][Music Player][Error] " + error));
105+
musicPlayer.events.on("state", state => console.log("[" + interaction.guildId + "][Music Player][State Update] " + state));
106+
players.set(interaction.guildId, musicPlayer);
107+
}
59108
try {
109+
const musicPlayer = players.get(interaction.guild.id);
60110
switch(name) {
61111
case "join":
62112
let s = musicPlayer.join(interaction);
@@ -124,4 +174,19 @@ async function execute(name, interaction) {
124174
}
125175
}
126176

177+
const tmp = config.guildIds.slice();
178+
const guilds = client.guilds.cache.map(guild => guild.id);;
179+
guilds.forEach((guild, i) => {
180+
if (!config.guildIds.includes(guild)) {
181+
config.guildIds.push(guild);
182+
}
183+
});
184+
if (tmp.some(r=> config.guildIds.indexOf(r) >= 0)) {
185+
fs.writeFile(configFile, JSON.stringify(config), (err) => {
186+
if (err) console.log("[GuildDelete][WriteFile][Error]: ", err);
187+
});
188+
}
189+
190+
deployCommands(guildIds);
191+
127192
client.login(token);

logs.log

Whitespace-only changes.

logs.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tail -f logs.log

start.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
nohup node index.js > logs.log 2>&1 &
2+
echo $! > save_pid.txt

stop.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
if [ ! -f save_pid.txt ];
2+
then
3+
echo "No process running or file not found!"
4+
exit 1
5+
fi
6+
7+
while IFS= read -r line; do
8+
kill $line
9+
done < save_pid.txt
10+
11+
rm save_pid.txt

update.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
git checkout .
2+
cp config.json ./update-backup-config.json
3+
branch=$(git branch --show-current)
4+
git pull origin $branch
5+
npm install
6+
cp update-backup-config.json ./config.json
7+
node command-deployer.js

0 commit comments

Comments
 (0)