Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Set up the Discord bot interface
- var Discord = require("discord.js");
- var bot = new Discord.Client();
- //Set up the channel list
- var fs = require("fs");
- var channels;
- try {
- channels = JSON.parse(fs.readFileSync("./channellist.json", "utf8"));
- } catch (err) {
- if (err.code == "ENOENT") {
- fs.writeFileSync("channellist.json", JSON.stringify({}));
- channels = {};
- }
- }
- //Function to get the key(s) relating to a value
- function getChannelKey(object, value) {
- let keys = [];
- for (var key in object) {
- if (object[key].id == value) keys.push(key);
- }
- return keys;
- }
- //Function to safely handle writing to stdout
- function safeWrite(sendstring) {
- if (!process.stdout.write(sendstring)) {
- safe = false;
- process.stdout.once('drain', safeWrite(sendstring));
- } else {
- safe = true;
- }
- }
- var safe = true;
- //Set utf8 encoding for both stdin and stdout
- process.stdin.setEncoding('utf8');
- process.stdout.setDefaultEncoding('utf8');
- //Receive input from primary program
- process.stdin.on('readable', () => {
- let input = process.stdin.read();
- if (input !== null) {
- //Get the channelname to send the message to
- let separator = input.indexOf("$");
- let channelid = input.substring(0, separator);
- if (channels[channelid]) {
- //Send the message to the proper channel if channelname has been registered to a channel.
- bot.channels.get(channels[channelid].id).sendMessage("[" + channels[channelid].name + "] " + input.substring(separator + 1));
- } else return;
- }
- });
- //Receive input from Discord
- bot.on('message', (message) => {
- //Ignore own messages
- if (message.author == bot.user) return;
- //Set the prefix
- let prefix = "::";
- //If message is setting the channel
- if (message.content.startsWith(prefix)) {
- let command = message.content.substring(2).split(" ");
- if (command[0] == "setchannel") {
- if (command.length < 3) {
- message.channel.sendMessage("The setchannel command requires 2 arguments. ::setchannel channelid channelname");
- return;
- }
- //Get the first argument of the command (::setchannel channelid channelname)
- let channelid = command[1];
- let channelname = command.slice(2).join(" ");
- channels[channelid] = { id: message.channel.id, name: channelname };
- fs.unlinkSync("channellist.json");
- fs.writeFileSync("channellist.json", JSON.stringify(channels));
- message.channel.sendMessage("Messages from server " + channelid + " will now be sent to this channel with the prefix " + channelname + ".\n");
- } else if (command[0] == "unsetchannel") {
- let removelist = getChannelKey(channels, message.channel.id);
- if (removelist.length === 0) {
- message.channel.sendMessage("There were no servers registered to this channel.");
- return;
- }
- for (let i = 0; i < removelist.length; i++) {
- delete channels[removelist[i]];
- }
- fs.unlinkSync("channellist.json");
- fs.writeFileSync("channellist.json", JSON.stringify(channels));
- message.channel.sendMessage("All servers previously registered to this channel will no longer send messages to this channel.\n");
- } else return;
- } else {
- //Get an array of servers that match this channel id. End function if array length is 0 (unreigstered channel)
- let sendto = getChannelKey(channels, message.channel.id);
- var name;
- if (message.member.nickname === null) name = message.author.username;
- else name = message.member.nickname;
- if (sendto.length === 0) return;
- for (let i = 0; i < sendto.length; i++) {
- while (!safe) {
- console.log("DEBUG: Buffer overflowing, waiting until it is safe.");
- }
- let sendstring = sendto[i] + "$[DISCORD] " + name + ": " + message.content + "\n";
- safeWrite(sendstring);
- }
- }
- });
- bot.on('ready', () => {
- bot.user.setGame("Factorio");
- });
- //WARNING: THIS TOKEN IS NOT TO BE SHARED TO THE PUBLIC
- var token = JSON.parse(fs.readFileSync("./token.json", "utf8"))
- bot.login(token);
Advertisement
Add Comment
Please, Sign In to add comment