Skip to content

Commit 1ab65c9

Browse files
committed
fix(inactivity): way that verified users are handled
1 parent 332156d commit 1ab65c9

File tree

4 files changed

+70
-13
lines changed

4 files changed

+70
-13
lines changed

config.example.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@
117117
},
118118
"inactivity": {
119119
"enabled": false,
120-
"channel": "INACTIVITY_LOG_CHANNEL"
120+
"channel": "INACTIVITY_LOG_CHANNEL",
121+
"maxInactivityTime": 30
121122
}
122123
},
123124
"other": {

src/discord/commands/gexpCheckCommand.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = {
2929
throw new HypixelDiscordChatBridgeError("The linked data file does not exist. Please contact an administrator.");
3030
}
3131

32-
const linked = JSON.parse(linkedData);
32+
const linked = JSON.parse(linkedData.toString());
3333
if (!linked) {
3434
throw new HypixelDiscordChatBridgeError("The linked data file is malformed. Please contact an administrator.");
3535
}
@@ -65,13 +65,13 @@ module.exports = {
6565
await interaction.editReply({ embeds: [progressEmbed] });
6666

6767
const username = await getUsername(member.uuid);
68-
const linkedUser = Object.keys(linked).find((key) => linked[key] === member.uuid);
68+
const linkedUser = Object.keys(linked).find((value) => value === member.uuid);
6969
if (linkedUser === undefined) {
7070
skippedString += `${username} » User not verified | ${member.weeklyExperience.toLocaleString()}\n`;
7171
continue;
7272
}
7373

74-
const inactive = Object.keys(inactivity).find((key) => inactivity[key].uuid === member.uuid);
74+
const inactive = Object.keys(inactivity).find((value) => value === member.uuid);
7575
if (inactive) {
7676
inactiveString += `${username} » Member Inactive | ${inactivity[inactive].reason}`;
7777
continue;

src/discord/commands/inactivityCommand.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@ async function handleInactivitySubmit(interaction) {
3232
throw new HypixelDiscordChatBridgeError("The linked data file does not exist. Please contact an administrator.");
3333
}
3434

35-
const linked = JSON.parse(linkedData);
35+
const linked = JSON.parse(linkedData.toString());
3636
if (!linked) {
3737
throw new HypixelDiscordChatBridgeError("The linked data file is malformed. Please contact an administrator.");
3838
}
3939

40+
const uuid = Object.entries(linked).find(([, value]) => value === interaction.user.id)?.[0];
41+
if (!uuid) {
42+
throw new HypixelDiscordChatBridgeError("You are not linked to a Minecraft account.");
43+
}
44+
const username = await getUsername(uuid);
45+
4046
const inactivityData = readFileSync("data/inactivity.json");
4147
if (!inactivityData) {
4248
throw new HypixelDiscordChatBridgeError("The inactivity data file does not exist. Please contact an administrator.");
@@ -47,22 +53,26 @@ async function handleInactivitySubmit(interaction) {
4753
throw new HypixelDiscordChatBridgeError("The inactivity data file is malformed. Please contact an administrator.");
4854
}
4955

50-
if (inactivity[interaction.user.id]) {
56+
if (inactivity[uuid]) {
5157
const embed = new Embed()
5258
.setTitle("Inactivity Failed")
53-
.setDescription(`You are already inactive until <t:${inactivity[interaction.user.id].expire}:F> (<t:${inactivity[interaction.user.id].expire}:R>)`)
59+
.setDescription(`You are already inactive until <t:${inactivity[uuid].expire}:F> (<t:${inactivity[uuid].expire}:R>)`)
5460
.setFooter({
5561
text: `by @.kathund | /help [command] for more information`,
5662
iconURL: "https://i.imgur.com/uUuZx2E.png"
5763
});
58-
return await interaction.reply({ embeds: [embed] });
64+
return await interaction.followUp({ embeds: [embed] });
5965
}
6066

61-
const uuid = linked[interaction.user.id];
62-
const username = await getUsername(uuid);
63-
6467
const time = Math.floor(ms(interaction.fields.getTextInputValue("inactivityTime")) / 1000);
6568
if (isNaN(time)) throw new HypixelDiscordChatBridgeError("Please input a valid time");
69+
70+
if (time > config.verification.inactivity.maxInactivityTime * 86400) {
71+
throw new HypixelDiscordChatBridgeError(
72+
`You can only request inactivity for ${config.verification.inactivity.maxInactivityTime} day(s) or less. Please contact an administrator if you need more time.`
73+
);
74+
}
75+
6676
const reason = interaction.fields.getTextInputValue("inactivityReason") || "None";
6777
const expire = Math.floor(Date.now() / 1000) + time;
6878
const date = Math.floor(new Date().getTime() / 1000);
@@ -83,7 +93,7 @@ async function handleInactivitySubmit(interaction) {
8393
throw new HypixelDiscordChatBridgeError("Inactivity channel not found. Please contact an administrator.");
8494
}
8595

86-
inactivity[interaction.user.id] = { uuid, reason, expire };
96+
inactivity[uuid] = { id: interaction.user.id, reason, expire };
8797
writeFileSync("data/inactivity.json", JSON.stringify(inactivity, null, 2));
8898
await channel.send({ embeds: [inactivityEmbed] });
8999
const inactivityResponse = new SuccessEmbed(`Inactivity request has been successfully sent to the guild staff.`).setFooter({
@@ -98,6 +108,7 @@ module.exports = {
98108
description: "Send an inactivity notice to the guild staff",
99109
inactivityCommand: true,
100110
guildOnly: true,
111+
linkedOnly: true,
101112
verifiedOnly: true,
102113
handleInactivitySubmit,
103114
removeExpiredInactivity,

src/discord/events/interactionCreate.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { handleInactivitySubmit } = require("../commands/inactivityCommand.js");
44
// eslint-disable-next-line no-unused-vars
55
const { CommandInteraction } = require("discord.js");
66
const config = require("../../../config.json");
7+
const { readFileSync } = require("fs");
78

89
module.exports = {
910
name: "interactionCreate",
@@ -37,10 +38,18 @@ module.exports = {
3738
throw new HypixelDiscordChatBridgeError("You don't have permission to use this command.");
3839
}
3940

41+
if (command.verifiedOnly === true && isVerifiedMember(interaction) === false) {
42+
throw new HypixelDiscordChatBridgeError("You don't have permission to use this command.");
43+
}
44+
4045
if (command.guildOnly === true && isGuildMember(interaction) === false) {
4146
throw new HypixelDiscordChatBridgeError("You don't have permission to use this command.");
4247
}
4348

49+
if (command.linkedOnly === true && isLinkedMember(interaction) === false) {
50+
throw new HypixelDiscordChatBridgeError("You are not linked to a Minecraft account.");
51+
}
52+
4453
if (command.requiresBot === true && isBotOnline() === false) {
4554
throw new HypixelDiscordChatBridgeError("Bot doesn't seem to be connected to Hypixel. Please try again.");
4655
}
@@ -112,7 +121,43 @@ function isGuildMember(interaction) {
112121
const user = interaction.member;
113122
const userRoles = user.roles.cache.map((role) => role.id);
114123

115-
if (config.discord.commands.checkPerms === true && !(userRoles.includes(config.verification.guildMemberRole) || config.discord.commands.users.includes(user.id))) {
124+
if (
125+
config.discord.commands.checkPerms === true &&
126+
!(userRoles.includes(config.verification.roles.guildMember.roleId) || config.discord.commands.users.includes(user.id))
127+
) {
128+
return false;
129+
}
130+
131+
return true;
132+
}
133+
134+
function isVerifiedMember(interaction) {
135+
const user = interaction.member;
136+
const userRoles = user.roles.cache.map((role) => role.id);
137+
138+
if (
139+
config.discord.commands.checkPerms === true &&
140+
!(userRoles.includes(config.verification.roles.verified.roleId) || config.discord.commands.users.includes(user.id))
141+
) {
142+
return false;
143+
}
144+
145+
return true;
146+
}
147+
148+
function isLinkedMember(interaction) {
149+
const linkedData = readFileSync("data/linked.json");
150+
if (!linkedData) {
151+
throw new HypixelDiscordChatBridgeError("The linked data file does not exist. Please contact an administrator.");
152+
}
153+
154+
const linked = JSON.parse(linkedData.toString());
155+
if (!linked) {
156+
throw new HypixelDiscordChatBridgeError("The linked data file is malformed. Please contact an administrator.");
157+
}
158+
159+
const uuid = Object.entries(linked).find(([, value]) => value === interaction.user.id)?.[0];
160+
if (!uuid) {
116161
return false;
117162
}
118163

0 commit comments

Comments
 (0)