admin管理员组

文章数量:1377643

I am new to javascript as well have discord js, i am getting an error when running my discord bot, here, Also please fix the code and tell me what i did wrong, thankyou.

I have been making a all feature discord bot, and this is the first error i have found which i am unable to fix, i'd appreciate it greatly if any of you could help.

let args = message.content.slice(prefix.length).trim().split(' ');

ReferenceError: message is not defined

// Constant Variables
const Discord = require("discord.js");
const prefix = "!";
const client = new Discord.Client();

// Startup 1 Below Listener Events
client.once("ready", () => {
  console.log("Countless Is Up And Running, Wooohooo");
});

// Moderation 2 Below
client.on("Message", (Message) => {
  if (Message.member.hasPermission(["KICK_MEMBERS"])) {
    //console.log(message.content);

    if (Message.content.startsWith(`${prefix}kick`)) {
      //message.channel.send("Kick")

      let member = Message.mentions.members.first();
      member.kick().then((member) => {
        Message.channel.send(
          " :wave: " +
            member.displayName +
            " has been kicked!"
        );
      });
    }
  }
});

client.on("Message", (Message) => {
  if (Message.member.hasPermission(["BAN_MEMBERS"])) {
    //console.log(message.content);

    if (Message.content.startsWith(`${prefix}ban`)) {
      //message.channel.send("Ban")

      let member = Message.mentions.members.first();
      member.ban().then((member) => {
        Message.channel.send(
          " :wave: " +
            member.displayName +
            " has been banned!"
        );
      });
    }
  }
});

// Variables
let args = message.content.slice(prefix.length).trim().split(" ");
let cmd = args.shift().toLowerCase();

// Return Statements
if (Message.author.bot) return; // Ignores All Bots
if (!Message.content.startWith(prefix)) return; // Returns Messages if doesnt start with the prefix

// Command Handler
try {
  // Bonus: Auto-Reload ()
  delete require.cache[require.resolve(`./mands/${cmd}.js`)];

  let mandFile = require(`./mands/${cmd}.js`);
  mandFile.run(client, Message, args);
} catch (e) {
  // Will catch any errors within code or mands
  console.log(e.stack);
}

client.login(token);

I am new to javascript as well have discord js, i am getting an error when running my discord bot, here, Also please fix the code and tell me what i did wrong, thankyou.

I have been making a all feature discord bot, and this is the first error i have found which i am unable to fix, i'd appreciate it greatly if any of you could help.

let args = message.content.slice(prefix.length).trim().split(' ');

ReferenceError: message is not defined

// Constant Variables
const Discord = require("discord.js");
const prefix = "!";
const client = new Discord.Client();

// Startup 1 Below Listener Events
client.once("ready", () => {
  console.log("Countless Is Up And Running, Wooohooo");
});

// Moderation 2 Below
client.on("Message", (Message) => {
  if (Message.member.hasPermission(["KICK_MEMBERS"])) {
    //console.log(message.content);

    if (Message.content.startsWith(`${prefix}kick`)) {
      //message.channel.send("Kick")

      let member = Message.mentions.members.first();
      member.kick().then((member) => {
        Message.channel.send(
          "https://gph.is/2iBA79h :wave: " +
            member.displayName +
            " has been kicked!"
        );
      });
    }
  }
});

client.on("Message", (Message) => {
  if (Message.member.hasPermission(["BAN_MEMBERS"])) {
    //console.log(message.content);

    if (Message.content.startsWith(`${prefix}ban`)) {
      //message.channel.send("Ban")

      let member = Message.mentions.members.first();
      member.ban().then((member) => {
        Message.channel.send(
          "https://gph.is/29507Ei :wave: " +
            member.displayName +
            " has been banned!"
        );
      });
    }
  }
});

// Variables
let args = message.content.slice(prefix.length).trim().split(" ");
let cmd = args.shift().toLowerCase();

// Return Statements
if (Message.author.bot) return; // Ignores All Bots
if (!Message.content.startWith(prefix)) return; // Returns Messages if doesnt start with the prefix

// Command Handler
try {
  // Bonus: Auto-Reload ()
  delete require.cache[require.resolve(`./mands/${cmd}.js`)];

  let mandFile = require(`./mands/${cmd}.js`);
  mandFile.run(client, Message, args);
} catch (e) {
  // Will catch any errors within code or mands
  console.log(e.stack);
}

client.login(token);
Share Improve this question edited Jul 2, 2020 at 20:39 hgb123 14.9k3 gold badges23 silver badges43 bronze badges asked Jul 2, 2020 at 3:04 GelveyGelvey 11 gold badge1 silver badge1 bronze badge 5
  • 3 The error message is pretty clear; at that point in your code, message is not defined anywhere. What did you expect it to be? – Phil Commented Jul 2, 2020 at 3:20
  • I watched this video, as i said i am new to javascript and discordjs. how would i define it youtube./watch?v=Ygc04PcjoLk&t=223s – Gelvey Commented Jul 2, 2020 at 3:26
  • 1 Is this a typo? JavaScript is case-sensitive, and your code is using Message everywhere except in the line with the error (and in some ments....) Should the line with the error be let args = Message.content.slice(prefix.length).trim().split(' '); instead? – ad absurdum Commented Jul 2, 2020 at 3:51
  • Still doesnt work – Gelvey Commented Jul 2, 2020 at 4:29
  • 4 Well, Message still needs to be defined so you probably want to move the code into the scope which has Message defined. I.e. client.on('Message', Message => {... let args = Message.content.slice(prefix.length).trim().split(' '); } – Sam Gomena Commented Jul 2, 2020 at 4:50
Add a ment  | 

3 Answers 3

Reset to default 2

JavaScript is case sensitive.

clinet.on("message", Message => {});

In the following you defined message parameter with a capital "M".

So you need to mention the Message with a capital M.

Here's the fix for args V

let args = Message.content.slice(prefix.length).trim().split(' ');

Hope This helps.

本文标签: javascriptReferenceError message is not definedplease resolve thisStack Overflow