Skip to content

Commit

Permalink
Merge pull request #78 from oof2win2/feature/tests
Browse files Browse the repository at this point in the history
Release 1.0!
  • Loading branch information
DistroByte authored Feb 10, 2021
2 parents bb1bbd9 + d544e5e commit 2c680cd
Show file tree
Hide file tree
Showing 52 changed files with 28,551 additions and 5,852 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ log-all
.DS_Store
servers.json
/commands/owner/updateip.js
test.js
testing.js
temp/*
24 changes: 3 additions & 21 deletions chatFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
const functions = require("./functions");
const { filterBan } = require("./filterBan");
const servers = require("./servers.json");
const { RconConnectionManager } = require("./utils/rcon-connection")
const { RconConnectionManager } = require("./utils/rcon-connection");
const { DatabaseConnection } = require("./utils/database-manager");

module.exports = function chatFormat(line, channel, client, serverConsoleName) {
const helpdesk = client.channels.cache.get("590241134740111387");
Expand Down Expand Up @@ -100,26 +101,7 @@ module.exports = function chatFormat(line, channel, client, serverConsoleName) {
}
//join
if (line.includes("[JOIN]")) {
// check if a player is linked to factorio. if not, tell them to get linked
const username = functions.formatChatData(line).slice(2).split(" ")[0];
functions
.searchOneDB("otherData", "linkedPlayers", { factorioName: username })
.then((out) => {
if (out == null) {
Object.keys(servers).forEach((server) => {
if (
servers[server].discordChannelID ==
client.channels.cache.get(channel).id
) {
RconConnectionManager.rconCommand(
`/w ${username} Welcome to AwF. You can join the Discord server on awf.yt and link yourself to Discord with \`!linkme <discordUsername>\`\n`,
servers[server].name
);
}
});
}
})
.catch((err) => console.log(err));
// removed, as this is processed in functions.js
}
} else if (line.includes("JLOGGER:")) {
line = line.slice(line.indexOf("JLOGGER:") + "JLOGGER:".length + 1);
Expand Down
61 changes: 61 additions & 0 deletions commands/dev/shellcmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const child_process = require("child_process");
const Str = require("@supercharge/strings");
const fs = require('fs');
const { ErrorManager } = require("../../utils/error-manager");

module.exports = {
config: {
name: "shellcmd",
aliases: [],
usage: "<shell command>",
category: "dev",
description: "Execute a shell command",
accessableby: "Dev",
},
run: async (client, message, args) => {
let authRoles = message.member.roles.cache;
if (
!authRoles.some((r) => ["Admin", "dev"].includes(r.name))
) {
// if user is not Admin/Moderator/dev
return message.channel.send(
"You don't have enough priviliges to run this command!"
);
}
if (!args[0]) return message.channel.send('No command!');
const initialCmd = args.shift();
let outputData = [];
let child = child_process.spawn(initialCmd, args);
child.stdout.on("data", (data) => {
outputData.push(`${data.toString().slice(0, -1)}`);
});
child.stderr.on("data", (data) => {
outputData.push(`${data.toString().slice(0, -1)}\n`);
});
child.on("close", async (code) => {
let msg = outputData.join("\n");
let outFilePath = `${process.env.PWD}/temp/${Str.random()}.out`;
fs.writeFileSync(outFilePath, msg);
try {
await message.channel.send(`Process exited with code ${code}. Output is as an attatchment.`, { files: [outFilePath] });
} catch (error) {
message.channel.send(`Error: ${error}`);
ErrorManager.Error(error);
}
fs.rmSync(outFilePath);
});
setTimeout(() => {
if (child.exitCode === null) {
child.kill();
// console.log("Killed after 60s!");
message.channel.send(`Command killed after 5s`);
setTimeout(() => {
if (child.exitCode === null) {
child.kill()
message.channel.send(`Command killed after another 500ms with force`);
}
}, 500);
}
}, 5000);
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports = {
name: "spamfactorioserver",
aliases: [],
usage: "<server ping> <number of messages> [message to send]",
category: "testing",
category: "dev",
description: "Spam a Factorio server with lots of chat stuff",
accessableby: "Admin",
accessableby: "Dev",
},
run: async (client, message, args) => {
let authRoles = message.member.roles.cache;
Expand Down
13 changes: 7 additions & 6 deletions commands/factorio/fstatsserver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Discord = require("discord.js");
const { searchOneDB, getServerFromChannelInput } = require("../../functions");
const { getServerFromChannelInput } = require("../../functions");
const { DatabaseConnection } = require("../../utils/database-manager");
const { RconConnectionManager } = require("../../utils/rcon-connection");

module.exports = {
Expand All @@ -14,13 +15,13 @@ module.exports = {
run: async (client, message, args) => {
async function getPlayer(server, message, alternative) {
const alternativeDB = async(server, alternative) => {
return searchOneDB(server, "deaths", { player: alternative });
return DatabaseConnection.findOneDB(server, "deaths", { player: alternative });
}
if (message.mentions.users.first()) {
let linkedPlayer = await searchOneDB("otherData", "linkedPlayers", { discordID: message.mentions.users.first().id });
let linkedPlayer = await DatabaseConnection.findOneDB("otherData", "linkedPlayers", { discordID: message.mentions.users.first().id });
// if the player is linked, it is possible to get their factorio name
if (linkedPlayer !== null)
return await searchOneDB(server, "deaths", { player: linkedPlayer.factorioName });
return await DatabaseConnection.findOneDB(server, "deaths", { player: linkedPlayer.factorioName });
else
return null;
} else {
Expand Down Expand Up @@ -52,13 +53,13 @@ module.exports = {
${message.guild.me.displayName} | Developed by DistroByte & oof2win2 | Total Commands: ${client.commands.size}`,
client.user.displayAvatarURL()
);
let rockets = await searchOneDB(server, "stats", {
let rockets = await DatabaseConnection.findOneDB(server, "stats", {
rocketLaunches: { $exists: true },
});
if (rockets == null) rockets = 0;
else rockets = rockets.rocketLaunches;
statsEmbed.addField("Rockets launched", rockets);
let research = await searchOneDB(server, "stats", {
let research = await DatabaseConnection.findOneDB(server, "stats", {
research: "researchData",
});
if (research == null) research = {};
Expand Down
8 changes: 4 additions & 4 deletions commands/factorio/linkedplayers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Discord = require("discord.js");
const { searchOneDB } = require("../../functions");
const { DatabaseConnection } = require("../../utils/database-manager");

module.exports = {
config: {
Expand All @@ -13,7 +13,7 @@ module.exports = {
run: async (client, message, args) => {
if (!args[0]) {
// no argument at all
let res = await searchOneDB("otherData", "linkedPlayers", {
let res = await DatabaseConnection.findOneDB("otherData", "linkedPlayers", {
factorioName: message.author.username,
});
if (res == null) return message.channel.send("You are not linked yet");
Expand All @@ -22,7 +22,7 @@ module.exports = {
if (!args[1]) {
// if the server name is provided but no 2nd argument, searches for generic server data
if (message.mentions.users.first()) {
let res = await searchOneDB("otherData", "linkedPlayers", {
let res = await DatabaseConnection.findOneDB("otherData", "linkedPlayers", {
discordID: message.mentions.members.first().id,
});
if (res == null)
Expand All @@ -38,7 +38,7 @@ module.exports = {
}\` is already linked under username ${res.factorioName}!`
);
} else {
let res = await searchOneDB("otherData", "linkedPlayers", {
let res = await DatabaseConnection.findOneDB("otherData", "linkedPlayers", {
factorioName: args[0],
});
if (res == null)
Expand Down
15 changes: 8 additions & 7 deletions commands/factorio/linkme.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const Discord = require("discord.js");
const { searchOneDB, giveFactorioRole, insertOneDB, findOneAndReplaceDB } = require("../../functions");
const { giveFactorioRole } = require("../../functions");
const { LinkingCache } = require("../../functions");
const { ErrorManager } = require("../../utils/error-manager");
const lodash = require("lodash");
let { linkConfirmation } = require("../../config/messages.json")
let { linkConfirmation } = require("../../config/messages.json");
const { DatabaseConnection } = require("../../utils/database-manager");

module.exports = {
config: {
Expand Down Expand Up @@ -51,15 +52,15 @@ module.exports = {
}
const reaction = reactions.first();
const dat = { factorioName: factorioName, discordID: message.author.id };
const found = await searchOneDB("otherData", "linkedPlayers", {
const found = await DatabaseConnection.findOneDB("otherData", "linkedPlayers", {
discordID: message.author.id,
});

if (reaction.emoji.name == "❌")
return message.channel.send("Linking cancelled!");
if (found !== null && reaction.emoji.name === "🔨") {
// re-link user
let res = await findOneAndReplaceDB(
let res = await DatabaseConnection.findOneAndReplaceDB(
"otherData",
"linkedPlayers",
found,
Expand All @@ -70,12 +71,12 @@ module.exports = {
"Please contact devs/admins for re-linking, process failed"
);
//redo statistics
let prevStats = await searchOneDB("otherData", "globPlayerStats", {
let prevStats = await DatabaseConnection.findOneDB("otherData", "globPlayerStats", {
discordID: found.discordID,
});
let newStats = lodash.cloneDeep(prevStats);
newStats.factorioName = factorioName;
res = await findOneAndReplaceDB(
res = await DatabaseConnection.findOneAndReplaceDB(
"otherData",
"globPlayerStats",
prevStats,
Expand All @@ -96,7 +97,7 @@ module.exports = {
}
try {
giveFactorioRole(factorioName, "Member"); // give the Member role to new players
let res = await insertOneDB("otherData", "linkedPlayers", toInsert);
let res = await DatabaseConnection.insertOneDB("otherData", "linkedPlayers", toInsert);
if (res.result.ok == true) return message.channel.send("Linked successfully!");
else return message.channel.send("Didn't insert correctly into database");
} catch (error) {
Expand Down
5 changes: 2 additions & 3 deletions commands/factorio/statsg.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const Discord = require("discord.js");
const { searchOneDB } = require("../../functions");

const { DatabaseConnection } = require("../../utils/database-manager");
module.exports = {
config: {
name: "statsg",
Expand All @@ -25,7 +24,7 @@ module.exports = {
user = await server.members.fetch({ query: args[0], limit: 1 });
user = user.first().user;
}
let data = await searchOneDB("otherData", "globPlayerStats", {
let data = await DatabaseConnection.findOneDB("otherData", "globPlayerStats", {
discordID: user.id,
});
if (data == null)
Expand Down
45 changes: 25 additions & 20 deletions commands/moderator/getfactorioroles.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const Discord = require("discord.js");
const { searchOneDB, getFactorioRoles } = require("../../functions");
const { getFactorioRoles } = require("../../functions");
const { LinkingCache } = require("../../functions");
const { ErrorManager } = require("../../utils/error-manager");
const lodash = require("lodash");
let { linkConfirmation } = require("../../config/messages.json")
let { linkConfirmation } = require("../../config/messages.json");
const { DatabaseConnection } = require("../../utils/database-manager");

module.exports = {
config: {
Expand All @@ -28,31 +29,35 @@ module.exports = {
return message.channel.send("Give a Factorio name/Discord ping!")
let user;
if (message.mentions.users.first()) {
let res = await searchOneDB("otherData", "linkedPlayers", {
let res = await DatabaseConnection.findOneDB("otherData", "linkedPlayers", {
discordID: (message.mentions.users.first()).id,
})
if (res == undefined)
return message.channel.send("User not linked!");
if (res === undefined || res === null) return message.channel.send("User not linked, therefore has no roles!");
user = res.factorioName;
} else {
user = args[0];
let res = await DatabaseConnection.findOneDB("otherData", "linkedPlayers", {
factorioName: args[0],
})
if (res === undefined || res === null) return message.channel.send("User not linked, therefore has no roles!");
user = res.factorioName;
}

let rolesEmbed = new Discord.MessageEmbed()
.setTitle("Roles of a Factorio player")
.setDescription("Roles of a Factorio player")
.setColor("GREEN")
.setAuthor(
`${message.guild.me.displayName} Help`,
message.guild.iconURL
)
.setThumbnail(client.user.displayAvatarURL())
.setFooter(
${message.guild.me.displayName} | Developed by DistroByte & oof2win2 | Total Commands: ${client.commands.size}`,
client.user.displayAvatarURL()
);
const roles = (await getFactorioRoles(user)).roles;
rolesEmbed.addField("\u200B", roles.join(", "));
.setTitle("Roles of a Factorio player")
.setDescription("Roles of a Factorio player")
.setColor("GREEN")
.setAuthor(
`${message.guild.me.displayName} Help`,
message.guild.iconURL
)
.setThumbnail(client.user.displayAvatarURL())
.setFooter(
${message.guild.me.displayName} | Developed by DistroByte & oof2win2 | Total Commands: ${client.commands.size}`,
client.user.displayAvatarURL()
);
const roles = (await getFactorioRoles(user));
if (roles === null || roles === undefined) return message.channel.send('No roles!');
rolesEmbed.addField("\u200B", roles.roles.join(", "));
return message.channel.send(rolesEmbed);
},
};
13 changes: 8 additions & 5 deletions commands/moderator/givefactoriorole.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const Discord = require("discord.js");
const { searchOneDB, giveFactorioRole, insertOneDB, findOneAndReplaceDB } = require("../../functions");
const { giveFactorioRole } = require("../../functions");
const { LinkingCache } = require("../../functions");
const { ErrorManager } = require("../../utils/error-manager");
const lodash = require("lodash");
let { linkConfirmation } = require("../../config/messages.json")
let { linkConfirmation } = require("../../config/messages.json");
const { DatabaseConnection } = require("../../utils/database-manager");

module.exports = {
config: {
Expand Down Expand Up @@ -31,7 +32,7 @@ module.exports = {

let user;
if (message.mentions.users.first()) {
let res = await searchOneDB("otherData", "linkedPlayers", {
let res = await DatabaseConnection.findOneDB("otherData", "linkedPlayers", {
discordID: (message.mentions.users.first()).id,
})
if (res == undefined)
Expand All @@ -40,10 +41,12 @@ module.exports = {
} else {
user = args[0];
}
const role = args[1];
args.shift();
const role = args.join(' ');
let res = await giveFactorioRole(user, role);
console.log(res);
if (res == false) return message.channel.send("User already has role!");
if (res.ok == true)
if (res.ok === true || res.result.ok === 1)
return message.channel.send("Assigned role successfully!");
return message.channel.send("Error adding to database");
},
Expand Down
6 changes: 4 additions & 2 deletions commands/moderator/linkuser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { searchOneDB, insertOneDB } = require("../../functions");
const { DatabaseConnection } = require("../../utils/database-manager");

module.exports = {
config: {
Expand All @@ -19,9 +19,11 @@ module.exports = {
"You don't have enough priviliges to run this command!"
);
}
if (!args[0]) return message.channel.send("No Discord identifier!");
if (!args[1]) return message.channel.send("No Factorio identifier!");
const discordID = message.mentions.users.first() ? (message.mentions.users.first()).id : args[0];
args.shift();
let db = await searchOneDB("otherData", "linkedPlayers", { discordID: discordID });
let db = await DatabaseConnection.findOneDB("otherData", "linkedPlayers", { discordID: discordID });
if (db !== null) return message.channel.send("User is already linked!");
const factorioName = args.join(" ");
const toWrite = {
Expand Down
Loading

0 comments on commit 2c680cd

Please sign in to comment.