|
| 1 | +const Discord = require("discord.js"); |
| 2 | +const { searchOneDB, giveFactorioRole, insertOneDB, findOneAndReplaceDB } = require("../../functions"); |
| 3 | +const { LinkingCache } = require("../../functions"); |
| 4 | +const { ErrorManager } = require("../../utils/error-manager"); |
| 5 | +const lodash = require("lodash"); |
| 6 | +let { linkConfirmation } = require("../../config/messages.json") |
| 7 | + |
| 8 | +module.exports = { |
| 9 | + config: { |
| 10 | + name: "linkme", |
| 11 | + aliases: [], |
| 12 | + usage: "<linking ID>", |
| 13 | + category: "factorio", |
| 14 | + description: "Link yourself to Factorio", |
| 15 | + accessableby: "Members", |
| 16 | + }, |
| 17 | + run: async (client, message, args) => { |
| 18 | + if (!args[0]) { |
| 19 | + return message.channel.send("Provide a linking ID!"); |
| 20 | + } |
| 21 | + let linkingID |
| 22 | + try { |
| 23 | + linkingID = parseInt(args[0]); |
| 24 | + if (Number.isNaN(linkingID)) return message.channel.send("Provide a number given by the server, not a string!"); |
| 25 | + } catch (error) { |
| 26 | + ErrorManager.Error(error); |
| 27 | + return message.channel.send(`Error: ${error}`); |
| 28 | + } |
| 29 | + const factorioName = LinkingCache.take(linkingID); |
| 30 | + if (factorioName === undefined) |
| 31 | + return message.channel.send("Wrong linking ID or timed out!"); |
| 32 | + |
| 33 | + linkConfirmation = linkConfirmation.replace("%%USERNAME%%", message.author.username); |
| 34 | + linkConfirmation = linkConfirmation.replace("%%FACTORIONAME%%", factorioName) |
| 35 | + let confirmationMsg = await message.channel.send(linkConfirmation); |
| 36 | + confirmationMsg.react("✅"); |
| 37 | + confirmationMsg.react("🔨"); |
| 38 | + confirmationMsg.react("❌"); |
| 39 | + |
| 40 | + const filter = (reaction, user) => { |
| 41 | + return user.id == message.author.id; |
| 42 | + } |
| 43 | + |
| 44 | + let reactions; |
| 45 | + try { |
| 46 | + reactions = await confirmationMsg.awaitReactions(filter, { max: 1, time: 60 * 1000, errors: ["time"]}); |
| 47 | + } catch (error) { |
| 48 | + if (error.size === 0) return message.channel.send("Timed out!") |
| 49 | + else ErrorManager.Error(error); |
| 50 | + return message.channel.send("Error getting reaction"); |
| 51 | + } |
| 52 | + const reaction = reactions.first(); |
| 53 | + const dat = { factorioName: factorioName, discordID: message.author.id }; |
| 54 | + const found = await searchOneDB("otherData", "linkedPlayers", { |
| 55 | + discordID: message.author.id, |
| 56 | + }); |
| 57 | + |
| 58 | + if (reaction.emoji.name == "❌") |
| 59 | + return message.channel.send("Linking cancelled!"); |
| 60 | + if (found !== null && reaction.emoji.name === "🔨") { |
| 61 | + // re-link user |
| 62 | + let res = await findOneAndReplaceDB( |
| 63 | + "otherData", |
| 64 | + "linkedPlayers", |
| 65 | + found, |
| 66 | + dat |
| 67 | + ); |
| 68 | + if (res.ok != 1) |
| 69 | + return message.channel.send( |
| 70 | + "Please contact devs/admins for re-linking, process failed" |
| 71 | + ); |
| 72 | + //redo statistics |
| 73 | + let prevStats = await searchOneDB("otherData", "globPlayerStats", { |
| 74 | + discordID: found.discordID, |
| 75 | + }); |
| 76 | + let newStats = lodash.cloneDeep(prevStats); |
| 77 | + newStats.factorioName = factorioName; |
| 78 | + res = await findOneAndReplaceDB( |
| 79 | + "otherData", |
| 80 | + "globPlayerStats", |
| 81 | + prevStats, |
| 82 | + newStats |
| 83 | + ); |
| 84 | + if (res.ok != 1) |
| 85 | + return message.channel.send( |
| 86 | + "Please contact devs/admins for re-linking, process failed" |
| 87 | + ); |
| 88 | + message.channel.send("Re-linked succesfully!"); |
| 89 | + giveFactorioRole(factorioName, "Member"); // give the Member role to new players |
| 90 | + } |
| 91 | + if (found !== null && reaction.emoji.name == "✅") return message.channel.send("Already linked!"); |
| 92 | + if (found === null && reaction.emoji.name == "✅") { |
| 93 | + const toInsert = { |
| 94 | + factorioName: `${factorioName}`, |
| 95 | + discordID: `${message.author.id}`, |
| 96 | + } |
| 97 | + try { |
| 98 | + giveFactorioRole(factorioName, "Member"); // give the Member role to new players |
| 99 | + let res = await insertOneDB("otherData", "linkedPlayers", toInsert); |
| 100 | + if (res.result.ok == true) return message.channel.send("Linked successfully!"); |
| 101 | + else return message.channel.send("Didn't insert correctly into database"); |
| 102 | + } catch (error) { |
| 103 | + ErrorManager.Error(error); |
| 104 | + return message.channel.send("Error inserting into database"); |
| 105 | + } |
| 106 | + } |
| 107 | + }, |
| 108 | +}; |
0 commit comments