简体   繁体   中英

How do i make a madlib with a discord bot? (discord JS v12)

Im trying to make a madlib with a discord bot and somehow i have to assign a variable to each line of the user's message...

here's my code so far:

client.on('message', message=>{//madlib
    var lib1 = [
        ' ',//<--(X)
        'VERB',
        'ADJECTIVE',
        'NOUN (PLURAL)',
        'ADJECTIVE',
        'VERB ENDING IN "ING"',
        'VERB',
        'NUMBER',
        'ADJECTIVE',
        'NOUN (PLURAL)',
        'NOUN (PLURAL)',
        'NOUN (PLURAL)',
        'RELATIVE (PLURAL)',
        'ADJECTIVE',
        'ADJECTIVE',
        'NOUN (PLURAL)',
    ]
    
    
    if (message.content == '!madlib'){
        var rand = Math.floor(Math.random() * 2) + 1;
        if (rand == '1'){
            message.reply(lib1);
        }
        if (rand == '2'){
            //im gonna put another madlib right here as another option and more random options.
        }
    }

})

not sure how the user is gonna insert the variables... does somebody know how to?

You need to figure out how to make your bot interactive. It can send a message like "Give me a VERB" and then take the response from a command in chat. I don't know how to do this for Discord specifically, so you will need to do some further research about how to get a response from a user.

You can simply use message.channel.awaitMessages() to await a response from a user.

Here's an example:

message.reply("Send a message!! (expires in 10s)")
const filter = m => m.autor.id === message.author.id;
message.channel.awaitMessages(filter, {
   max: 1,
   time: 10_000,//(10 seconds)
   errors: ['time']
})
  .then(async(msgs) => {
    //msgs is a collection. You need to do msgs.first() to get the first message
     const msg = msgs.first();
      //now just use "msg" as your normal message variable, like so
          message.reply("I have received your message: " + msg.content)
})
  .catch(() => message.reply("You took too long"))

Also see https://discordjs.guide/popular-topics/collectors.html#await-messages

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM