0

I am trying to create a bot, which will be given the role when user joins the server by link, but this doesn't work. Error:

C:\DGhostsBot\bot.js:45
bot.on("guildMemberAdd", (member) => {
^

ReferenceError: bot is not defined
    at Object.<anonymous> (C:\DGhostsBot\bot.js:45:1)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

My code here:

const Discord = require('discord.js');

const client = new Discord.Client();

var prefix = "dg!"

client.login(`**************************************************************`);

client.on("message", (message) => {
    if(message.content == prefix + "test") {
        message.reply("just a command that is used for performance testing. Do not pay attention.");
     }
});

client.on("message", (message) => {
    if(message.content == prefix + "cake") {
        message.reply("here's your cake :3 :cake: ");
     }
});

client.on("message", (message) => {
    if(message.content == prefix + "help") {
        message.reply("it's in development");
}     
});

client.on("message", (message) => {
    if(message.content == prefix + "kick") {
if(message.member.roles.some(r=>["Developer", "devadmin"].includes(r.name)) ) {
     const user = message.mentions.users.first();
    if (user) {
      const member = message.guild.member(user);
      if (member) {
       member.kick()
}
}
} else {
 message.reply("!!!!ACCESS_DENIED!!!!").then(sentMessage => sentMessage.delete("delete"));

}
}     
});


bot.on("guildMemberAdd", (member) => {

    if (member.id == bot.user.id) {
        return;
    }
    let guild = member.guild
    guild.fetchInvites().then(invdat => {
        invdat.forEach((invite, key, map) => {
            console.log(invite.code)
            if (invite.code === "qQAkqFQ") {
                return member.addRole(member.guild.roles.find(role => role.name === "Member"));
            }
        })
    })
});

I check a lot of answers on the internet, but nothing of all this doesn't work. So, I do not know how to fix this error.

1
  • It's quite simple, you've never declared a variable called bot. Instead you have a variable called client. The fix: change bot.on(...) to client.on(...) Commented Mar 10, 2019 at 8:48

2 Answers 2

1

Instead of bot use client like you did all the times before.

There are people who do const client = new Discord.Client(); but there are also people who name it bot or even something really weired.

If you want to learn how to make your own bot, you can use the open-source guide created by the members and creators of discord.js wich can be found here: https://discordjs.guide/

Sign up to request clarification or add additional context in comments.

Comments

0

You have to change bot.on(...) to client.on(...)

You didn't define bot in previous code, so you can't use it. You defined your Discord client as client in the following line:

const client = new Discord.Client();

That's why you have to use client. You can't create a listenener for a non existing client.

For further details about all events of Discord.js, you can have a look at the official discord.js documentation: https://discord.js.org/#/docs/main/stable/class/Client

There you can also find all details on how to listen to events etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.