Last active
April 19, 2016 20:01
-
-
Save naikrovek/ec9068fa57840b468ce7 to your computer and use it in GitHub Desktop.
very simple lurkdis-like bot for discord.
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'; | |
// 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