How to make a interactive command in discord.js

我的未来我决定 提交于 2020-01-26 04:52:10

问题


I am wondering how in discord.js to make a interactive giveaway command like the giveaway bot

I am in V11/12 For example when I do g!giveaway start, it starts a interactive setup what will work like this

The bot will say "Time"

Then I set the type it will last with variables (m for minutes, d for days, w for weeks)

Then it will say

"Okay! Now what do you want to giveaway?"

Then I just say what I want to giveaway

And then it will say

"Great! What channel will the giveaway be in?"

Then I put the channel

Then the bot says

"Good! The giveaway for (prize) has started in (channel) and will last (time) seconds/days/weeks

Please can I get some help here, thanks!


回答1:


Send a message and await a response using a collector (awaitMessages)

We will want to wait for a message after asking, so we will use a collector.

Async TextChannel.awaitMessages() (read docs) can be used to collect messages. It needs a filter to know which messages to accept, as well as some options to know when to stop collecting.

// accepted messages will be those from the same author, we compare IDs to make sure
const filter = msg => msg.author.id == message.author.id;

// the only option needed will be maxMatches, to only take one message before ending the collector
const options = {
  maxMatches: 1
};

The collector will then return a Collection of messages, we will always be taking .first() since there will only be one, and store its content.

// assuming you have the `channel` object, and are inside an async function
let collector = await channel.awaitMessages(filter, options);
let answer = collector.first().content;

Use the above after every channel.send() for each different answer you're looking from the user.

Example on how to use the Collector

client.on("message", async message => {
  if (message.content === "!color") {
    // request
    message.channel.send("What's your fav color?");

    // collector
    let collector = await message.channel.awaitMessages(filter, options);
    let answer = collector.first().content;

    // response
    await message.reply("your fav color is " + answer + "!");
  }
});

Note that this is just an example and in a real implementation you must handle errors properly. Here's the example result:

If you need more inputs just create more collectors and answers, and do with that information however you need to.




回答2:


You need to use MessageCollectors. For example:

let opt = {
    prize: null, 
    time: null
};
message.channel.send("Whats the prize?");
let collector = new Discord.MessageCollector(message.channel, () => true);
collector.on("collect", (m) => {
    if(opt.prize && !opt.time){
       opt.time = m.content;
       message.channel.send("Giveaway started! (prize:"+opt.prize+", time:"+opt.time+")");
       collector.stop();
    }
    if(!opt.prize && !opt.time){
         opt.prize = m.content;
         message.channel.send("ok! So what's the time?");
    }
});

You can add as many opt as you want, follow the same logic. You can use my npm package, discord-giveaways to create giveaways easily.



来源:https://stackoverflow.com/questions/58900409/how-to-make-a-interactive-command-in-discord-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!