1

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!

3
  • 2
    In your add function when you push to the array you wrote this "${command}" with quotes, this won't work if you want your variable to be appended into the string you should replace "${command}" by `${command}` anyway this isn't required in your case because your string only includes the command name so you could just wrote the variable's name Commented Nov 10, 2020 at 15:36
  • When you are posting a question, please try to add cleaner code. Remove all unnecessary parts of the code so everyone who want to help can focus on the important part. For example, you do not need to add everything in your .json file. You can remove all the validation code etc. since they are not related with your problem. Commented Nov 10, 2020 at 15:52
  • Okay, thank you! I'm kinda new to asking these questions. I'll fix it now Commented Nov 10, 2020 at 15:52

1 Answer 1

1

I see a mistake in you add.js file.

There is no json.stations in your json file. It would be available if you had something like:

// stations.json
{
  stations: [{
    "b101": {
      "command":"b101",
      "link":"https://playerservices.streamtheworld.com/api/livestreamredirect/WBEBFMAAC.aac",
      "name":"Philly's B101"
    }
  }]
}

Now json.stations is possible and it's and array.

If you want to add something to an object you should use

json[command] = {
  `${command}`: {
    "command": command,
    "link": link,
    "name": name
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

How would I update my play.js file to play from the JSON file?
@zangw says in question 35389060: "require is synchronous and only reads the file once, following calls return the result from cache." Maybe you can try replacing your require with file system calls.
Alright, it's my lunchtime now and I will try all these fixes. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.