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
7 changes: 0 additions & 7 deletions res/server-configs.json

This file was deleted.

7 changes: 0 additions & 7 deletions res/test-server-configs.json

This file was deleted.

8 changes: 8 additions & 0 deletions src/__mocks__/botSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const botSettings = jest.requireActual<typeof import("../botSettings")>("../botSettings");
const mockBotSettings = jest.createMockFromModule<typeof import("../botSettings")>("../botSettings");

mockBotSettings.getUserSetting = jest.fn().mockResolvedValue("enabled");
mockBotSettings.getServerSetting = jest.fn().mockResolvedValue("active");
mockBotSettings.toggleServerSetting = jest.fn().mockResolvedValue("inactive");

module.exports = mockBotSettings;
19 changes: 19 additions & 0 deletions src/serverSetting.ts → src/botSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ export async function getServerSetting(guildId: string): Promise<string> {
}
}

// Get the current setting for the server (whether the bot is enabled or disabled)
export async function getUserSetting(userId: string): Promise<string> {
const dbRef = ref(database, `users/${userId}/dmsStatus`);

try {
const snapshot = await get(dbRef);
if (snapshot.exists()) {
return snapshot.val();
} else {
// Default to "enabled" if no setting is found
await set(dbRef, "enabled");
return "enabled";
}
} catch (error) {
console.error("Error getting user setting:", error);
throw error;
}
}

// Toggle the bot's enabled/disabled setting for the server
export async function toggleServerSetting(guildId: string): Promise<string> {
const currentSetting = await getServerSetting(guildId);
Expand Down
22 changes: 2 additions & 20 deletions src/discordApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import {
Message,
} from "discord.js";
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";
import { getServerSetting, getUserSetting } from "./botSettings";


export async function launchBot(): Promise<Client> {
Expand Down Expand Up @@ -53,25 +52,8 @@ export async function launchBot(): Promise<Client> {
const guildId = interaction.guildId!; // Get the guildId for server-wide bot status

try {
// 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
const botStatus = await getServerSetting(guildId); // Default to 'active' if not set

// If the bot is inactive, ignore the interaction and reply with a message
if (botStatus === "inactive") {
Expand Down
2 changes: 2 additions & 0 deletions src/interactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import { MockDiscord } from "./testing/mocks/mockDiscord";
import * as gptRequests from "./gptRequests";
import * as firebase from "firebase/database";
import * as helpers from "./helpers";
import * as botSettings from "./botSettings";
jest.mock("./gptRequests")
jest.mock("discord.js");
jest.mock("firebase/database");
jest.mock("./helpers");
jest.mock("./botSettings");

describe("Testing application commands", ()=>{
beforeAll(()=>{
Expand Down
18 changes: 15 additions & 3 deletions src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { addRoleToDatabase, MINIMUM_MOOD_LIFESPAN, removeRoleIfUnused } from "./
//getTones and Clarify rely on toneJSON. Implementing it in firebase would be better
//import tonesData from "./tones.json" assert { type: "json"};
import { readFile } from 'fs/promises';
import { getUserSetting } from "./botSettings";

//console.log(tonesData.tones);

Expand Down Expand Up @@ -473,6 +474,17 @@ export async function mood(interaction: ChatInputCommandInteraction<CacheType>):
export async function requestAnonymousClarification(interaction: MessageContextMenuCommandInteraction<CacheType>): Promise<void>{
await interaction.deferReply({ephemeral: true});

// Check the user's DM status
const userDMStatus = await getUserSetting(interaction.user.id);

// If DMs are disabled for the user, ignore the interaction and reply with a message
if (userDMStatus === "disabled") {
interaction.editReply({
content: "Sorry, DMs are currently disabled for this user."
});
return;
}

try {
const targetMessage = interaction.targetMessage;

Expand Down Expand Up @@ -570,8 +582,8 @@ export async function toggleDMs(interaction: ChatInputCommandInteraction<CacheTy
// 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
if (snapshot.exists() && snapshot.val() === "disabled") {
newStatus = snapshot.val() === "disabled" ? "enabled" : "disabled"; // Toggle the status
} else {
await set(dbRef, "disabled");
}
Expand All @@ -584,7 +596,7 @@ export async function toggleDMs(interaction: ChatInputCommandInteraction<CacheTy

// Respond to the user with confirmation
interaction.editReply({
content: `Your DMs have been ${newStatus === "enabled" ? "enabled" : "disabled"}.`,
content: `Your DMs have been ${newStatus}.`,
});
} catch (error) {
console.error("Error toggling DMs status for user:", error);
Expand Down
70 changes: 0 additions & 70 deletions src/serverConfigManager.test.ts

This file was deleted.

99 changes: 0 additions & 99 deletions src/serverConfigManager.ts

This file was deleted.