node.js - 我的 Discord 机器人无法上线

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

我已经尝试过使用“nodeindex.js”和“nodeindex”命令,但每次都会收到语法错误消息。

这是我的 .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' has already been declared.

最佳答案

由于错误状态为 SyntaxError: Identifier 'Discord' has not been said.,它表明您已经声明了 '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);

关于node.js - 我的 Discord 机器人无法上线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48738334/

相关文章:

javascript - 在 Express Handlebars 中设置目录管理

node.js - 在 Docker 容器中安装 Web 应用程序 : dial unix/var/run/docker. sock:没有这样的文件或目录

javascript - Nest.JS 和 Observables

visual-studio-code - 没有管理员权限的 VS Code 扩展安装

java - 通过 unicode 添加表情符号 react 不起作用

python - 如何使用 Discord.py 重写更改机器人用户名/头像?

json - 如何使用 JSONStream 对大对象进行字符串化

visual-studio-code - 如何在 VS 代码中设置 Julia?

git - 连接到远程ssh时如何启用vs终端git命令自动完成?

javascript - 添加 react 以嵌入订单中