Skip to content

Instantly share code, notes, and snippets.

@naikrovek
Last active April 19, 2016 20:01
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/ec9068fa57840b468ce7 to your computer and use it in GitHub Desktop.
Save naikrovek/ec9068fa57840b468ce7 to your computer and use it in GitHub Desktop.
very simple lurkdis-like bot for discord.
'use strict';
// set up the Discord client.
let Discord = require("discord.js");
let bot = new Discord.Client({ forceFetchUsers: true });
// this is not an api-compatible bot, so we must log
// in as a regular user. These are those credentials.
let botEmail = "";
let botPasswd = "";
let targetUserIDs = [
"12341234",
"23452345",
"34563456"
];
// empty list of destination user objects. we populate it
// when the client says it is ready and the "ready" event
// fires.
let targetUserObjects = [];
bot.on("ready", function () {
// find the users who match the targetUserIDs and add
// them to an array of users to send messages to.
for (let user of bot.users) {
if (targetUserIDs.indexOf(user.id) !== -1) {
targetUserObjects.push(user);
}
}
// for sanity, print out the usernames & discriminator
// numbers of the users we're going to send to.
for (let user of targetUserObjects) {
console.log("Sending to: " + user.name + "#" + user.discriminator);
}
});
// exit when disconnected so that forever.js can restart us promptly.
bot.on("disconnected", function () {
console.log("Disconnected!");
process.exit();
});
// if message is from an admin send it to the specified users.
bot.on("message", function(msg) {
if ( msg.author.hasRole("09876") || // you'll have to find the IDs for the
msg.author.hasRole("98765") || // role(s) you want to rebroadcast
msg.author.hasRole("87654")) { // messages from.
for (let targetUser of targetUserObjects) {
bot.sendMessage(
targetUser,
"`" + timeStamp() + " UTC" + "` " +
" *" + msg.channel.server.name + "* " +
"__#" + msg.channel.name + "__ " +
"**" + msg.author.name + "** " +
msg.cleanContent
);
}
}
});
// log in to Discord.
bot.login(botEmail, botPasswd).then(success).catch(err);
// show login success message when login is successful.
function success(token) {
console.log("Connected!");
}
// show login failure message when login is unsuccessful.
function err(token) {
console.log("Error: " + token);
}
// get a human readable date representing this instant.
function timeStamp() {
let now = new Date();
let date = [now.getMonth() + 1, now.getDate(), now.getFullYear()];
let time = [now.getHours(), now.getMinutes(), now.getSeconds()];
let suffix = (time[0] < 12) ? "AM" : "PM";
time[0] = (time[0] < 12) ? time[0] : time[0] - 12;
time[0] = time[0] || 12;
for (let i = 1; i < 3; i++) {
if (time[i] < 10) {
time[i] = "0" + time[i];
}
}
return date.join("/") + " " + time.join(":") + " " + suffix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment