2
votes

I've already tried using the ' node index.js ' and ' node index ' commands, but I receive a syntax error message every time.

Here's what my .js file looks like. I'm using Visual Studio Code.

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

const TOKEN = "MYTOKENHERE";

var Discord = require("discord.js");
var schedule = require('node-schedule');
var bot = new Discord.Client();

bot.on("message", function(message) {
    var channel = bot.channels.find("name", "general");
    channel.sendMessage("Hello Owner Just Restarted Me!");

    var rule = new schedule.RecurrenceRule();
    rule.minute = 0;
    rule.hour = [14, 19, 20];

    var j = schedule.scheduleJob(rule, function() {
        bot.channels.get("id", channel).sendMessage("Testing");
    })

    console.log("Bot is ready.");
});

bot.login(TOKEN);

The error is:

SyntaxError: Identifier 'Discord' has already been declared.

2
what isn't working? Have you authenticated your bot?Mike Tung
Please post the error as well, not just that you have oneSterling Archer
You declare Discord twice, by the waySterling Archer
@MariellaDeDios you just need to delete the second DiscordMike Tung
@MariellaDeDios I edited your question to include the error. As Mike said, you should just remove var Discord = on line 5.Nate Barbettini

2 Answers

4
votes

As the error states SyntaxError: Identifier 'Discord' has already been declared., it indicates that you have declared 'Discord' and then re-declared it... Which you have done here:

const Discord = require("discord.js"); // you've declared 'Discord' here

const TOKEN = "MYTOKENHERE";

var Discord = require("discord.js"); // and here again

Simply, remove the var Discord = require("discord.js"); line to fix the error, here is what that should look like:

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

const TOKEN = "MYTOKENHERE";

var schedule = require('node-schedule');
var bot = new Discord.Client();

bot.on("message", function(message) {
    var channel = bot.channels.find("name", "general");
    channel.sendMessage("Hello Owner Just Restarted Me!");

    var rule = new schedule.RecurrenceRule();
    rule.minute = 0;
    rule.hour = [14, 19, 20];

    var j = schedule.scheduleJob(rule, function() {
        bot.channels.get("id", channel).sendMessage("Testing");
    })

    console.log("Bot is ready.");
});

bot.login(TOKEN);
1
votes

It's in the syntax error itself, it is telling you that you have already declared a variable called Discord. In C++, the program would ignore the second variable. To fix this for javascript, you just have to take out one of the variables. Hope this was helpful. FYI, different code editors usually DON'T have a different effect on your code.