Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
disable automatic error - discord.js-commando

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}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

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`);
}
about 4 years ago · Juan Pablo Isaza Relatório

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`);
    // ....
  }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda