user17263435
user17263435

Reputation:

How can I do a command on a query basis discord.js?

I want a command like -clear to be done on a query basis. e.g.

Thank you very much in advance for your replies! (V.12)


Ok!

I changed the code a bit and it works, but the problem is that the user can type anything, in this example:

module.exports = {
    name: 'clearr',
    description: "Clear messages!",
    async execute(client, message, args) {
        if(!args[0]) {
            let filter = m => m.author.id === '365113443898097666'
            message.channel.send(`How many messages do you want to delete?`).then(() => {
                message.channel.awaitMessages(filter, {
                    max: 1,
                    time: 10000,
                    errors: ['time']
                })
                .then(message => {
                    message = message.first()
                    message.channel.bulkDelete(message);
                    message.channel.send (`\`${message} message\` successfully deleted!`)
                    .then(message => {
                        message.delete({ timeout: 5000 })
                    })
                    .catch(console.error);
                })
            })
        }
    }
}

So I would like to eliminate this, so that it can only type a number, because if it types any other character, it will say "Not a valid value"

Thank you very much in advance for your replies!

Upvotes: 1

Views: 187

Answers (1)

MrMythical
MrMythical

Reputation: 9041

This should use message collectors. There are 2 ways to make them, but since you are only listening for 1 message, you can use TextChannel.awaitMessages.

client.on("messageCreate", async msg => {
  if (msg.content === "-clear") { //only respond to -clear
    const filter = (m) => m.author.id === msg.author.id //only accept from original author
    const message = await msg.channel.awaitMessages(filter, {
      max: 1, //only collect 1 message
      time: 10000 //time in ms they have to respond    
    })
    if (!parseInt(message.first()) return; //use .first() because it returns collection
  msg.channel.bulkDelete(parseInt(message.first())).then(ms => {
      msg.channel.send(`Deleted ${ms.size} messages.`)
    })
  }
})

This is untested, and "base" code. You will need to tweak this to your liking

Upvotes: 1

Related Questions