Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

javascript - TypeError: Class extends value Command is not a constructor or null - node.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.

question from:https://stackoverflow.com/questions/65644880/typeerror-class-extends-value-command-is-not-a-constructor-or-null-node-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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",
      ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...