Skip to content

Commit 55d1bbf

Browse files
committed
feat: auto update roles every x horus
1 parent ea011b5 commit 55d1bbf

File tree

5 files changed

+44
-22
lines changed

5 files changed

+44
-22
lines changed

src/discord/commands/forceUpdateCommand.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = {
2323
}
2424
],
2525

26-
execute: async (interaction) => {
26+
execute: async (interaction, extra = { everyone: false, hidden: false }) => {
2727
const linkedData = fs.readFileSync("data/linked.json");
2828
if (!linkedData) {
2929
throw new HypixelDiscordChatBridgeError("The linked data file does not exist. Please contact an administrator.");
@@ -34,8 +34,8 @@ module.exports = {
3434
throw new HypixelDiscordChatBridgeError("The linked data file is malformed. Please contact an administrator.");
3535
}
3636

37-
const user = interaction.options.getUser("user");
38-
const everyone = interaction.options.getBoolean("everyone");
37+
const user = interaction?.options?.getUser("user");
38+
const everyone = extra.everyone ?? interaction?.options?.getBoolean("everyone");
3939
if (!user && !everyone) {
4040
throw new HypixelDiscordChatBridgeError("You must specify a user or everyone.");
4141
}
@@ -45,12 +45,15 @@ module.exports = {
4545
}
4646

4747
if (user) {
48-
await updateRolesCommand.execute(interaction, { discordId: user.id });
48+
await updateRolesCommand.execute(interaction, { discordId: user.id, hidden: extra.hidden });
4949
}
5050

51-
const discordIds = Object.values(linked);
52-
for (const discordId of discordIds) {
53-
await updateRolesCommand.execute(interaction, { discordId });
51+
if (everyone) {
52+
const discordIds = Object.values(linked);
53+
for (const discordId of discordIds) {
54+
await updateRolesCommand.execute(interaction, { discordId, hidden: extra.hidden });
55+
console.log(`Updated roles for ${discordId}`);
56+
}
5457
}
5558
}
5659
};

src/discord/commands/updateCommand.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const { getNetworth } = require("skyhelper-networth");
1515
const config = require("../../../config.json");
1616
const fs = require("fs");
1717
const { getUsername } = require("../../contracts/API/mowojangAPI.js");
18+
const { hideLinkEmbed } = require("discord.js");
1819

1920
async function updateRoles({ discordId, uuid }) {
2021
const member = await guild.members.fetch(discordId);
@@ -310,7 +311,7 @@ module.exports = {
310311
verificationCommand: true,
311312
description: "Update your current roles",
312313

313-
execute: async (interaction, extra = { discordId: null }) => {
314+
execute: async (interaction, extra = { discordId: null, hidden: false }) => {
314315
try {
315316
const linkedData = fs.readFileSync("data/linked.json");
316317
if (!linkedData) {
@@ -330,22 +331,26 @@ module.exports = {
330331

331332
await updateRoles({ discordId, uuid });
332333

333-
const updateRole = new SuccessEmbed(
334-
`Successfully synced ${extra.discordId ? `<@${extra.discordId}>` : "your"} roles with \`${await getUsername(uuid)}\`'s stats!`
335-
).setFooter({
336-
text: `by @.kathund | /help [command] for more information`,
337-
iconURL: "https://i.imgur.com/uUuZx2E.png"
338-
});
334+
if (!extra.hidden) {
335+
const updateRole = new SuccessEmbed(
336+
`Successfully synced ${extra.discordId ? `<@${extra.discordId}>` : "your"} roles with \`${await getUsername(uuid)}\`'s stats!`
337+
).setFooter({
338+
text: `by @.kathund | /help [command] for more information`,
339+
iconURL: "https://i.imgur.com/uUuZx2E.png"
340+
});
339341

340-
await interaction.followUp({ embeds: [updateRole], ephemeral: true });
342+
await interaction.followUp({ embeds: [updateRole], ephemeral: true });
343+
}
341344
} catch (error) {
342345
console.log(error);
343-
const errorEmbed = new ErrorEmbed(`\`\`\`${error}\`\`\``).setFooter({
344-
text: `by @.kathund | /help [command] for more information`,
345-
iconURL: "https://i.imgur.com/uUuZx2E.png"
346-
});
346+
if (!extra.hidden) {
347+
const errorEmbed = new ErrorEmbed(`\`\`\`${error}\`\`\``).setFooter({
348+
text: `by @.kathund | /help [command] for more information`,
349+
iconURL: "https://i.imgur.com/uUuZx2E.png"
350+
});
347351

348-
await interaction.editReply({ embeds: [errorEmbed], ephemeral: true });
352+
await interaction.editReply({ embeds: [errorEmbed], ephemeral: true });
353+
}
349354
}
350355
},
351356

src/discord/handlers/StateHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class StateHandler {
1919
return console.error(`Channel "Guild" not found!`);
2020
}
2121

22-
if (config.verification.autoUpdater) require("../other/updateUsers.js");
22+
if (config.verification.autoRoleUpdater.enabled) require("../other/updateUsers.js");
2323
if (config.statsChannels.enabled) require("../other/statsChannels.js");
2424

2525
channel.send({

src/discord/other/statsChannels.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ const cron = require("node-cron");
44

55
if (config.statsChannels.enabled) {
66
cron.schedule(`*/${config.statsChannels.autoUpdaterInterval} * * * *`, () => updateChannels.execute(null, { hidden: true }));
7-
console.log("Stats channels enabled");
7+
console.discord(`StatsChannels ready, executing every ${config.statsChannels.autoUpdaterInterval} minutes.`);
88
}

src/discord/other/updateUsers.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const updateRolesCommand = require("../commands/forceUpdateCommand.js");
2+
const config = require("../../../config.json");
3+
const cron = require("node-cron");
4+
5+
if (config.verification.autoRoleUpdater.enabled) {
6+
console.discord(`RoleSync ready, executing every ${config.verification.autoRoleUpdater.interval} hours.`);
7+
cron.schedule(`0 */${config.verification.autoRoleUpdater.interval} * * *`, async () => {
8+
try {
9+
await updateRolesCommand.execute(null, { everyone: true, hidden: true });
10+
} catch (error) {
11+
console.error(error);
12+
}
13+
});
14+
}

0 commit comments

Comments
 (0)