TypeError:类扩展值命令不是构造函数或 null - node.js

2022-01-10 00:00:00 node.js javascript discord.js

I tried to make a !warn command, so that I can warn people in my server and if you have 2 warns you get a mute etc.

When I was done there was only 1 error left. I tried many things, but I just can't figure it out, every help would be awesome.

Full error:

 TypeError: Class extends value Command is not a constructor or null
   at Object.<anonymous> (/app/server.js:297:12)
   at Module._compile (internal/modules/cjs/loader.js:759:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
   at Module.load (internal/modules/cjs/loader.js:628:32)
   at Function.Module._load (internal/modules/cjs/loader.js:555:12)
   at Function.Module.runMain (internal/modules/cjs/loader.js:826:10)
   at internal/main/run_main_module.js:17:11

Code:

const ms = require("ms");

class Warn extends ("Command") {
constructor(client) {
    super(client, {
        name: "warn",
        description: "Command om een gebruiker te waarschuwen",
        usage: "warn",
        category: "System",
        permLevel: "Moderator"
    });
}

async run(message, args) {
try {
    const warnedUser = message.guild.member(
        message.mentions.users.first() || message.guild.members.get(args[0])
    );
    if (!warnedUser) return message.channel.send("Deze gebruiker bestaat niet");

    const warnToAdd = 1;
    const warnToDel = 4;
    const reason = args.join(" ").slice(22);

    this.client.warns.ensure(`${warnedUser.id}`, {
        warnings: 0
    });

    let userWarnings = this.client.warns.get(`${warnedUser.id}`, "warnings");
    userWarnings += warnToAdd;

    this.client.warns.set(`${warnedUser.id}`, userWarnings, "warnings");

    message.delete();

    if (this.client.warns.get(`${warnedUser.id}`, "warnings") == 1) {
        message.channel.send(`${warnedUser}, eerste waarschuwing (reden: ${reason})`);
    } else if (this.client.warns.get(`${warnedUser.id}`, "warnings") == 2) {
        const muteRole = message.guild.roles.find(x => x.name === "muted");
        if (!muteRole) message.guild.createRole("name", "muted");
        message.channel.send(
            `${warnedUser}, tweede waarschuwing (reden: ${reason})`
        );
        const muteTime = "1h";
        await warnedUser.addRole(muteRole.id);
        message.channel.send(
            `${warnedUser} is gemuted voor ${muteTime} (reden: ${reason})`
        );

        setTimeout(function() {
            warnedUser.removeRole(muteRole.id);
            message.channel.send(`De gebruiker ${warnedUser} is niet meer gemuted!`);
        }, ms(muteTime));

        } else if (this.client.warns.get(`${warnedUser.id}`, "warnings") == 3) {
            message.channel.send(`${warnedUser}, derde waarschuwing (reden: ${reason})`);
            message.channel.send(`:warning: Pas op ${warnedUser}, bij 4 waarschuwingen krijg je een kick`);
        } else if (this.client.warns.get(`${warnedUser.id}`, "warnings") == 4) {
            message.channel.send(`${warnedUser}, vierde waarschuwing, veel plezier met je kick (reden: ${reason})`);
            warnedUser.kick(reason);
            userWarnings -= warnToDel;
        }
    } catch (e) {
        console.log(e);
    }
}
}

module.exports = Warn;

If someone could help me with this it would be amazing. One thing that also surprised me is that when I put this code in Visual Studio Code, there are no errors, but when I put it in glitch.com, this error appears.

解决方案

It should be class Warn extends Command, not class Warn extends ("Command"). I think, you try to extend the Command class from Discord.js Commando.

You can only extend classes, not strings.

Also, make sure you import the class in your file:

const { Command } = require('discord.js-commando');
const ms = require("ms");

class Warn extends Command {
  constructor(client) {
    super(client, {
      name: "warn",
      ...

相关文章