lbry-tipbot/bot/bot.js

86 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2017-10-25 21:12:38 +02:00
'use strict';
// Load up libraries
2017-11-23 02:01:52 +01:00
const Discord = require('discord.js');
2017-10-25 21:12:38 +02:00
// Load config!
let config = require('config');
let botConfig = config.get('bot');
let commands = {};
2017-10-25 21:12:38 +02:00
const bot = new Discord.Client();
2017-10-25 21:12:38 +02:00
bot.on('ready', function() {
console.log(`Logged in! Serving in ${bot.guilds.array().length} servers`);
require('./plugins.js').init();
console.log(`type ${botConfig.prefix}help in Discord for a commands list.`);
bot.user.setActivity(botConfig.prefix + 'tip');
2017-10-25 21:12:38 +02:00
});
bot.on('disconnected', function() {
console.log('Disconnected!');
process.exit(1); //exit node.js with an error
2017-10-25 21:12:38 +02:00
});
function checkMessageForCommand(msg) {
//check if message is a command
if (msg.author.id !== bot.user.id && msg.content.startsWith(botConfig.prefix)) {
console.log(`treating ${msg.content} from ${msg.author} as command`);
let cmdTxt = msg.content.split(' ')[0].substring(botConfig.prefix.length);
let suffix = msg.content.substring(cmdTxt.length + botConfig.prefix.length + 1); //add one for the ! and one for the space
if (msg.isMentioned(bot.user)) {
try {
cmdTxt = msg.content.split(' ')[1];
suffix = msg.content.substring(bot.user.mention().length + cmdTxt.length + botConfig.prefix.length + 1);
} catch (e) {
//no command
return msg.channel.send('Yes?');
}
}
let cmd = commands[cmdTxt];
if (cmd) {
// Add permission check here later on ;)
try {
cmd.process(bot, msg, suffix);
} catch (e) {
let msgTxt = `command ${cmdTxt} failed :(`;
if (botConfig.debug) {
msgTxt += '\n' + e.stack;
2017-10-25 21:12:38 +02:00
}
msg.channel.send(msgTxt);
}
}
} else {
//message isn't a command or is from us
//drop our own messages to prevent feedback loops
if (msg.author === bot.user) {
return;
}
2017-10-25 21:12:38 +02:00
if (msg.author !== bot.user && msg.isMentioned(bot.user)) {
msg.channel.send('yes?'); //using a mention here can lead to looping
2017-10-25 21:12:38 +02:00
}
}
2017-10-25 21:12:38 +02:00
}
bot.on('message', msg => checkMessageForCommand(msg));
2017-10-25 21:12:38 +02:00
exports.addCommand = function(commandName, commandObject) {
try {
commands[commandName] = commandObject;
} catch (err) {
console.log(err);
}
2017-10-25 21:12:38 +02:00
};
exports.addCustomFunc = function(customFunc) {
try {
customFunc(bot);
} catch (err) {
console.log(err);
}
2017-11-23 02:01:52 +01:00
};
exports.commandCount = function() {
return Object.keys(commands).length;
2017-10-25 21:12:38 +02:00
};
bot.login(botConfig.token);