Skip to content

Commit

Permalink
Fixed help command + logs deletion #783
Browse files Browse the repository at this point in the history
  • Loading branch information
niqore committed Dec 21, 2021
1 parent ba346b0 commit bfddfbd
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 44 deletions.
3 changes: 2 additions & 1 deletion src/commands/admin/SendLogsCommand.js
Expand Up @@ -34,12 +34,13 @@ const SendLogsCommand = async (message, language, args) => {
let msg = "```";
files.forEach(function(file) {
msg += file + " (" + fs.statSync("logs/" + file).size / 1000.0 + " ko)" + "\n";
if (msg > 1800) {
if (msg.length > 1800) {
message.author.send({ content: msg + "```" });
msg = "```";
}
});
if (msg !== "```") {
console.log(msg);
message.author.send({ content: msg + "```" });
}
});
Expand Down
10 changes: 3 additions & 7 deletions src/commands/player/HelpCommand.js
@@ -1,3 +1,5 @@
import {isOnMainServer} from "../../core/utils/ShardUtils";

module.exports.commandInfo = {
name: "help",
aliases: ["h"]
Expand Down Expand Up @@ -125,13 +127,7 @@ const HelpCommand = async (message, language, args) => {
}

const [entity] = await Entities.getOrRegister(message.author.id);
if (
client.guilds.cache
.get(JsonReader.app.MAIN_SERVER_ID)
.members.cache.find(
(val) =>
val.id === message.author.id) === undefined && entity.Player.dmNotification
) {
if (!await isOnMainServer(entity.discordUserId) && entity.Player.dmNotification) {
await sendDirectMessage(message.author, JsonReader.commands.help.getTranslation(language).mp.title,
JsonReader.commands.help.getTranslation(language).mp.description, JsonReader.bot.embed.default, language);
}
Expand Down
70 changes: 36 additions & 34 deletions src/core/bot/DraftBot.ts
Expand Up @@ -261,42 +261,44 @@ export class DraftBot {
const now = Date.now();
const originalConsoleLog = console.log;

/* Create log folder and remove old logs (> 7 days) */
if (!fs.existsSync("logs")) {
fs.mkdirSync("logs");
}
else {
fs.readdir("logs", function(err, files) {
if (err) {
return;
}
files.forEach(function(file) {
const parts = file.split("-");
if (parts.length === 5) {
if (
now -
new Date(
parseInt(parts[1]),
parseInt(parts[2]) - 1,
parseInt(parts[3])
).getTime() >
7 * 24 * 60 * 60 * 1000
) {
// 7 days
fs.unlink("logs/" + file, function(err: Error) {
if (err !== undefined && err !== null) {
originalConsoleError(
"Error while deleting logs/" +
file +
": " +
err
);
}
});
}
if (this.isMainShard) {
/* Create log folder and remove old logs (> 7 days) */
if (!fs.existsSync("logs")) {
fs.mkdirSync("logs");
}
else {
fs.readdir("logs", function(err, files) {
if (err) {
return;
}
files.forEach(function(file) {
const parts = file.split("-");
if (parts.length >= 5) {
if (
now -
new Date(
parseInt(parts[1]),
parseInt(parts[2]) - 1,
parseInt(parts[3])
).getTime() >
7 * 24 * 60 * 60 * 1000
) {
// 7 days
fs.unlink("logs/" + file, function(err: Error) {
if (err !== undefined && err !== null) {
originalConsoleError(
"Error while deleting logs/" +
file +
": " +
err
);
}
});
}
}
});
});
});
}
}

this.updateGlobalLogsFile(new Date());
Expand Down
19 changes: 19 additions & 0 deletions src/core/utils/ShardUtils.ts
@@ -0,0 +1,19 @@
import {botConfig, draftBotClient} from "../bot";

export const isOnMainServer = async function(discordId: string): Promise<boolean> {
const response = (await draftBotClient.shard.broadcastEval((client, context) => {
const mainServer = client.guilds.cache.get(context.mainServerId);
if (mainServer) {
return mainServer.members.cache.find(
(val) =>
val.id === context.discordId) === undefined;
}
return false;
}, {
context: {
discordId,
mainServerId: botConfig.MAIN_SERVER_ID
}
}));
return response.includes(true);
};
6 changes: 4 additions & 2 deletions src/index.ts
Expand Up @@ -6,9 +6,11 @@ process.on("unhandledRejection", function(err: Error) {
// process.exit(1);
});

const shardCount = "auto";

const main = function() {
const shardingManager = new ShardingManager("./dist/src/core/bot/index.js", {
totalShards: "auto",
totalShards: shardCount,
// Needed as in auto mode it has to make a request to know the needed number of shards
token: loadConfig().DISCORD_CLIENT_TOKEN
});
Expand All @@ -19,7 +21,7 @@ const main = function() {
});
});
shardingManager.spawn({
amount: "auto"
amount: shardCount
}).then();
};

Expand Down

0 comments on commit bfddfbd

Please sign in to comment.