Skip to content

Commit 4587f76

Browse files
authored
Merge pull request #76 from oof2win2/master
New linking support
2 parents d78fdd8 + 4f4fedb commit 4587f76

File tree

11 files changed

+375
-32
lines changed

11 files changed

+375
-32
lines changed

commands/factorio/linkme.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const Discord = require("discord.js");
2+
const { searchOneDB, getFactorioRoles } = 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: "getfactorioroles",
11+
aliases: [],
12+
usage: "<mention/Factorio name>",
13+
category: "moderator",
14+
description: "Gives a user a Factorio role",
15+
accessableby: "Moderator",
16+
},
17+
run: async (client, message, args) => {
18+
let authRoles = message.member.roles.cache;
19+
if (
20+
!authRoles.some((r) => ["Admin", "Moderator", "dev"].includes(r.name))
21+
) {
22+
// if user is not Admin/Moderator/dev
23+
return message.channel.send(
24+
"You don't have enough priviliges to run this command!"
25+
);
26+
}
27+
if (!args[0] && !message.mentions.users.first())
28+
return message.channel.send("Give a Factorio name/Discord ping!")
29+
let user;
30+
if (message.mentions.users.first()) {
31+
let res = await searchOneDB("otherData", "linkedPlayers", {
32+
discordID: (message.mentions.users.first()).id,
33+
})
34+
if (res == undefined)
35+
return message.channel.send("User not linked!");
36+
user = res.factorioName;
37+
} else {
38+
user = args[0];
39+
}
40+
41+
let rolesEmbed = new Discord.MessageEmbed()
42+
.setTitle("Roles of a Factorio player")
43+
.setDescription("Roles of a Factorio player")
44+
.setColor("GREEN")
45+
.setAuthor(
46+
`${message.guild.me.displayName} Help`,
47+
message.guild.iconURL
48+
)
49+
.setThumbnail(client.user.displayAvatarURL())
50+
.setFooter(
51+
${message.guild.me.displayName} | Developed by DistroByte & oof2win2 | Total Commands: ${client.commands.size}`,
52+
client.user.displayAvatarURL()
53+
);
54+
const roles = (await getFactorioRoles(user)).roles;
55+
rolesEmbed.addField("\u200B", roles.join(", "));
56+
return message.channel.send(rolesEmbed);
57+
},
58+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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: "givefactoriorole",
11+
aliases: [],
12+
usage: "<mention/Factorio name> <role name>",
13+
category: "moderator",
14+
description: "Gives a user a Factorio role",
15+
accessableby: "Moderator",
16+
},
17+
run: async (client, message, args) => {
18+
let authRoles = message.member.roles.cache;
19+
if (
20+
!authRoles.some((r) => ["Admin", "Moderator", "dev"].includes(r.name))
21+
) {
22+
// if user is not Admin/Moderator/dev
23+
return message.channel.send(
24+
"You don't have enough priviliges to run this command!"
25+
);
26+
}
27+
if (!args[0] && !message.mentions.users.first())
28+
return message.channel.send("Give a Factorio name/Discord ping!")
29+
if (!args[1])
30+
return message.channel.send("Give role to assign!")
31+
32+
let user;
33+
if (message.mentions.users.first()) {
34+
let res = await searchOneDB("otherData", "linkedPlayers", {
35+
discordID: (message.mentions.users.first()).id,
36+
})
37+
if (res == undefined)
38+
return message.channel.send("User not linked!");
39+
user = res.factorioName;
40+
} else {
41+
user = args[0];
42+
}
43+
const role = args[1];
44+
let res = await giveFactorioRole(user, role);
45+
if (res == false) return message.channel.send("User already has role!");
46+
if (res.ok == true)
47+
return message.channel.send("Assigned role successfully!");
48+
return message.channel.send("Error adding to database");
49+
},
50+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const Discord = require("discord.js");
2+
const { searchOneDB, removeFactorioRole } = require("../../functions");
3+
const { ErrorManager } = require("../../utils/error-manager");
4+
5+
module.exports = {
6+
config: {
7+
name: "removefactoriorole",
8+
aliases: [],
9+
usage: "<mention/Factorio name> <role name>",
10+
category: "moderator",
11+
description: "Removes Factorio role from a user",
12+
accessableby: "Moderator",
13+
},
14+
run: async (client, message, args) => {
15+
let authRoles = message.member.roles.cache;
16+
if (
17+
!authRoles.some((r) => ["Admin", "Moderator", "dev"].includes(r.name))
18+
) {
19+
// if user is not Admin/Moderator/dev
20+
return message.channel.send(
21+
"You don't have enough priviliges to run this command!"
22+
);
23+
}
24+
if (!args[0] && !message.mentions.users.first())
25+
return message.channel.send("Give a Factorio name/Discord ping!")
26+
if (!args[1])
27+
return message.channel.send("Give role to remove!")
28+
29+
let username;
30+
if (message.mentions.users.first()) {
31+
let res = await searchOneDB("otherData", "linkedPlayers", {
32+
discordID: (message.mentions.users.first()).id,
33+
})
34+
if (res == undefined)
35+
return message.channel.send("User not linked!");
36+
username = res.factorioName;
37+
} else {
38+
username = args[0];
39+
}
40+
const role = args[1];
41+
let res = await removeFactorioRole(username, role);
42+
if (res == false) return message.channel.send("User doesn't have roles!")
43+
if (res.ok == true)
44+
return message.channel.send("Removed role successfully!");
45+
return message.channel.send("Error adding to database");
46+
},
47+
};

config/messages.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"firstJoinMessage": "Heyas! Welcome to the AwF community. Since you are new, you don't have many permissions. For those, please go to the http://awf.yt Discord"
3-
}
2+
"firstJoinMessage": "Heyas! Welcome to the AwF community. Since you are new, you don't have many permissions. For those, please go to the http://awf.yt Discord",
3+
"linkConfirmation": "You have chosen to link your Discord account, %%USERNAME%% with your Factorio account on AwF, %%FACTORIONAME%%. The request will timeout after 1 minute of sending. React with :hammer: to re-link your account. If complications arise, please contact devs/admins (relinking is when switching Factorio username, for switching Discord account contact admins/devs. Changing your Discord username **IS NOT** changing an account, whilst changing your Factorio username **is**)"
4+
}

events/client/ready.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
const { ErrorManager } = require("../../utils/error-manager");
2+
const { clientErrChannelID } = require("../../botconfig.json");
3+
14
module.exports = (client) => {
25
console.log(
36
`${client.user.username} is online: ${new Date().toString().slice(4, 24)}`
@@ -17,4 +20,9 @@ module.exports = (client) => {
1720
),
1821
15000
1922
);
23+
24+
client.channels.fetch(clientErrChannelID)
25+
.then(channel => {
26+
ErrorManager.setJammyErrChannel(channel);
27+
})
2028
};

0 commit comments

Comments
 (0)