Skip to content

Instantly share code, notes, and snippets.

@barnden
Last active June 7, 2017 21:24
Show Gist options
  • Save barnden/1cb38b53ecd60dc8fa3308d466e3526a to your computer and use it in GitHub Desktop.
Save barnden/1cb38b53ecd60dc8fa3308d466e3526a to your computer and use it in GitHub Desktop.
.abot 1.0
var Discord = require('discord.js');
var Winston = require('winston');
var fs = require('fs');
var request = require('request');
var defaultDir = "C:\\Users\\Brandon\\Desktop\\DiscordJSBot\\";
var bot = new Discord.Client();
var logger = new (Winston.Logger)({
transports: [
new (Winston.transports.Console)({}),
new (Winston.transports.File)({filename: defaultDir + '\\logs2\\discord.' + new Date().getTime() + '.log'})
]
});
var superuser = [
"132757896952807424" /*a_verlieren*/
];
var admins = [
];
var mods = [
"134437116338896896" /*Rhebz*/,
"134468946576277505" /*Keebaun*/,
];
String.prototype.cfl = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
String.prototype.replaceAll = function(search, replacement) {
return this.replace(new RegExp(search, 'g'), replacement);
};
var commands = {
"airhorn": {
"command": function(message, args){
playSound(message, args, "airhorn");
},
"description": "Plays an airhorn sound.",
},
"coin": {
"command": function(message, args){
reply(message, String(Math.floor(Math.random() * 2)).replace('0', "Heads").replace('1', "Tails"), 15000);
},
"description": "Tosses a coin, heads or tails.",
},
"commands": {
"command": function(message, args){
commands["help"].command(message, args);
},
"description": "Alias for \"help\" command.",
},
"eb": {
"command": function(message, args){
playSound(message, args, "eb");
},
"description": "ETHAN BRADBERRY.",
},
"g": {
"command": function(message, args){
if(args[0].toLowerCase() === "list"){
reply(message,
"Game Role List:" + "\n" +
"League of Legends (IDs: 1, lol, league)" + "\n" +
"Counter-Strike: Global Offensive (IDs: 2, cs, csgo)" + "\n" +
"Hearthstone (IDs: 3, hearthstone)" + "\n" +
"Overwatch (IDs: 4, ow)" + "\n" +
"Smash (IDs: 5, smash)" + "\n" +
"osu! (IDs: 6, osu, osu!)" + "\n" +
"World of Warcraft (IDs: 7, wow, warcraft)" + "\n" +
"Garry's Mod (IDs: 8, gmod, garry)" + "\n" +
"Terraria (IDs: 9, terraria)" + "\n" +
"Brawlhalla (IDs: 10, brawlhalla, brawl)" + "\n" +
"Blade & Soul (IDs: 11, bladesoul, blade&soul)" + "\n",
5000
);
} else {
modifyGameRoles(args[0].toLowerCase(), message.sender, message.channel.server, message);
}
},
"description": "Join or leave a game role.",
},
"help": {
"command": function(message, args){
var keys = "";
for(var k in commands)
keys += "!" + k + " : " + commands[k].description + "\n";
reply(message, "\nCommands:\n\n" + keys, 15000);
},
"description": "Lists all commands",
},
"hs": {
"command": function(message, args){
var card = message.content.replace('!hs', '').trim().toLowerCase();
var replyHS = function(send){
if(message.channel instanceof Discord.TextChannel){
bot.sendMessage(message.channel, "<@" + message.sender.id + "> " + send, function(error, smsg){
if(message.channel.toString() != "<#172058190584741888>")
bot.deleteMessage(smsg, {wait: 5000});
});
} else {
bot.sendMessage(message.channel, "<@" + message.sender.id + "> " + send);
}
}
request('https://api.hearthstonejson.com/v1/latest/enUS/cards.collectible.json', function(error, response, body){
if (!error && response.statusCode == 200) {
var result = JSON.parse(body);
for(var i = 0; i < result.length; i++){
if(String(result[i].name).toLowerCase() == card){
try{
var rst = result[i];
var info = {
"name": rst.name,
"text": String(rst.text).replaceAll("<i>", "*").replaceAll("</i>", "*").replaceAll("<b>", "**").replaceAll("</b>", "**"),
"rarity": String(rst.rarity).toLowerCase(),
"type": String(rst.type).toLowerCase(),
"cost": rst.cost,
"attack": rst.attack,
"health": rst.health,
};
replyHS(
"\nHearthstone" + "\n\n" +
info.name + "\n" +
info.rarity.cfl() + "\n" +
info.text + "\n" +
info.type.cfl() + "\n" +
"Attack: " + info.attack + "\n" +
"Health: " + info.health + "\n" +
"Mana: " + info.cost + "\n"
);
} catch(exception){
reply(message, "Error occurred with card information.", 20000);
}
}
}
}
});
},
"description": "Hearthstone lookup",
},
"joke": {
"command": function(message, args){
request("http://tambal.azurewebsites.net/joke/random", function(error, response, body) {
if (!error && response.statusCode == 200) {
var result = JSON.parse(body);
reply(message, result.joke, 20000);
}
});
},
"description": "Generates a random joke.",
},
"motd": {
"command": function(message, args){
if(args[0].toLowerCase() === "set"){
var msg = message.content;
if(getRole(message.sender) != 'default'){
fs.writeFile(defaultDir + '\\conf\\motd.txt', msg.substr(msg.indexOf(' ', 9)).trim());
reply(message, "\nServer MOTD has been set to \"" + msg.substr(msg.indexOf(' ', 9)).trim() + "\"", 5000);
}
} else {
fs.readFile(defaultDir + '\\conf\\motd.txt', 'utf8', function (e, data) {
reply(message, "\nServer MOTD:\n" + data, 5000);
});
}
},
"description": "Message of the Day",
},
"ping": {
"command": function(message, args){
reply(message, "pong!", 5000);
},
"description": "Check if bot is online.",
},
"quit": {
"command": function(message, args){
if(getRole(message.sender) == 'superuser'){
reply(message, "Shutting down.", 1000);
setTimeout(function(){
bot.logout();
}, 2000);
}
},
"description": "Shutdown the bot.",
},
"soundboard": {
"command": function(message, args){
playSound(message, args, "soundboard");
},
"description": "Plays a random sound.",
},
"taps": {
"command": function(message, args){
if(getRole(message.sender) != 'default'){
bot.joinVoiceChannel(message.sender.voiceChannel, function(error, connection){
connection.playFile(defaultDir + "\\soundboard\\jc_scream.mp3", {volume: 0.25});
});
}
bot.deleteMessage(message, {wait: 2500});
},
"description": "They talk about my one taps.",
},
"togethertube": {
"command": function(message, args){
reply(message, "Our TogetherTube: https://togethertube.com/rooms/anarchy", 15000);
},
"description": "Watch TogetherTube, together.",
},
"weather": {
"command": function(message, args){
curWeather(message);
},
"description": "Get current weather for Cypress, TX area.",
},
}
function curWeather(message){
request("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22cypress%2C%20tx%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", function(error, response, body){
if (!error && response.statusCode == 200) {
var result = JSON.parse(body);
var data = {
"creation": String(result.query.created),
"conditions": {
"wind": {
"chill": String(result.query.results.channel.wind.chill),
"direction": String(result.query.results.channel.wind.direction),
"speed": String(result.query.results.channel.wind.speed),
},
"atmosphere": {
"humidity": String(result.query.results.channel.atmosphere.humidity),
"pressure": String(result.query.results.channel.atmosphere.pressure),
"rising": String(result.query.results.channel.atmosphere.rising),
"visibility": String(result.query.results.channel.atmosphere.visibility),
},
"astronomy": {
"sunrise": String(result.query.results.channel.astronomy.sunrise),
"sunset": String(result.query.results.channel.astronomy.sunset),
},
"current": {
"temperature": String(result.query.results.channel.item.condition.temp),
"weather": String(result.query.results.channel.item.condition.text),
}
}
};
var units = {
"distance": String(result.query.results.channel.units.distance),
"pressure": String(result.query.results.channel.units.pressure),
"speed": String(result.query.results.channel.units.speed),
"temperature": String(result.query.results.channel.units.temperature)
}
reply(message,
"```Weather for Cypress as of " + data.creation + "\n\n" +
"Temperature " + data.conditions.current.temperature + units.temperature + "\n" +
"Current conditon is " + String(data.conditions.current.weather).toLowerCase() + "\n" +
"Wind chill " + data.conditions.wind.chill + units.temperature + "\n" +
"Wind speed " + data.conditions.wind.speed + units.speed + "\n" +
"Wind direction " + degToCard(Number(data.conditions.wind.direction)) + "\n" +
"Humidity: " + data.conditions.atmosphere.humidity + "%\n" +
"Atmospheric pressure: " + data.conditions.atmosphere.pressure + units.pressure + "\n" +
"Pressure is " + risingToState(Number(data.conditions.atmosphere.rising)) + "\n" +
"Visibility " + data.conditions.atmosphere.visibility + units.distance + "\n" +
"Sunrise: " + data.conditions.astronomy.sunrise + "\n" +
"Sunset: " + data.conditions.astronomy.sunset + "\n" +
"```",
300000
);
}
});
}
function playSound(message, args, directory){
try{
if(getRole(message.sender) != 'default'){
bot.joinVoiceChannel(message.sender.voiceChannel, function(error, connection){
fs.readdir(defaultDir + directory, function(err, files){
var rand = files[Math.floor(Math.random() * files.length)];
connection.playFile(defaultDir + directory + "\\" + rand, {volume: 0.25});
});
});
}
} catch(exception){
console.log(exception);
}
bot.deleteMessage(message, {wait: 2500});
}
var risingToState = function(int){
switch(int){
case 1:
return "rising";
case 2:
return "falling";
default:
return "steady";
}
}
var degToCard = function(deg){
if (deg > 11.25 && deg < 33.75){
return "NNE";
} else if (deg > 33.75 && deg < 56.25){
return "NE";
} else if (deg > 56.25 && deg < 78.75){
return "ENE";
} else if (deg > 78.75 && deg < 101.25){
return "E";
} else if (deg > 101.25 && deg < 123.75){
return "ESE";
} else if (deg > 123.75 && deg < 146.25){
return "SE";
} else if (deg > 146.25 && deg < 168.75){
return "SSE";
} else if (deg > 168.75 && deg < 191.25){
return "S";
} else if (deg > 191.25 && deg < 213.75){
return "SSW";
} else if (deg > 213.75 && deg < 236.25){
return "SW";
} else if (deg > 236.25 && deg < 258.75){
return "WSW";
} else if (deg > 258.75 && deg < 281.25){
return "W";
} else if (deg > 281.25 && deg < 303.75){
return "WNW";
} else if (deg > 303.75 && deg < 326.25){
return "NW";
} else if (deg > 326.25 && deg < 348.75){
return "NNW";
} else{
return "N";
}
}
var gameIDToRole = function(game, server){
var names = {
1: "League of Legends",
2: "Counter-Strike: Global Offensive",
3: "Hearthstone",
4: "Overwatch",
5: "Smash",
6: "osu!",
7: "World of Warcraft",
8: "Garry's Mod",
9: "Terraria",
10: "Brawlhalla",
11: "Blade & Soul",
};
var common = {
"league": 1, "lol": 1,
"cs": 2, "csgo": 2,
"hearthstone": 3, "hs": 3,
"overwatch": 4, "ow": 4,
"smash": 5,
"osu": 6,
"wow": 7, "warcraft": 7,
"gmod": 8,
"terraria": 9,
"brawlhalla": 10, "brawl": 10,
"bladesoul": 11,
};
var id = undefined;
var ro = undefined;
if(isNaN(game)){
if(common.hasOwnProperty(game)){
id = Number(common[game]);
}
} else {
id = Number(game);
}
if(names.hasOwnProperty(id)){
server.roles.forEach(function(role, index, array){
if(role.name === names[id]){
ro = role;
}
});
}
return ro;
}
function modifyGameRoles(game, user, server, message){
var role = gameIDToRole(game, server);
if(role != undefined){
if(server.rolesOfUser(user).indexOf(role) > -1){
bot.removeUserFromRole(user, role);
reply(message, "You have been removed from " + role.name, 5000);
} else{
bot.addUserToRole(user, role);
reply(message, "You have been added to " + role.name, 5000);
}
}
return null;
}
function tag(message){
var msg = message.content.toLowerCase();
var server = message.channel.server;
var split = msg.split(' ');
var role = undefined;
var usrTagged = "";
split.forEach(function(p, pIndex, pArray){
if(p.indexOf("@") > -1){
if(gameIDToRole(p.replaceAll("@", ""), server) != undefined){
role = gameIDToRole(p.replaceAll("@", ""), server);
server.members.forEach(function(value, index, array){
server.rolesOfUser(value).forEach(function(v, i, a){
if(v == role && !(usrTagged.indexOf(value.id) > -1))
usrTagged += "<@" + value.id + ">\t";
});
});
}
}
});
if(role != undefined && message.sender.id != "134451876728012800" && usrTagged != ""){
bot.sendMessage(message.channel, "You have been tagged for " + role.name + "\n\n" + usrTagged + "\n\nBy <@" + message.sender.id + ">" + ": " + message.content);
return true;
}
return false;
}
function command(message){
var msg = message.content;
if(startsWith(msg, '!')){
var cmd = message.content.split(' ')[0].replace('!', '').toLowerCase();
var args = message.content.replace('!', '').replace(cmd, '').trim().split(' ');
var servers = bot.servers;
if(commands.hasOwnProperty(cmd)){
commands[cmd].command(message, args);
log("[CMD EXEC]", message.content, "info");
return true;
}
}
return false;
}
function reply(message, send, time){
if(time != -1){
bot.deleteMessage(message, {wait: time});
bot.sendMessage(message.channel, "<@" + message.sender.id + "> " + send, function(err, smsg){
bot.deleteMessage(smsg, {wait: time});
});
} else{
bot.sendMessage(message.channel, "<@" + message.sender.id + "> " + send);
}
}
function log(prefix, msg, level){
logger.log(level, prefix + ' ' + msg);
}
function getRole(user){
if(superuser.indexOf(user.id) > -1)
return 'superuser';
else if(admins.indexOf(user.id) > -1)
return 'admin';
else if(mods.indexOf(user.id) > -1)
return 'mod';
return 'default';
}
function startsWith(string, prefix){
return string.slice(0, prefix.length) == prefix;
}
bot.on('debug', function(debug){
log("[DEBUG]", debug, "info");
});
bot.on('message', function(message){
try{
command(message);
tag(message);
log("[MESSAGE]", "NEW[" + new Date().getTime() + "] | " + message.sender.id + " | " + message.sender.name + " | " + message.content, "info");
} catch(exception){
console.log(exception);
}
});
bot.on('messageUpdated', function(oldmsg, newmsg){
try{
log("[MESSAGE]", "UPDATE[" + new Date().getTime() + "] | " + newmsg.sender.id + " | " + newmsg.sender.name + " | OLD: " + oldmsg.content + " | NEW: " + newmsg.content, "info");
} catch(exception){}
});
bot.on('messageDeleted', function(message){
try{
log("[MESSAGE]", "DELETE[" + new Date().getTime() + "] | " + message.sender.id + " | " + message.sender.name + " | " + message.content, "info");
} catch(exception){}
});
bot.on('ready', function(){
log("[STATUS]", "Anarchy Discord Bot 2.0 has connected successfully, and is online.", "info");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment