我的Discord机器人不会上线

时间:2018-02-12 01:21:46

标签: node.js visual-studio-code discord discord.js

我已经尝试过使用' node index.js'和'节点索引'命令,但每次都会收到语法错误消息。

这是我的.js文件的样子。我正在使用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);

错误是:

  

SyntaxError:Identifier' Discord'已经宣布。

2 个答案:

答案 0 :(得分:4)

由于错误状态SyntaxError: Identifier 'Discord' has already been declared.,它表示您已声明'Discord'然后重新声明它...您在此处所做的:

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

const TOKEN = "MYTOKENHERE";

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

简单地说,删除var Discord = require("discord.js");行来修复错误,这应该是这样的:

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 :(得分:0)

它在语法错误本身中,它告诉您已经声明了一个名为Discord的变量。在C ++中,程序会忽略第二个变量。要为javascript修复此问题,您只需要取出其中一个变量。 希望这有用。仅供参考,不同的代码编辑通常不会对您的代码产生不同的影响。