2017-06-15 19:14:04 +02:00
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 ) ;
2017-06-21 21:14:04 +02:00
if ( process . env . CHANNEL _LOG ) {
slackbot . postMessage ( process . env . CHANNEL _LOG , ` User <@ ${ data . user _profile . name } > tried to change the topic in <# ${ data . channel } > to: \` \` \` ${ data . topic } ! \` \` \` ` , globalSlackParams ) ;
}
2017-06-15 19:14:04 +02:00
needle . get ( ` https://slack.com/api/channels.setTopic?token= ${ apitoken } &channel= ${ data . channel } &topic= ${ document . topic } ` ) ;
} ) ;
}
} , err => {
console . log ( err ) ;
} )
}
2017-06-21 21:14:04 +02:00
if ( data . type == 'pin_added' ) { // Gets called on a user pin
needle . get ( ` https://slack.com/api/users.info?token= ${ apitoken } &user= ${ data . user } ` , function ( err , resp ) {
if ( ! resp . body . user . is _admin ) {
needle . get ( ` https://slack.com/api/pins.remove?token= ${ apitoken } &channel= ${ data . item . channel } ×tamp= ${ data . item . message . ts } ` ) ;
slackbot . postMessage ( data . user , ` Hey <@ ${ resp . body . user . name } >, you are not allowed to add pins in <# ${ data . channel _id } >! ` , globalSlackParams ) ;
if ( process . env . CHANNEL _LOG ) {
slackbot . postMessage ( process . env . CHANNEL _LOG , ` User <@ ${ resp . body . user . name } > tried to pin a message in <# ${ data . channel _id } >! ` , globalSlackParams ) ;
}
}
} ) ;
}
if ( data . channel == process . env . CHANNEL _OA && data . type == 'message' ) { //If user is trying to post in a admin only channel..
needle . get ( ` https://slack.com/api/users.info?token= ${ apitoken } &user= ${ data . user } ` , function ( err , resp ) {
if ( ! resp . body . user . is _admin ) {
console . log ( ` https://slack.com/api/chat.delete?token= ${ apitoken } &ts= ${ data . ts } &channel= ${ data . channel } ` ) ;
needle . get ( ` https://slack.com/api/chat.delete?token= ${ apitoken } &ts= ${ data . ts } &channel= ${ data . channel } ` ) ;
if ( data . subtype !== 'channel_join' && data . subtype !== 'channel_leave' ) {
slackbot . postMessage ( data . user , ` Hey <@ ${ resp . body . user . name } >, you are not allowed to post messages in <# ${ data . channel } >! ` , globalSlackParams ) ;
if ( data . subtype !== 'channel_join' && data . subtype !== 'channel_leave' && process . env . CHANNEL _LOG ) {
slackbot . postMessage ( process . env . CHANNEL _LOG , ` User <@ ${ resp . body . user . name } > tried to post a message in <# ${ data . channel } >! ` , globalSlackParams ) ;
}
}
}
} ) ;
}
2017-06-15 19:14:04 +02:00
}