编辑:我忘了为我使用的代码添加源代码,这里是:https://medium.com/davao-js/tutorial-creating-a-simple-discord-bot-9465a2764dc0
我一直在尝试编写一个discord bot,并且遇到了主文件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应用程序中,bot仍然显示为脱机且对!ping命令没有响应。我不知道如何解决这个问题,但我运行了节点-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文件有正确的信息,所以我不知道如何解决这个问题,非常感谢任何帮助!
答案 0 :(得分:0)
试试这个
const Discord = require('discord.js');
const bot = new Discord.Client();
const auth = require('./auth.json');
const token = auth.token;
bot.login(token);