I need help reassigning a variable in a bot menu

Viewed 507

I am trying to build a setup menu for my bot that the server owner can use to configure the bot. The menu is triggered when the owner types =setup. I want the bot to reply with several embed messages asking the user questions in order to correctly configure the bot.

This is my first Discord.js project so I am unaware of the syntax but trying to learn. I have a constant variable called prefix assigned to = when the bot is implemented into the server.

The first prompt on the bot menu asks the user to change the prefix to anything they want. I need help understanding how to reassign my original constant variable to the new prefix they are requesting.

var PREFIX = '=';

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

    switch(args[0]){
        case 'setup':
            const embed = new Discord.RichEmbed()
            .setTitle('Step 1 of 1')
            .setDescription('Set your Prefix')
            .setColor(0xF1C40F)
            message.channel.sendEmbed(embed);
            //I want the user to now enter their own PREFIX and have the
            //bot save their responce as the new PREFIX
        break;
    }
})

What I want to happen is when the user types their desired prefix, the bot will reassign prefix in the code, and delete the bots question and the users response and begin to prompt them with the next question.

3 Answers

depending if your bot is going to be in multiple servers with different prefixes:

  • If so then you need a database to save the prefix for each server and then get it when a user sends a message from that server
  • If not I would use a json file to store the prefix, then have node edit the file when it needs to change

Or look at https://discordjs.guide/keyv/ there is a great tutorial there to do what you want

You can't prompt the user to respond (from what I know), you'll need to wait for the user to write another message and analyze it.

A message is linked to a user, so when a user initiates the command to change the prefix, you want to make sure that the same user changed the prefix.

This untested but should be close to the solution you're looking for.


let prefix = '=';
let expectingResponseFrom = null;

bot.on('message', message=>{

    // same user sent a response
    if(expectingResponseFrom !== null && expectingResponseFrom === message.user.id){
      expectingRepsonseFrom = null;
      prefix = message.content.trim();
      return;
    }

    const regex = new RegExp(`^${prefix}([^\s]+)`, 'g');
    cosnt command = regex.exec(message.content)[1] || '';

    switch(command){
        case 'setup':
            const embed = new Discord.RichEmbed()
            .setTitle('Step 1 of 1')
            .setDescription('Set your Prefix')
            .setColor(0xF1C40F)
            message.channel.sendEmbed(embed);
            // memorize user who initiated a prefix change
            expectingResponseFrom = message.user.id;
        break;
    }
})

The regex allows for a better (my opinion) way of getting the command

console.log(
  (/^=([^\s]+)/g).exec("=hello should not get this"),
  (/^=([^\s]+)/g).exec("="),
  (/^=([^\s]+)/g).exec("= should not get this")
)

You should definitely use a database to store prefixes and other server settings, it's more efficient and stable than using a JSON file for example.

I suggest you use mongoose and/or just MongoDB if you're unsure as to what to use.

Related