Last active
July 26, 2016 20:13
-
-
Save naikrovek/9aa966e1b1bedbe7c6234b049a41a724 to your computer and use it in GitHub Desktop.
Discord Bot a la lurkdis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
let Discord = require("discord.js"); | |
let bot = new Discord.Client({ | |
forceFetchUsers: true | |
}); | |
let botEmail = ""; | |
let botPasswd = ""; | |
let notifyUserIds = [ | |
"432143214321432142" // fake user | |
]; | |
let rolesToRebrodcast = [ | |
"123412341234123412" // fake role | |
]; | |
// start with an empty array of users to notify... | |
let notifyUsers = []; | |
// then populate it with User objects whose id matches the Ids above. | |
bot.on("ready", function () { | |
for (let user of bot.users) { | |
if (notifyUserIds.indexOf(user.id) != -1) { | |
notifyUsers.push(user); | |
} | |
} | |
}); | |
bot.on("serverNewMember", function (server, user) { | |
if (server.id === "special server id") { | |
for (let u = 0; u < notifyUsers.length; u++) { | |
bot.sendMessage(notifyUsers[u], "New user to server! Discord username is: " + user.name); | |
} | |
} | |
}); | |
bot.on("serverMemberRemoved", function (server, user) { | |
if (server.id === "special server id") { | |
for (let u = 0; u < notifyUsers.length; u++) { | |
bot.sendMessage(notifyUsers[u], "Eesh! User left server! Discord username is: " + user.name); | |
} | |
} | |
}); | |
// straight up exit when we get disconnected, so forever.js can restart us. | |
bot.on("disconnected", function () { | |
console.log("Disconnected!"); | |
process.exit(1); | |
}); | |
// if message is from an admin send it to the specified users. | |
bot.on("message", function(msg) { | |
for (let i = 0; i < rolesToRebrodcast.length; i++) { | |
if (msg.author.hasRole(rolesToRebrodcast[i]) ) { | |
let message = | |
"`" + new Date().toUTCString() + "`" // UTC time | |
+ " __" + msg.server.name + "__" // server name | |
+ " <#" + msg.channel.id + ">" // linked channel name | |
+ " **<@" + msg.author.id + ">** " // linked user name | |
+ msg.cleanContent; // message text | |
// actually send the message to the users configured. | |
for (let j = 0; j < notifyUsers.length; j++) { | |
bot.sendMessage(notifyUsers[j], message); | |
} | |
// drop out of the 'for' loop. if an admin is a member of two of the roles | |
// we look for, it will send twice, if we don't break out after the first. | |
break; | |
} | |
} | |
}); | |
// login. | |
bot.login(botEmail, botPasswd).then(success).catch(err); | |
// do this if we log in successfully. | |
function success(token) { | |
console.log("Connected!"); | |
} | |
// do this if we do not log in successfully. | |
function err(token) { | |
console.log("Error logging in. "); | |
process.exit(1); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment