0

My Problem:

I'm setting up a discord bot using discord.js in repl.it. In my config.js file i have set the bot status and want to include a predetermined variable in that status. So far everything I have tried does not work.


What I've Done to Try and Fix It:

I have tried many ways to include the variable and some have allowed the program to run. However those that have allowed it to run (including this.prefix) have showed the status as undefinedhelp as supposed as the actual value of the prefix (//)help.


My Code:

var config = {
  token: "MY BOT TOKEN HERE",
  prefix: "//",
  statusMessage: "video games | (" + this.prefix + "help)", 
  dbltoken: undefined
}

module.exports = config;

Output and Errors:

The output should be a status on the bot that says video games | (//help)

What it is currently saying is video games | (undefinedhelp)


Thank you in advance for any help

obeenham
  • 3
  • 1
  • 4
  • Possibly related: [Self-references in object literals / initializers](https://stackoverflow.com/a/4616262/9902555) – Robert Dyjas Oct 15 '19 at 10:11
  • Possible duplicate of [Self-references in object literals / initializers](https://stackoverflow.com/questions/4616202/self-references-in-object-literals-initializers) – Caltrop Oct 15 '19 at 13:20

2 Answers2

4

You can't use this inside object.

you can do this

var config = {
  token: "MY BOT TOKEN HERE",
  prefix: "//",
}
config.statusMessage = "video games | (" + config.prefix + "help)";
Saeed Jalali
  • 416
  • 4
  • 16
  • Sorry that does not work. Would you like to see the error it gives? – obeenham Oct 15 '19 at 09:51
  • This is the error given with the code shown above.```/home/runner/config.js:4 statusMessage: "video games | (" + config.prefix + "help)", ^ TypeError: Cannot read property 'prefix' of undefined``` – obeenham Oct 15 '19 at 09:56
  • Just change config. to this. – Cursed Oct 15 '19 at 10:01
  • I tried that and it is said in my original question. The output is in the output section. – obeenham Oct 15 '19 at 10:09
  • first defined the config object. – Saeed Jalali Oct 15 '19 at 10:35
  • How would I do that? Sorry I'm a bit new to this – obeenham Oct 15 '19 at 12:00
  • ```var config={}; config.token = "MY BOT TOKEN HERE"; config.prefix = "//"; config.statusMessage = "video games | (" + config.prefix + "help)"; ``` – Saeed Jalali Oct 15 '19 at 12:03
  • I have done what you said but now the main index file can't extract the token from `config.js` – obeenham Oct 15 '19 at 14:59
  • Here is my login code: ```client.login(client.config.token) // Logs into Discord with the token defined in config. .then(() => { if(!client.user.bot) { console.log("[JPBTips] Don't self bot idot"); return process.exit() }; console.log(`Client logged in as ${client.user.tag}`); // Tells you when the bot has logged in! })``` – obeenham Oct 15 '19 at 15:05
  • Thank you, your solutions have helped me sort the problem. – obeenham Oct 15 '19 at 16:54
0
const token = MY BOT TOKEN HERE;

const PREFIX = '=';

bot.on('message', message=>{
    let args = message.content.substring(PREFIX.length).split(" ");

    switch(args[0]){
        case 'help':
            //CODE GOES HERE
        break;
    }
})
  • 1
    Generally, when leaving an answer, a bit of a narrative should accompany any code (i.e. explain to the OP what the issue was, and why/how your code addresses it). – theMayer Oct 17 '19 at 23:05