hackedshadow
hackedshadow

Reputation: 15

Can Command B only be Executed if command A is executed succesfully

I am trying to make a smart questions bot, and I was wondering if there is a way for allowing command B to be executed if command A was executed first

    } else if(message.content.match(/Discord/gi)){
    const Embed = new Discord.MessageEmbed()
}

This looks for messages that contains Discord (lowercase or uppercase) I don't want the bot to look for every message that contains Discord. I was hoping that It would only execute if the preceding command was executed first, and then after that it would also be disabled, sounds complicated but possible

Upvotes: 0

Views: 102

Answers (1)

Cannicide
Cannicide

Reputation: 4520

Assuming I understood your intentions correctly, you want to only look for command B after command A is called, and then once command B is executed, you want to stop looking for it. This can be achieved using a message collector that searches for command B. Here is some sample code (works in the latest version):

if (message.content.match("Command A")) {
    //Execute your command A code
    message.channel.send("Did some command A stuff");

    //After command A code is executed, do the following:

    var filter = m => m.author.id == message.author.id;

    //Creates a message collector, to collect messages sent by the same user
    //When max is 1, only 1 message will be collected and then the collector dies
    const collector = new Discord.MessageCollector(message.channel, filter, {max: 1});

    collector.on("collect", msg => {
        //msg is the new message that the user just sent, so check if it contains Discord
        if(msg.content.match(/Discord/gi)){
            //Do your command B stuff:
            const Embed = new Discord.MessageEmbed()
        }
    });
}

This code checks for Command A, and executes the Command A code. Then it creates a message collector. The message collector will wait until the user that just executed Command A sends another message. Once the user sends another message, it will run the code listening for the collect event. So we take the collected message, in this case msg, and we check if it matches "Discord". From there, you can do your Command B functionality.

Note that immediately after the user sends a message after executing Command A, the collector ends. That means if the user does not enter a message containing "Discord" within 1 attempt, the collector ends and the user must execute Command A once again in order to attempt to execute Command B again. If you want to allow the user to make more attempts to execute Command B, or if you want the user to be able to execute Command B more than once in a row after executing Command A, then you'll need to increase the value of max.

Upvotes: 1

Related Questions