简体   繁体   English

Discord.JS 中的消息收集器未运行

[英]Message collector in Discord.JS not running

I have a basic command for testing the message collector feature on my discord.js v13 bot.我有一个基本命令,用于在我的 discord.js v13 机器人上测试消息收集器功能。

The bot doesn't crash when I run it, but the feature doesn't load properly, as it has an error.机器人在我运行时不会崩溃,但该功能无法正确加载,因为它有错误。

import { ICommand } from 'wokcommands'
import { Message } from 'discord.js'

export default {
    category: 'Testing',
    description: 'Tests the collector system',
    hidden: true,

    callback: ({ message, channel }) => {
        message.reply('Answer your username')

        const filter = (m: Message) => {
            m.author.id === message.author.id
        }

        const collector = channel.createMessageCollector({
            filter,
            max: 1,
            time: 1000 * 10,
        })

        collector.on('collect', message => {
            console.log(message.content)
        })
        
        collector.on('end', collected => {
            if (collected.size === 0) {
                message.reply('You did not provide your username')
                return
            }

            let text = 'Collected:\n\n'

            collected.forEach((message) => {
                text += `${message.content}\n`
            })

            message.reply(text)
        })
    }
} as ICommand

The error is in the line inside the collector function when I call filter.当我调用过滤器时,错误出现在收集器函数内的行中。 The IDE gives me an error: IDE 给了我一个错误:

Type '(m: Message) => void' is not assignable to type 'CollectorFilter<[Message]>'.类型 '(m: Message) => void' 不能分配给类型 'CollectorFilter<[Message]>'。 Type 'void' is not assignable to type 'boolean |类型 'void' 不能分配给类型 'boolean | Promise'.承诺'。

I understand what the error is trying to say, but how can I fix it?我明白错误的意思,但我该如何解决?

Your filter needs to return a boolean.您的filter需要返回一个布尔值。 At the moment you don't return anything, you just compare the two variables.目前你没有返回任何东西,你只是比较两个变量。 Any of these will work:这些中的任何一个都可以工作:

const filter = (m: Message) => {
  return m.author.id === message.author.id
}
const filter = (m: Message) => m.author.id === message.author.id

Zsolt Meszaros is to be commended. Zsolt Meszaros 值得称赞。 There has to be a boolean return from your filter.您的过滤器必须有一个布尔值返回。 Right now, there is no output, you only compare the two variables.现在,没有输出,您只需比较两个变量。 You can choose from the following code he mentioned.您可以从他提到的以下代码中进行选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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