Merge pull request #12 from filipnyquist/master

Added basic structure of modbot + topic controller.
This commit is contained in:
Fillerino 2017-06-15 19:16:05 +02:00 committed by GitHub
commit 5cb50a226d
2 changed files with 80 additions and 0 deletions

4
app.js
View file

@ -35,8 +35,12 @@ claimbot.init(slackbot, process.env.CLAIMS_CHANNEL, process.env.RPCUSER, process
var pricebot = require('./bots/pricebot'); var pricebot = require('./bots/pricebot');
pricebot.init(process.env.MARKET_TRADING_CHANNEL); pricebot.init(process.env.MARKET_TRADING_CHANNEL);
var modbot = require('./bots/modbot');
modbot.init(process.env.MONGODB_URL, process.env.SLACK_TOKEN, slackbot);
slackbot.on('start', function() { slackbot.on('start', function() {
slackbot.on('message', function(data) { slackbot.on('message', function(data) {
modbot.check(slackbot,data);
if (data.type == 'team_join') { if (data.type == 'team_join') {
setTimeout(function() { sendWelcomeMessage(data.user.id); }, 2000); //Delay because of slow slack api updates which sometimes does not send msg. setTimeout(function() { sendWelcomeMessage(data.user.id); }, 2000); //Delay because of slow slack api updates which sometimes does not send msg.
} }

76
bots/modbot.js Normal file
View file

@ -0,0 +1,76 @@
var needle = require('needle');
var slackbot;
var mongo;
var command = '!m';
var apitoken;
module.exports={
command: command,
init: init,
check: check
};
var globalSlackParams = {
icon_emoji: ':lbr:'
};
function init(mongodburl,apitoken_, slackbot) {
const MongoClient = require('mongodb').MongoClient;
MongoClient.connect(mongodburl, function (err, db) {
if (err) {
throw err;
}
mongo = db;
});
apitoken = apitoken_
slackbot.getChannels()
.then( data => {
data.channels.forEach(function(ch) {
mongo.collection('m_topic').findOneAndUpdate(
{ 'channel': ch.id },
{ 'channel': ch.id, 'topic': ch.topic.value },
{ 'upsert': true, 'returnOriginal': false },
function (err, obj) {
if(err){
console.log(err);
}
});
}, this);
}, err => {
console.log(err);
})
console.log('Loaded moderationbot!');
}
function check(slackbot, data) {
if(data.text){
if (data.text.trim().split(' ')[0] === command) {
//Add commands here later aswell!
}
}
if(data.topic){ // Gets called on topic change in a channel
slackbot.getUser(data.user_profile.name)
.then( usr => {
if(usr.is_admin){
mongo.collection('m_topic').findOneAndUpdate(
{ 'channel': data.channel },
{ 'channel': data.channel, 'topic': data.topic },
{ 'upsert': true, 'returnOriginal': false },
function (err, obj) {
if(err){
console.log(err);
}
});
}else if(!usr.is_bot){
mongo.collection('m_topic').findOne({'channel': data.channel}, function(err, document) {
slackbot.postMessage(data.user,`Hey <@${data.user_profile.name}>, you are not allowed to change the topic in <#${data.channel}>!`, globalSlackParams);
needle.get(`https://slack.com/api/channels.setTopic?token=${apitoken}&channel=${data.channel}&topic=${document.topic}`);
});
}
}, err => {
console.log(err);
})
}
}