Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion res/test-server-configs.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"123456789": {
"isEnabled": false,
"lastToggled": "2025-03-23T18:25:56.621Z",
"lastToggled": "2025-04-02T05:09:09.785Z",
"toggledBy": "987654321"
}
}
67 changes: 40 additions & 27 deletions src/discordApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Client> {
// 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
Expand Down Expand Up @@ -44,43 +45,54 @@ export async function launchBot(): Promise<Client> {

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 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);
Expand All @@ -90,13 +102,14 @@ export async function launchBot(): Promise<Client> {
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
Expand Down
37 changes: 37 additions & 0 deletions src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,40 @@ export async function toggleBot(interaction: ChatInputCommandInteraction<CacheTy
});
}
}


export async function toggleDMs(interaction: ChatInputCommandInteraction<CacheType>): Promise<void> {
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);
// Default the *new* status to 'disabled' since the default *old status* is 'enabled'
let newStatus = "disabled";

if (snapshot.exists() && snapshot.val() === "enabled") {
newStatus = snapshot.val() === "enabled" ? "disabled" : "enabled"; // Toggle the status
} else {
await set(dbRef, "disabled");
}

// 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.",
});
}
}
5 changes: 5 additions & 0 deletions src/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]);