const evalTemplate = util.evalTemplate;
const hexToRGB = util.hexToRGB;

let firstConnect = true;

function formatFromDiscord(format, _message) {
    const scope = {
        username: _message.member.user.username,
        nick: _message.member.nick || _message.member.user.username,
        channel: _message.channel.name || "Unknown channel",
        message: _message.content,
        _message
    };
    return evalTemplate(format, scope);
}

function formatTopic() {
    const scope = {
        players: jcmp.players.length,
        maxPlayers: JSON.parse(jcmp.server.config).maxPlayers,
        jcmp
    };
    return evalTemplate(config.formatting.gameToDiscord.topic, scope);
}

function setTopic(topic) {
    eris.editChannel(config.eris.id, { topic }).catch((reason) => {
        log(`Could not update topic (reason: ${reason})`);
    });
}

eris.on("ready", () => {
    log("Connected to Discord.")
    eris.createMessage(config.eris.id, (firstConnect ? config.gameToDiscord.dConnect : config.gameToDiscord.dReconnect)).catch((reason) => {
        log(`Could not send connection message (reason: ${reason})`);
    });
    eris.editStatus("online", { name: config.eris.playing });

    // Topic updater
    if (config.eris.topicTimeout > 0) {
        setInterval(() => {
            setTopic(formatTopic());
        }, config.eris.topicTimeout);
    }
});

eris.on("messageCreate", (_message) => {
    if (_message.channel.id === config.eris.id && _message.member.id !== eris.user.id) {
        const message = formatFromDiscord(config.formatting.discordToGame.chat, _message);
        chat.broadcast(message, hexToRGB(config.formatting.discordToGame.colour));
        log(`Discord: ${message}`);
    }
});

process.on("exit", () => {
    eris.createMessage(config.eris.id, config.gameToDiscord.dExit).catch(() => log("wat"));
    eris.editStatus("dnd", { name: "Server stopping" });
});

@@ -906,95 +906,60 @@
 d.%22)
+;
 %0A    
-eris.createMessage(config.eris.id, (firstConnect ? config.gameToDiscord.dConnect :
+let msg;%0A    if (firstConnect) %7B%0A        msg =
  con
@@ -981,181 +981,52 @@
 rd.d
-Rec
+C
 onnect
-)).catch((reason) =%3E %7B%0A        log(%60Could not send connection message (reason: $%7Breason%7D)%60);%0A    %7D);%0A    eris.editStatus(%22online%22, %7B name: config.eris.playing %7D);%0A%0A
+;%0A        firstConnect = false;%0A%0A    
     
@@ -1042,16 +1042,20 @@
 updater%0A
+    
     if (
@@ -1094,16 +1094,20 @@
         
+  
+  
 setInter
@@ -1114,24 +1114,28 @@
 val(() =%3E %7B%0A
+    
             
@@ -1167,16 +1167,20 @@
         
+  
+  
 %7D, confi
@@ -1201,25 +1201,299 @@
 meout);%0A    
-%7D
+    %7D%0A    %7D else %7B%0A        msg = config.gameToDiscord.dReconnect;%0A    %7D%0A    eris.createMessage(config.eris.id, msg).catch((reason) =%3E %7B%0A        log(%60Could not send connection message (reason: $%7Breason%7D)%60);%0A    %7D);%0A    eris.editStatus(%22online%22, %7B name: config.eris.playing %7D);
 %0A%7D);%0A%0Aeris.o

Refactor firstConnect check