显示离线的不和谐机器人,机器人.js中的引用错误



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

我一直在尝试编写一个不和谐的机器人,并且主文件机器人遇到了问题.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);

这样可以保持程序运行,但是在不和谐应用程序中,机器人仍显示脱机并且对 !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文件具有正确的信息,所以我不确定如何解决此问题,任何帮助将不胜感激!

试试这个

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

相关内容

最新更新