英文:
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
不,但是你可以创建一个包含消息和事件信息的集合,并检查新消息。
简单来说:
- 创建一个集合/数组
- 添加消息信息
- 在消息事件处理程序中检查传入的消息是否在集合中
- 处理事件
- 从集合中移除
不要忘记设置超时时间并清除过期的数据。
英文:
No, but you can make a collection that holds message and event information and checking news messages.
Simply
- Make a collection/array
- Add message information
- Check if the incoming message in the message event handler is in the collection
- Handle event
- Remove from collection
Don't forget set a timeout and clear expired data from collection.
答案2
得分: 0
根据文档:awaitMessages
> time:收集器运行的时间(以毫秒为单位)
>
> max:成功通过筛选器的消息数量
在Go语言中,你可以使用一个关键字go
轻松地创建一个例程,因此实现异步(async
)非常简单。
解决思路:
- 创建一个消息存储中心:具有以下功能:
- 存储所有发送的消息
- 具有垃圾回收机制:由于我们不断地收集消息,需要一种机制来清除旧消息。
- 需要一个互斥锁:考虑到消息可能会产生竞争条件,添加此锁以确保安全性。
- 它可以生成筛选器:为了与每个筛选器进行通信,我们需要为每个筛选器提供一个通道。
- 每当创建一个新消息时,我们将该消息添加到消息中心,并且还会通知每个筛选器。
- 定义你的筛选器函数:只有当满足此条件时,消息才会被收集。例如,示例。
- 定义回调函数:这是筛选器函数的结果。你可以根据这个结果做一些操作。例如,示例。
完整代码
我将完整的代码放在了replit和gist上。
如果你想运行它,可以从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:
- Create a message storage center: It has the following features:
- store all sent messages
- Have a garbage collection mechanism: Since we are constantly collecting messages, we need a mechanism to eliminate old messages.
- Need a mutex: Considering that the messages may generate race conditions, this lock is added to ensure security
- It can generate filters: to communicate with each filter, we need to provide a chan for each filter.
- Whenever a new message is created, we add the message to the message center, and it also notifies each filter.
- Define your filter function: The message will be collected whenever this criterion is true. for example
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论