feat: handle blocking users
Node.js CI / Lint and Test (push) Successful in 54s

This commit is contained in:
2025-09-27 17:30:28 -07:00
parent 64d5d4a9b5
commit bb90584693
3 changed files with 83 additions and 3 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ veluna.discord.on(Events.InteractionCreate, (interaction) => {
return; return;
} }
if (interaction.isButton()) { if (interaction.isButton()) {
void handleButton(interaction); void handleButton(veluna, interaction);
} }
if (interaction.isModalSubmit()) { if (interaction.isModalSubmit()) {
void handleModalSubmit(veluna, interaction); void handleModalSubmit(veluna, interaction);
+68 -2
View File
@@ -4,18 +4,26 @@
* @author Naomi Carrigan * @author Naomi Carrigan
*/ */
import {
MessageFlags,
PermissionFlagsBits,
type ButtonInteraction,
} from "discord.js";
import { answer } from "../modals/answer.js"; import { answer } from "../modals/answer.js";
import { ask } from "../modals/ask.js"; import { ask } from "../modals/ask.js";
import type { ButtonInteraction } from "discord.js"; import type { Veluna } from "../interfaces/veluna.js";
/** /**
* Displays the appropriate modal when a button is pressed. * Displays the appropriate modal when a button is pressed.
* @param veluna - Veluna's instance.
* @param interaction - The button interaction payload from Discord. * @param interaction - The button interaction payload from Discord.
*/ */
// eslint-disable-next-line max-lines-per-function , max-statements -- Big boi function.
export const handleButton = async( export const handleButton = async(
veluna: Veluna,
interaction: ButtonInteraction, interaction: ButtonInteraction,
): Promise<void> => { ): Promise<void> => {
const { customId } = interaction; const { customId, message } = interaction;
if (customId === "ask") { if (customId === "ask") {
await interaction.showModal(ask); await interaction.showModal(ask);
@@ -24,4 +32,62 @@ export const handleButton = async(
if (customId === "answer") { if (customId === "answer") {
await interaction.showModal(answer); await interaction.showModal(answer);
} }
if (customId.startsWith("block-")) {
await interaction.deferReply({
flags: MessageFlags.Ephemeral,
});
if (!interaction.inCachedGuild()) {
await interaction.editReply({
content: "This interaction can only be used in a server.",
});
return;
}
const { member, guild } = interaction;
if (!member.permissions.has(PermissionFlagsBits.ManageGuild)) {
await interaction.editReply({
content: "You do not have permission to use this interaction.",
});
return;
}
const userId = customId.replace("block-", "");
const record = await veluna.db.servers.findUnique({
where: {
serverId: guild.id,
},
});
if (!record) {
await interaction.editReply({
content:
// eslint-disable-next-line stylistic/max-len -- Big boi string.
"This server is not registered in the database. Please configure your settings.",
});
return;
}
if (record.blockedUsers.includes(userId)) {
await interaction.editReply({
content: "This user is already blocked.",
});
await message.delete();
return;
}
await veluna.db.servers.update({
data: {
blockedUsers: {
push: userId,
},
},
where: {
serverId: guild.id,
},
});
await interaction.editReply({
content: "User blocked successfully.",
});
await message.delete();
}
}; };
+14
View File
@@ -47,6 +47,12 @@ export const handleModalSubmit = async(
}); });
return; return;
} }
if (record.blockedUsers.includes(interaction.user.id)) {
await interaction.editReply({
content: "You are blocked from asking questions in this server.",
});
return;
}
const question = interaction.fields.getTextInputValue("textinput"); const question = interaction.fields.getTextInputValue("textinput");
const channel const channel
= veluna.discord.channels.cache.get(record.questionChannelId) = veluna.discord.channels.cache.get(record.questionChannelId)
@@ -71,6 +77,13 @@ export const handleModalSubmit = async(
style: 3, style: 3,
type: 2, type: 2,
}, },
{
// eslint-disable-next-line @typescript-eslint/naming-convention -- Discord API.
custom_id: `block-${interaction.user.id}`,
label: "Block user",
style: 4,
type: 2,
},
], ],
type: 1, type: 1,
}, },
@@ -159,5 +172,6 @@ export const handleModalSubmit = async(
}, },
], ],
}); });
await message.delete();
} }
}; };