From 58bf69234459e8419bb32efc4fe696a153357124 Mon Sep 17 00:00:00 2001 From: bryansturdivant <157143446+bryansturdivant@users.noreply.github.com> Date: Thu, 27 Mar 2025 19:47:34 -0400 Subject: [PATCH 1/3] Buggy but close --- src/discordApp.ts | 67 ++++++++++++++++++++++++----------------- src/interactions.ts | 36 ++++++++++++++++++++++ src/registerCommands.ts | 5 +++ 3 files changed, 81 insertions(+), 27 deletions(-) diff --git a/src/discordApp.ts b/src/discordApp.ts index 1e35563..928c61c 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()){ + return interaction.reply({ + content: "Sorry, DMs are currently disabled for you.", + 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 diff --git a/src/interactions.ts b/src/interactions.ts index 1633653..9db6116 100644 --- a/src/interactions.ts +++ b/src/interactions.ts @@ -556,3 +556,39 @@ export async function toggleBot(interaction: ChatInputCommandInteraction): Promise { + await interaction.deferReply({ flags: MessageFlags.Ephemeral }); + + const userId = interaction.user.id; // Get the user ID + const dbRef = ref(db, `users/${userId}/dmsStatus`); + + try { + // Get the current DMs status from the Realtime Database for the user + const snapshot = await get(dbRef); + let newStatus = "enabled"; // Default to 'enabled' + + if (snapshot.exists() && snapshot.val() === "enabled") { + newStatus = snapshot.val() === "enabled" ? "disabled" : "enabled"; // Toggle the status + } else { + await set(dbRef, "enabled"); + } + + // Log the new status for debugging + console.log(`Toggling DMs status for user ${userId} to: ${newStatus}`); + + // Update the DMs status in the Realtime Database for the user + await set(dbRef, newStatus); + + // Respond to the user with confirmation + interaction.editReply({ + content: `Your DMs have been ${newStatus === "enabled" ? "enabled" : "disabled"}.`, + }); + } catch (error) { + console.error("Error toggling DMs status for user:", error); + interaction.editReply({ + content: "There was an error while toggling your DMs status. Please try again later.", + }); + } +} \ No newline at end of file diff --git a/src/registerCommands.ts b/src/registerCommands.ts index 9949cf3..2a99911 100644 --- a/src/registerCommands.ts +++ b/src/registerCommands.ts @@ -97,5 +97,10 @@ updateCommands([ name: "togglebot", description: "Enable or Disable VibeCheque for this server", type: 1, + }, + { + name:"toggledms", + description: "Enable or Disable DM's from VibeCheque", + type:1 } ]); \ No newline at end of file From ecb90dd4296f8fbafe8a0c71afb2f79507be6549 Mon Sep 17 00:00:00 2001 From: bryansturdivant <157143446+bryansturdivant@users.noreply.github.com> Date: Fri, 28 Mar 2025 16:44:47 -0400 Subject: [PATCH 2/3] working right now --- src/discordApp.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/discordApp.ts b/src/discordApp.ts index 928c61c..a8d8b43 100644 --- a/src/discordApp.ts +++ b/src/discordApp.ts @@ -60,7 +60,7 @@ export async function launchBot(): Promise { // If DMs are disabled for the user, ignore the interaction and reply with a message if (userDMStatus === "disabled") { - if (interaction.isCommand()){ + if (interaction.isCommand() && interaction.commandName !== "toggledms"){ return interaction.reply({ content: "Sorry, DMs are currently disabled for you.", flags: 64, // Make it ephemeral From 3a1a9282840c0c60359c7bd5f1bdf5df42c2910f Mon Sep 17 00:00:00 2001 From: BoppleOpple Date: Wed, 2 Apr 2025 01:23:23 -0400 Subject: [PATCH 3/3] changed the message for disabling dms to be clearer, changed the default state after the first toggle --- res/test-server-configs.json | 2 +- src/interactions.ts | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/res/test-server-configs.json b/res/test-server-configs.json index 2c21e24..7e94d67 100644 --- a/res/test-server-configs.json +++ b/res/test-server-configs.json @@ -1,7 +1,7 @@ { "123456789": { "isEnabled": false, - "lastToggled": "2025-03-23T18:25:56.621Z", + "lastToggled": "2025-04-02T05:09:09.785Z", "toggledBy": "987654321" } } \ No newline at end of file diff --git a/src/interactions.ts b/src/interactions.ts index 9db6116..c21f24c 100644 --- a/src/interactions.ts +++ b/src/interactions.ts @@ -567,12 +567,13 @@ export async function toggleDMs(interaction: ChatInputCommandInteraction