【问题标题】:.push is not a function. (DISCORD.JS).push 不是函数。 (DISCORD.JS)
【发布时间】:2021-05-17 03:20:32
【问题描述】:

在给予用户 xp 时尝试对消息事件进行冷却。遇到cooldowns.push() is not a fucntion 的问题,我已经完成了谷歌搜索,我发现我需要一个数组,但据我了解,Discord.Collection 是一回事。

我需要这个来检查集合以查看用户 ID 是否在其中,但如果在 3 秒内未添加,则将其删除。

const config = require(`../settings/config.js`)
const prefix = config.prefix
const { createConnection } = require(`mysql`)
const mysql = require("../settings/mysql.js")
const Discord  = require(`discord.js`)
let cooldowns = new Discord.Collection()

module.exports = async (client, message, member) => {

    if (!cooldowns.has(message.author.id)) {
        cooldowns.push(message.author.id)
    }

    setTimeout(() => cooldowns.delete(message.author.id), 3000)

提前致谢!!

【问题讨论】:

  • Push 是一种数组方法。 cooldowns 是一个扩展 Map 的集合

标签: node.js arrays discord.js


【解决方案1】:

来自https://discordjs.guide/additional-info/collections.html#array-like-methods

Discord.js 带有这个称为 Collection 的实用程序类。它扩展了 JavaScript 的原生 Map 类,因此它具有 Map 的所有功能等等!

所以不,它不是一个数组,它是一个Map。在你的情况下,我认为你想使用:

cooldowns.set(message.author.id, 1)

【讨论】:

    猜你喜欢
    • 2016-11-08
    • 2020-12-31
    • 2020-12-07
    • 2020-08-23
    • 2020-01-22
    • 2020-11-13
    • 2017-11-28
    • 2021-06-15
    • 2020-12-22
    相关资源
    最近更新 更多