Getting Data from Previous Commands

Viewed 287

I have been trying to get the Bio for my Discord Bot to save that a user has set using the setBio command, as every time I reset the bot the Bios would reset too. However, I came up with something that I'm not entirely sure how to do (if I can even do it at all).

After the bot resets and someone tries to use the setBio command for the first time, the bot looks through the message history and finds the last time that person used that command. It then uses args.slice(1) to only read the arguments for setting that Bio. The bot would then say something like:

I found a previous Bio that was set earlier. Type yes if you want to set your Bio to this:

Example bio

After that, if the user typed yes, that Bio would be used. In order to make it less annoying, it would only do that the FIRST time someone changed the Bio after it resets.

I only have that, and I don't know how it would be put in code. What would be used to make this possible?

Here is some code relevant to this question:

     case 'setBio':
        let newArr = args.slice(1)
        bio[message.author.id] = newArr
        message.channel.send('Your bio has been changed!')
            .then(msg => msg.delete(3000)); 
        break;
     case 'profile':
        if(!bio[message.author.id]) {
        return message.channel.send('Sorry, please set a bio with `!setBio` to view your profile!')
        } else {
        const embed = new Discord.RichEmbed()
            .setTitle('__' + message.author.username + '\'s Profile__')
            .addField('Bio:', bio[message.author.id].join(" "))
            .setColor(message.member.colorRole.color)
            .setThumbnail(message.author.avatarURL)
            message.channel.send(embed);
        }
        break;
1 Answers

This is possible, but there are more practical methods to accomplish your overall problem.

To answer your original question:

  1. Get all messages from the channel.

    TextChannel#messages is an property that contains all the messages in a channel.

  2. Filter out only the messages made by the user who called the command.

    Collection#filter(fn, thisArg) is a method that can filter out values in a collection based on any boolean-returning function fn.

  3. Filter out only the uses of the setBio command.

    Again, we will use Collection#filter.

  4. Get the first value from the collection of messages made by the user.

    Collection#last() is a method that gets the last value in a collection.

  5. Send a prompt, asking the user if they want to use the last bio set.
  6. Await user response.

    There are many ways to do this, but one way it can be done by making another message event, then waiting for a message where the user replies with either "yes" or "no".

So, you can use this piece of code:

case "setBio":
    let messages = message.channel.messages;
    let authorMessages = messages.filter(m => m.author.id === message.author.id);
    let setBioCommands = authorMessages.filter(m => m.content.startsWith(/*insert setBio command
                                                                           with prefix here*/);
    let firstBio = setBioCommands.last();
    // say user prompt, then wait for user to say yes.

However, a better way to approach this problem is to instead store all user data in a separate file, like what @Snel23 said.

This way, all user data will persist, even when the bot is turned off.

Related