1

I am making an RP profile creation setup for a discord bot using javascript. I have the conversation starting in a channel and moving to private messaging with the bot. The first question gets asked and the reply from the user is stored in a database. That is working fine.

What seems to be the problem comes when I try to use another command inside a private message with the bot to move to the next step of the RP profile creation. It doesn't seem to register the command is being used. Can commands even be used in private messaging with a bot?

I used the same code as the first question that worked, changed what needed to be, but nothing that should have broken the code. It just looks to not even see the second command, which is stored in a separate command file. How would I do this?

module.exports.run = async (bot, message, args) => {
message.author.send(` SECOND QUESTION, **What is the age of your Brawler or Character?**`)
  .then((newmsg) => { //Now newmsg is the message you send to the bot
    newmsg.channel.awaitMessages(response => response.content, {
      max: 1,
      time: 300000,
      errors: ['time'],
    }).then((collected) => {
      newmsg.channel.send(`Your brawler's age is: **${collected.first().content}**

      If you are okay with this age, type !profilegender to continue the profile creation process!

      If you would like to edit your age, please type !profileage`)
        con.query(`UPDATE profile SET age = '${collected.first().content}' WHERE id = ${message.author.id}`);
        console.log("1 record updated!")
    }).catch(() => {
      newmsg.channel.send('Please submit an age for your character. To restart Profile creation, please type "!profilecreate" command in Profile Creation channel on the server.');
    });
  });
}

Thanks in advance for your time!

EDIT: This is part of the code that is the bot/client is listening for on message.

bot.on(`message`, async message => {
  if(message.author.bot) return;
  if(message.channel.type === "dm") return;

  con.query(`SELECT * FROM profile WHERE id = '${message.author.id}'`, (err, rows) => {
    if(err) throw err;

    var sql;

    if(rows.length < 1) {
      var sql = (`INSERT INTO profile (id, username) VALUES (${message.author.id}, '${message.author.tag}')`);
    } else {
      var sql = (`UPDATE profile SET username = '${message.author.tag}' WHERE id = ${message.author.id}`);
    };

    //con.query(sql, console.log);
    //if (err) throw err;
    //console.log("1 record inserted!");
  });
Zarvix
  • 119
  • 4
  • 17
  • Are you sure that inside of your `client.on("message")` there is nothing that can prevent the bot from recognizing the command (maybe something that checks for the prefix, or for a specific channel, or stuff like that...). Could you post that part of your code? Anyway, there's no problem in using commands via DM, but if your code relies only on [TextChannels](https://discord.js.org/#/docs/main/master/class/TextChannel) (instead of [DMChannels](https://discord.js.org/#/docs/main/master/class/DMChannel)) it may cause some issues (usually it's easily fixable) – Federico Grandi Jul 06 '18 at 19:20
  • That might be the issue. I didn't even consider that. I updated the OP with that part of the code. It does mention the channel type with a return after, so it doesn't seem like it awaits anything in DM on message when it comes to commands. I am unsure how to fix that though. Simply remove the line mentioning "dm" as the message type? – Zarvix Jul 06 '18 at 21:25
  • Yep, that line tells the bot to exit the function if the channel is a DM. Keep in mind that if you remove that the bot will allow every command to be executed via DM. If that's fine for you, you can just remove that. If you want only some commands to be used via DM, you may want to add an if check that returns if the channel is a DM && the command is not allowed – Federico Grandi Jul 07 '18 at 09:05
  • 1
    I specified that it needed to be in a certain channel for a couple of my other commands, so that shouldn't be an issue. The private message commands will be very few and all the other commands will be specified to certain channels in the server, so it shouldn't be much of a hassle to specify this for my needs. Thanks so much. If you want to post the gist of these comments as an answer so I can mark it as answered, that would be great. – Zarvix Jul 07 '18 at 11:17
  • Perfect! I just added an answer – Federico Grandi Jul 07 '18 at 12:17

1 Answers1

4

Answer from comments

Inside of your client.on("message") there's an if check that exits the function if the channel is a DMChannel

if(message.channel.type === "dm") return;

To avoid that, simply remove this line: in this way, the bot will execute the command regardless of the channel type. If you want to allow some commands only in certain channels, you can do that either in the client.on("message") or in the function of the command itself.

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50