disable automatic error - discord.js-commando

137 views Asked by At

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

There are 2 answers

8
Compositr On BEST ANSWER

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`);
    // ....
  }
1
Thenlie On

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`);
}