Guest User

Untitled

a guest
Nov 10th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Set up the Discord bot interface
  2. var Discord = require("discord.js");
  3. var bot = new Discord.Client();
  4.  
  5. //Set up the channel list
  6. var fs = require("fs");
  7. var channels;
  8. try {
  9.     channels = JSON.parse(fs.readFileSync("./channellist.json", "utf8"));
  10. } catch (err) {
  11.     if (err.code == "ENOENT") {
  12.         fs.writeFileSync("channellist.json", JSON.stringify({}));
  13.         channels = {};
  14.     }
  15. }
  16.  
  17.  
  18. //Function to get the key(s) relating to a value
  19. function getChannelKey(object, value) {
  20.     let keys = [];
  21.     for (var key in object) {
  22.         if (object[key].id == value) keys.push(key);
  23.     }
  24.     return keys;
  25. }
  26.  
  27. //Function to safely handle writing to stdout
  28. function safeWrite(sendstring) {
  29.     if (!process.stdout.write(sendstring)) {
  30.         safe = false;
  31.         process.stdout.once('drain', safeWrite(sendstring));
  32.     } else {
  33.         safe = true;
  34.     }
  35. }
  36. var safe = true;
  37.  
  38. //Set utf8 encoding for both stdin and stdout
  39. process.stdin.setEncoding('utf8');
  40. process.stdout.setDefaultEncoding('utf8');
  41.  
  42. //Receive input from primary program
  43. process.stdin.on('readable', () => {
  44.     let input = process.stdin.read();
  45.  
  46.     if (input !== null) {
  47.         //Get the channelname to send the message to
  48.         let separator = input.indexOf("$");
  49.         let channelid = input.substring(0, separator);
  50.         if (channels[channelid]) {
  51.             //Send the message to the proper channel if channelname has been registered to a channel.
  52.             bot.channels.get(channels[channelid].id).sendMessage("[" + channels[channelid].name + "] " + input.substring(separator + 1));
  53.         } else return;
  54.     }
  55. });
  56.  
  57. //Receive input from Discord
  58. bot.on('message', (message) => {
  59.     //Ignore own messages
  60.     if (message.author == bot.user) return;
  61.  
  62.     //Set the prefix
  63.     let prefix = "::";
  64.  
  65.     //If message is setting the channel
  66.     if (message.content.startsWith(prefix)) {
  67.         let command = message.content.substring(2).split(" ");
  68.         if (command[0] == "setchannel") {
  69.             if (command.length < 3) {
  70.                 message.channel.sendMessage("The setchannel command requires 2 arguments. ::setchannel channelid channelname");
  71.                 return;
  72.             }
  73.             //Get the first argument of the command (::setchannel channelid channelname)
  74.             let channelid = command[1];
  75.             let channelname = command.slice(2).join(" ");
  76.             channels[channelid] = { id: message.channel.id, name: channelname };
  77.             fs.unlinkSync("channellist.json");
  78.             fs.writeFileSync("channellist.json", JSON.stringify(channels));
  79.             message.channel.sendMessage("Messages from server " + channelid + " will now be sent to this channel with the prefix " + channelname + ".\n");
  80.         } else if (command[0] == "unsetchannel") {
  81.             let removelist = getChannelKey(channels, message.channel.id);
  82.             if (removelist.length === 0) {
  83.                 message.channel.sendMessage("There were no servers registered to this channel.");
  84.                 return;
  85.             }
  86.             for (let i = 0; i < removelist.length; i++) {
  87.                 delete channels[removelist[i]];
  88.             }
  89.             fs.unlinkSync("channellist.json");
  90.             fs.writeFileSync("channellist.json", JSON.stringify(channels));
  91.             message.channel.sendMessage("All servers previously registered to this channel will no longer send messages to this channel.\n");
  92.         } else return;
  93.     } else {
  94.         //Get an array of servers that match this channel id. End function if array length is 0 (unreigstered channel)
  95.         let sendto = getChannelKey(channels, message.channel.id);
  96.         var name;
  97.         if (message.member.nickname === null) name = message.author.username;
  98.         else name = message.member.nickname;
  99.         if (sendto.length === 0) return;
  100.         for (let i = 0; i < sendto.length; i++) {
  101.             while (!safe) {
  102.                 console.log("DEBUG: Buffer overflowing, waiting until it is safe.");
  103.             }
  104.             let sendstring = sendto[i] + "$[DISCORD] " + name + ": " + message.content + "\n";
  105.             safeWrite(sendstring);
  106.         }
  107.     }
  108. });
  109.  
  110. bot.on('ready', () => {
  111.     bot.user.setGame("Factorio");
  112. });
  113.  
  114. //WARNING: THIS TOKEN IS NOT TO BE SHARED TO THE PUBLIC
  115. var token = JSON.parse(fs.readFileSync("./token.json", "utf8"))
  116. bot.login(token);
Advertisement
Add Comment
Please, Sign In to add comment