簡體   English   中英

這個腳本,它是我從 discord.js 的 V12 更新到 v13 的代碼

[英]this script, it's a code that I'm updating from V12 of discord.js to v13

想弄清楚錯誤的原因,如果再出現,大家可以改正,每次使用,whitelist命令都會出現如下錯誤(我的英文很基礎,拼寫錯誤太多請見諒)

E:\Desktop\Bot\commands\whitelist.js:30
  const whitelist = new Whitelist({
                    ^
TypeError: Whitelist is not a constructor

這是命令報錯的腳本

const Whitelist = require("../classes/whitelist.class");
const config = require("../config/whitelist.config");
const moment = require("moment-timezone");

const usersCooldown = {};
const whitelists = {};

module.exports = {
  run: async (client, message, args) => {
    const userItsNotOnCooldown = (userId) => {
      if (Array.isArray(usersCooldown[userId])) {
        if (usersCooldown[userId].length >= config.maximumTries) {
          const firstTryDate = usersCooldown[userId][0].date;
          if (
            Math.floor(Math.abs(new Date() - firstTryDate) / 1000 / 60) <=
            config.cooldown
          ) {
            return false;
          }
          delete usersCooldown[userId];
        }
      }
      return true;
    };
    const userId = message.author.id;

    if (typeof whitelists[userId] === "undefined") {
      if (userItsNotOnCooldown(userId)) {
        const whitelist = new Whitelist({
          message,
          client,
        });

        whitelist.on("finished", (whitelist) => {
          delete whitelists[userId];
          const data = {
            whitelist,
            date: new Date(),
          };

          // console.log(data) // @todo: log data into mongodb.

          if (!data.passed) {
            if (typeof usersCooldown[userId] === "undefined") {
              usersCooldown[userId] = [data];
              return;
            }

            usersCooldown[userId].push(data);
          }
        });

        whitelists[userId] = whitelist;
      } else {
        message.reply(
          `você atingiu o número máximo de tentativas, tente depois das: **${moment(
            usersCooldown[userId][0].date
          )
            .add(config.cooldown, "minutes")
            .tz("America/Sao_Paulo")
            .format(`DD/MM/YYYY [-] HH:mm`)}**`
        );
      }
    } else {
      message.reply("você só pode fazer uma whitelist por vez!");
    }
  },
};

如有必要,我會留下主文件

const Discord = require("discord.js");
const client = new Discord.Client({intents: 32767});
const leaveEvent = require('./events/leave.event');
const config = require('./config/whitelist.config');


client.on("ready", () => {
    console.log(`BOT FUNCIONANDO NORMALMENTE NOME DO BOT : ${client.user.tag}!`)
})

client.on('messageCreate', message => {
    if (message.author.bot) return;
    if (message.channel.type == 'dm') return;
    if (!message.content.toLowerCase().startsWith(config.prefix.toLowerCase())) return;
    if (message.content.startsWith(`<@!${client.user.id}>`) || message.content.startsWith(`<@${client.user.id}>`)) return;

   const args = message.content
       .trim().slice(config.prefix.length)
       .split(/ +/g);
   const command = args.shift().toLowerCase();

   try {
       const commandFile = require(`./commands/${command}.js`)
       commandFile.run(client, message, args);
   } catch (err) {
   console.error('Erro:' + err);
 }
});

client.on("guildMemberRemove", (member) => {
    leaveEvent(member)
});
client.login(config.discordClientId);

此問題與從 v12 更新到 v13 無關。 .class可能無法被require()識別。 確保whitelist.class文件是一個 JavaScript 文件,並將其擴展名更改為.js ,然后嘗試require() ing它。

錯誤是說Whitelist沒有引用 JavaScript 可以讀取為 class 構造函數的任何內容,這意味着它不是 JavaScript class,或者您的導入/導出存在問題。

您沒有從whitelist.class.js文件中導出 class。 請確保您的文件中包含module.exports =

它應該是這樣的:

class Whitelist {
  ...
}

module.exports = Whitelist;

或者像這樣:

module.exports = class Whitelist {
  ...
};

或者像這樣:

module.exports = class {
  ...
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM