【问题标题】:Discord bot showing offline, ReferenceError in bot.jsDiscord bot 显示离线,bot.js 中的 ReferenceError
【发布时间】:2018-11-01 19:54:20
【问题描述】:

编辑:我忘了放我使用的代码的来源,这里是:https://medium.com/davao-js/tutorial-creating-a-simple-discord-bot-9465a2764dc0

我一直在尝试编写一个不和谐的机器人,但在主文件 bot.js 中遇到了问题,代码如下:

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, {
    colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var bot = new Discord.Client({
   token: auth.token,
   autorun: true
});
bot.on('ready', function (evt) {
    logger.info('Connected');
    logger.info('Logged in as: ');
    logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function (user, userID, channelID, message, evt) {
    // Our bot needs to know if it will execute a command
    // It will listen for messages that will start with `!`
    if (message.substring(0, 1) == '!') {
        var args = message.substring(1).split(' ');
        var cmd = args[0];

        args = args.splice(1);
        switch(cmd) {
            // !ping
            case 'ping':
                bot.sendMessage({
                    to: channelID,
                    message: 'Pong!'
                });
            break;
            // Just add any case commands if you want to..
         }
     }
});

我尝试通过导航到保存文件的目录并在控制台中键入“node bot.js”来运行此程序。该程序似乎开始正常,但几乎立即停止,所以我将这段代码添加到底部:

setInterval(function() {
    console.log("timer that keeps nodejs processing running");
},1000*60*60);

这使程序保持运行,但是在 discord 应用程序中,机器人仍然显示脱机并且对 !ping 命令没有响应。我不知道如何解决这个问题,但我运行了 node -p bot.js 并得到了这个输出:

[eval]:1
bot.js
^

ReferenceError: bot is not defined
    at [eval]:1:1
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at Object.runInThisContext (vm.js:139:38)
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (module.js:652:30)
    at evalScript (bootstrap_node.js:463:27)
    at startup (bootstrap_node.js:164:9)
    at bootstrap_node.js:609:3

我的 package.json 和 auth.json 文件有正确的信息,所以我不知道如何解决这个问题,任何帮助将不胜感激!

【问题讨论】:

    标签: javascript node.js discord


    【解决方案1】:

    试试这个

    const Discord = require('discord.js');
    const bot = new Discord.Client();
    const auth = require('./auth.json');
    const token = auth.token;
    bot.login(token);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-16
      • 2021-04-16
      • 2020-09-10
      • 2021-05-13
      • 2021-06-28
      • 2018-10-16
      • 2021-10-28
      • 2021-01-21
      相关资源
      最近更新 更多