I am coding a Discord bot with Discord.JS and am fairly new to it. (started this summer)
I have a radio bot for me and my friends and I have it join the channel and read from JSON and play the music all fine.
What I want it to do, is so that I can add to the stations.json file via a command and then everything would update from there.
This is stations.json:
{
"b101":
{
"command":"b101",
"link":"https://playerservices.streamtheworld.com/api/livestream-redirect/WBEBFMAAC.aac",
"name":"Philly's B101"
},
}
That's the format for each station
And here is play.js:
module.exports = {
name: "music!play",
description: "Find a music stream and play it",
execute(msg, args, client) {
function validURL(str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
return !!pattern.test(str);
}
if (args.length === 0)
return msg.reply('supply a station please');
if (msg.member.voice.channel) {
let stationsJson = require('./stations.json');
const isStation = stationsJson.hasOwnProperty(args[0])
if (isStation) {
const station = args[0]
msg.member.voice.channel.join()
.then(connection => {
const link = stationsJson[station].link
const title = stationsJson[station].name
msg.reply(`Connected! Playing ${title}`)
connection.play(link);
});
} else {
// this is just manual link input here
}
} else {
msg.reply('You are not in a voice channel!');
}
}
}
and finally, here is add.js. The command to add stations.
const fs = require('fs')
var command = args[0]
var link = args[1]
var name = args.slice(2,20).join(" ")
fs.readFile('./commands/stations.json', function (err, data) {
if (err) throw err
var json = JSON.parse(data)
json.stations.push(
{
"${command}": {
"command": command,
"link": link,
"name": name
}
});
json = JSON.stringify(json);
fs.writeFile('./commands/stations.json', json, 'utf8', function (err) {
if (err) throw err
});
That is what I've tried so far, but I have to put it in an array and that doesn't work when I try to play from play.js.
Thanks for the help!