From 9cbbc4da8c2dae377c99a45d9c6433344db2bc02 Mon Sep 17 00:00:00 2001 From: BoppleOpple Date: Wed, 2 Apr 2025 01:28:43 -0400 Subject: [PATCH] changed message for disabled DMs --- src/discordApp.ts | 67 ++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/src/discordApp.ts b/src/discordApp.ts index 1e35563..61e33d6 100644 --- a/src/discordApp.ts +++ b/src/discordApp.ts @@ -5,13 +5,14 @@ import { Events, Message, } from "discord.js"; -import { clarify, embed, ping, tone, requestAnonymousClarification, mood, toggleBot, inDepthClarification, postemptiveToneAdd, getTones, action } from "./interactions" +import { clarify, embed, ping, tone, requestAnonymousClarification, mood, toggleBot, inDepthClarification, postemptiveToneAdd, getTones, action, toggleDMs } from "./interactions" import { get, ref } from "firebase/database"; import database from "./firebase"; import { cleanupMoods } from "./helpers"; + export async function launchBot(): Promise { - // the client has to declare the features it uses up front so discord.js kno9ws if it can + // the client has to declare the features it uses up front so discord.js knows if it can // ignore some fields and callbacks to save on hosting resources. here are some links to clarify: // discord.js: https://discordjs.guide/popular-topics/intents.html#error-disallowed-intents // discord API: https://discord.com/developers/docs/topics/gateway#list-of-intents @@ -44,43 +45,54 @@ export async function launchBot(): Promise { client.on(Events.MessageCreate, async (message: Message) => { console.log(message.content); - // TODO: add tone analysis here as well }); - - // called when an interaction (e.g. slash command) is called. there are a bunch of different - // interaction types, but we'll see which we need as time goes on. - // TODO: find references for this - // Called when an interaction (e.g., slash command) is triggered + client.on(Events.InteractionCreate, async (interaction) => { - const guildId = interaction.guildId!; - const dbRef = ref(database, `servers/${guildId}/botStatus`); + const userId = interaction.user.id; // Get the user ID for individual DM status + const guildId = interaction.guildId!; // Get the guildId for server-wide bot status try { - // Fetch the bot status from the database - const snapshot = await get(dbRef); - const botStatus = snapshot.exists() ? snapshot.val() : "active"; // Default to 'active' if not set + // Check the user's DM status + const userDMRef = ref(database, `users/${userId}/dmsStatus`); + const userDMSnapshot = await get(userDMRef); + const userDMStatus = userDMSnapshot.exists() ? userDMSnapshot.val() : "enabled"; // Default to 'enabled' if not set + + // If DMs are disabled for the user, ignore the interaction and reply with a message + if (userDMStatus === "disabled") { + if (interaction.isCommand() && interaction.commandName !== "toggledms"){ + return interaction.reply({ + content: "Sorry, DMs are currently disabled for this user.", + flags: 64, // Make it ephemeral + }); + } + } + + // Check the bot status for the server + const botStatusRef = ref(database, `servers/${guildId}/botStatus`); + const botSnapshot = await get(botStatusRef); + const botStatus = botSnapshot.exists() ? botSnapshot.val() : "active"; // Default to 'active' if not set // If the bot is inactive, ignore the interaction and reply with a message if (botStatus === "inactive") { - - if(interaction.isCommand() && interaction.commandName != "togglebot"){ + if (interaction.isCommand() && interaction.commandName !== "togglebot") { return interaction.reply({ content: "Sorry, the bot is currently disabled for this server.", flags: 64, // Make it ephemeral - }); - } + }); + } } - // If the bot is active, process the interaction - if (interaction.isChatInputCommand()) { // slash command + // Process the interaction if DMs are enabled for the user and the bot is active for the server + if (interaction.isChatInputCommand()) { if (interaction.commandName === "ping") await ping(interaction); if (interaction.commandName === "embed") await embed(interaction); if (interaction.commandName === "action") await action(interaction); if (interaction.commandName === "list-tones") await getTones(interaction); if (interaction.commandName === "mood") await mood(interaction); if (interaction.commandName === "togglebot") await toggleBot(interaction); - } else if (interaction.isMessageContextMenuCommand()) { // command from the "apps" menu when clicking on a message + if (interaction.commandName === "toggledms") await toggleDMs(interaction); // Handle toggle DM command + } else if (interaction.isMessageContextMenuCommand()) { if (interaction.commandName === "Tone") await tone(interaction); if (interaction.commandName === "Add Tone") await postemptiveToneAdd(interaction); if (interaction.commandName === "Clarify") await clarify(interaction); @@ -90,13 +102,14 @@ export async function launchBot(): Promise { console.log(interaction); } } catch (error) { - console.error("Error fetching bot status:", error); - if(interaction.isCommand()){ - interaction.reply({ - content: "There was an error while processing your request. Please try again later.", - flags: 64, - }); - }} + console.error("Error processing interaction:", error); + if (interaction.isCommand()) { + interaction.reply({ + content: "There was an error while processing your request. Please try again later.", + flags: 64, // Make it ephemeral + }); + } + } }); // attempt to connect