2

I'm trying to write a discord bot.

Now there is one command in which another user is selected. So far, all commands work except for one, checking for the existence of a user.

That's how I do it:

if (!userToMarry) {
      return message.channel.send('Please try again with a valid user.')}

If there is no such user, then a corresponding message should be displayed.

But instead I get this message:

You provided an invalid userToMarry. Please try again. Respond with cancel to cancel the command. The command will automatically be cancelled in 30 seconds.

How can this be fixed?

The second part of the error bothers me more, because of it the first one occurs, as far as I understand, this is a built-in command from discord.js-commando.

Can it be turned off somehow?

Please try again. Respond with cancel to cancel the command. The command will automatically be cancelled in 30 seconds.

client

const client = new CommandoClient({
    unknownCommandResponse: false,
    disableEveryone: true
});

client.registry
    .registerDefaultTypes()
    .registerGroups([
        ['test', 're']
    ])
    .registerDefaultGroups()
    .registerDefaultCommands({
        help: false,
        prefix: false,
        ping: true,
        eval: true,
        unknownCommand: false,
        commandState: false
    })
    .registerCommandsIn(path.join(__dirname, 'commands'));

command

const { Command } = require('discord.js-commando');
const db = require("quick.db");


module.exports = class MarryCommand extends Command {
  constructor(client) {
    super(client, {
      name: 'marry',
      memberName: 'marry',
      group: 'test',
      description: 'Marry the mentioned user',
      guildOnly: true,
      args: [
        {
          key: 'userToMarry',
          prompt: 'Please select the member you wish to marry.',
          type: 'member'
        }
      ]
    });
  }

  run(message, { userToMarry }) {
    const exists = db.get(`${message.author.id}.user`);
    const married = db.get(`${userToMarry.id}.user`);
    if (!userToMarry) {
      return message.channel.send('Please try again with a valid user.')}
    if (exists == message.author.id) {
      return message.channel.send('You are already married!')}
    if (married == userToMarry.id) {
      return message.channel.send('This user is already married!')}
    if (userToMarry.id == message.author.id) {
      return message.channel.send('You cannot marry yourself!');
    }
    if (exists != message.author.id && married != userToMarry.id) {
    message.channel.send(`**Important announcement!**
    
    ${message.author} makes a marriage proposal ${userToMarry}

2 Answers2

0

It looks like you are running the database query before checking if userToMarry exists. You might want to change it as seen below. Additionally you may want to wrap query in a try/catch in case userToMarry exists but is invalid.

run(message, { userToMarry }) {
  const exists = db.get(`${message.author.id}.user`);
  if (!userToMarry) {
    return message.channel.send('Please try again with a valid user.')
  }
  const married = db.get(`${userToMarry.id}.user`);
}
Thenlie
  • 674
  • 8
  • 15
0

It appears you can change the error message to a custom one by adding the error property to the argument. You can also take advantage of the validate property to validate input outside of the body.

See the docs here

This is what I would do:

// ...
module.exports = class MarryCommand extends Command {
  constructor(client) {
    super(client, {
      name: 'marry',
      memberName: 'marry',
      group: 'test',
      description: 'Marry the mentioned user',
      guildOnly: true,
      args: [
        {
          key: 'userToMarry',
          prompt: 'Please select the member you wish to marry.',
          type: 'member',
          error: 'Please try again with a valid user',
          validate: (_v,msg) => !!db.get(`${msg.author.id}.user`) // Double bang converts into boolean, this checks whether user exists
        }
      ]
    });
  }

  run(message, { userToMarry }) {
    // Exists no longer needed as it has been moved up
    const married = db.get(`${userToMarry.id}.user`);
    // ....
  }
Compositr
  • 717
  • 6
  • 18
  • get this error `An error occurred while running the command: TypeError: Cannot read property 'id' of null You shouldn't ever receive an error like this.` –  Mar 28 '22 at 08:07
  • and exists I further use for checks, added to the end of the code –  Mar 28 '22 at 08:10
  • Ah then you don't need the validate thing and the rest should work – Compositr Mar 28 '22 at 09:19
  • Thanks, it partially worked, post `You provided an invalid userToMarry. Please try again.` replaced by mine. –  Mar 28 '22 at 09:54
  • But about the former, this is what remains, can it also be removed somehow? `Respond with cancel to cancel the command. The command will automatically be cancelled in 30 second.` –  Mar 28 '22 at 09:54
  • I found the `cancelled` parameter here, but is it possible to disable it somehow? https://discord.js.org/#/docs/commando/master/typedef/ArgumentResult –  Mar 28 '22 at 11:01
  • The only way I've found is to go into the `argument.js` file in `node_modules` and remove this message in the right places. Maybe this is wrong, but I don't see any other way out yet) –  Mar 28 '22 at 11:23
  • @reywwww I have no idea since I don't work with commando that much, I think your best course of action now is to either: 1) Open another SO question 2) Ask in the Discord.JS discord server – Compositr Mar 29 '22 at 06:19