你能等待用户使用bwmarrin/discordgo发送的响应消息吗?

huangapple go评论61阅读模式
英文:

Can you wait for user response message with bwmarrin/discordgo?

问题

在discordgo中,有没有等待用户响应消息的选项?我正在寻找类似于discord.js中的awaitMessages的功能。

英文:

Is there any option to wait for user response message in discordgo? I'm looking for something similar to discord.js awaitMessages.

答案1

得分: 0

不,但是你可以创建一个包含消息和事件信息的集合,并检查新消息。

简单来说:

  1. 创建一个集合/数组
  2. 添加消息信息
  3. 在消息事件处理程序中检查传入的消息是否在集合中
  4. 处理事件
  5. 从集合中移除

不要忘记设置超时时间并清除过期的数据。

英文:

No, but you can make a collection that holds message and event information and checking news messages.

Simply

  1. Make a collection/array
  2. Add message information
  3. Check if the incoming message in the message event handler is in the collection
  4. Handle event
  5. Remove from collection

Don't forget set a timeout and clear expired data from collection.

答案2

得分: 0

根据文档:awaitMessages

> time:收集器运行的时间(以毫秒为单位)
>
> max:成功通过筛选器的消息数量


在Go语言中,你可以使用一个关键字go轻松地创建一个例程,因此实现异步(async)非常简单。


解决思路:

  1. 创建一个消息存储中心:具有以下功能:
  2. 每当创建一个新消息时,我们将该消息添加到消息中心,并且还会通知每个筛选器
  3. 定义你的筛选器函数:只有当满足此条件时,消息才会被收集。例如,示例
  4. 定义回调函数:这是筛选器函数的结果。你可以根据这个结果做一些操作。例如,示例

完整代码

我将完整的代码放在了replitgist上。
如果你想运行它,可以从replit复制代码,并在环境变量中设置你的令牌和频道ID(或用户ID)进行测试。

英文:

according the docs: awaitMessages

> time: Amount of time in milliseconds the collector should run for
>
> max: Number of messages to successfully pass the filter


In Go, you can easily use a routine with just one keyword go, so implementing asynchronous (async) is very simple.


solving ideas:

  1. Create a message storage center: It has the following features:
  2. Whenever a new message is created, we add the message to the message center, and it also notifies each filter.
  3. Define your filter function: The message will be collected whenever this criterion is true. for example
  4. Define the callback function: This is the result of the filter function. You can do something based on this result. for example

Full code

I put the full code on the replit and gist.
If you want to run it, you can copy the code from replit and set your token and channel ID (or user ID) in the environment variables to test it.

huangapple
  • 本文由 发表于 2022年3月21日 22:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/71558928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定