Skip to content

Instantly share code, notes, and snippets.

@naikrovek
Last active July 26, 2016 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naikrovek/9aa966e1b1bedbe7c6234b049a41a724 to your computer and use it in GitHub Desktop.
Save naikrovek/9aa966e1b1bedbe7c6234b049a41a724 to your computer and use it in GitHub Desktop.
Discord Bot a la lurkdis
'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