如何在discord.js中进行交互式命令 [英] How to make a interactive command in discord.js

查看:95
本文介绍了如何在discord.js中进行交互式命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在discord.js中如何做出像赠品机器人这样的交互式赠品命令



我在V11 / 12中 b $ b例如,当我执行g!giveaway start时,它将启动一个交互式设置,其工作方式如下:



机器人会说
Time



然后,我设置它将持续变量的类型(m代表分钟,d代表天,w代表周)



然后它会说



好吧!现在您想赠予什么?



然后我只需说出我想赠予的东西



然后它就会说



太好了!赠品将通过什么渠道是?



然后我放置频道



然后机器人说



好!(奖赏)的赠品已经在(频道)开始,并且将持续(时间)秒/天/周



请给我帮助,谢谢!

解决方案

发送一条消息并使用收集器(awaitMessages)等待响应。



我们将在询问后等待消息,因此我们将使用收集器。



异步 TextChannel.awaitMessages()



如果您需要更多输入只会创建更多的收集器和答案,并根据需要使用该信息。


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!

解决方案

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.

这篇关于如何在discord.js中进行交互式命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆