lbry-tipbot/bot/bot.js

110 lines
2.8 KiB
JavaScript
Raw 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');
config = config.get('bot');
var aliases;
try {
2017-11-23 02:01:52 +01:00
aliases = require('./alias.json');
2017-10-25 21:12:38 +02:00
} catch (e) {
//No aliases defined
aliases = {
2017-11-23 02:01:52 +01:00
test: {
process: function(bot, msg) {
2017-10-25 21:12:38 +02:00
msg.channel.send('test');
}
}
2017-11-23 02:01:52 +01:00
};
2017-10-25 21:12:38 +02:00
}
2017-11-23 02:01:52 +01:00
var commands = {};
2017-10-25 21:12:38 +02:00
var bot = new Discord.Client();
2017-11-23 02:01:52 +01:00
bot.on('ready', function() {
console.log('Logged in! Serving in ' + bot.guilds.array().length + ' servers');
require('./plugins.js').init();
console.log('type ' + config.prefix + 'help in Discord for a commands list.');
bot.user.setGame(config.prefix + 'tip');
2017-10-25 21:12:38 +02:00
});
2017-11-23 02:01:52 +01:00
bot.on('disconnected', function() {
console.log('Disconnected!');
2017-10-25 21:12:38 +02:00
process.exit(1); //exit node.js with an error
});
function checkMessageForCommand(msg, isEdit) {
//check if message is a command
if (msg.author.id != bot.user.id && msg.content.startsWith(config.prefix)) {
2017-11-23 02:01:52 +01:00
console.log('treating ' + msg.content + ' from ' + msg.author + ' as command');
var cmdTxt = msg.content.split(' ')[0].substring(config.prefix.length);
var suffix = msg.content.substring(cmdTxt.length + config.prefix.length + 1); //add one for the ! and one for the space
2017-10-25 21:12:38 +02:00
if (msg.isMentioned(bot.user)) {
try {
2017-11-23 02:01:52 +01:00
cmdTxt = msg.content.split(' ')[1];
suffix = msg.content.substring(bot.user.mention().length + cmdTxt.length + config.prefix.length + 1);
2017-10-25 21:12:38 +02:00
} catch (e) {
//no command
2017-11-23 02:01:52 +01:00
msg.channel.send('Yes?');
2017-10-25 21:12:38 +02:00
return;
}
}
let alias = aliases[cmdTxt];
if (alias) {
2017-11-23 02:01:52 +01:00
var cmd = alias;
2017-10-25 21:12:38 +02:00
} else {
2017-11-23 02:01:52 +01:00
var cmd = commands[cmdTxt];
2017-10-25 21:12:38 +02:00
}
if (cmd) {
// Add permission check here later on ;)
try {
cmd.process(bot, msg, suffix, isEdit);
} catch (e) {
2017-11-23 02:01:52 +01:00
var msgTxt = 'command ' + cmdTxt + ' failed :(';
2017-10-25 21:12:38 +02:00
if (config.debug) {
2017-11-23 02:01:52 +01:00
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;
}
if (msg.author != bot.user && msg.isMentioned(bot.user)) {
2017-11-23 02:01:52 +01:00
msg.channel.send('yes?'); //using a mention here can lead to looping
2017-10-25 21:12:38 +02:00
} else {
}
}
}
2017-11-23 02:01:52 +01:00
bot.on('message', msg => checkMessageForCommand(msg, false));
/*bot.on("messageUpdate", (oldMessage, newMessage) => {
2017-10-25 21:12:38 +02:00
checkMessageForCommand(newMessage, true);
});*/
2017-10-25 21:12:38 +02:00
exports.addCommand = function(commandName, commandObject) {
try {
commands[commandName] = commandObject;
} catch (err) {
console.log(err);
}
};
exports.addCustomFunc = function(customFunc) {
try {
customFunc(bot);
} catch (err) {
console.log(err);
}
2017-11-23 02:01:52 +01:00
};
2017-10-25 21:12:38 +02:00
exports.commandCount = function() {
return Object.keys(commands).length;
};
2017-11-23 02:01:52 +01:00
bot.login(config.token);