Skip to content
This repository was archived by the owner on May 12, 2026. It is now read-only.

Commit 936d42c

Browse files
committed
fix(bot): refactor config update logic to use updateConfig function
1 parent 4931b3a commit 936d42c

2 files changed

Lines changed: 21 additions & 24 deletions

File tree

apps/bot/src/config-functions/utils.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import {
1616
TextInputStyle,
1717
} from "discord.js";
1818
import { logger } from "@lib";
19-
import { GuildWithLogs, updateGuildLogs } from "@repo/database";
20-
import { trimString, getCurrentValue, getUpdatePayload } from "@utils";
19+
import { GuildWithLogs } from "@repo/database";
20+
import { trimString, getCurrentValue, updateConfig } from "@utils";
2121
import { TFunction } from "i18next";
2222
import { DbConfigKey } from "@constants";
2323
export async function waitForMessageComponent(
@@ -98,8 +98,7 @@ export async function dynamicChannel(
9898
const newValue = messageComponent.values[0] || null;
9999

100100
// 2. Update Database Safely
101-
const payload = getUpdatePayload(dbKey, newValue);
102-
await updateGuildLogs(messageComponent.guildId, payload);
101+
await updateConfig(messageComponent.guildId, dbKey, newValue);
103102

104103
// 3. Reply
105104
const responseKey = newValue ? "set" : "unset";
@@ -158,8 +157,7 @@ export async function dynamicMessage(
158157
}
159158
}
160159

161-
const payload = getUpdatePayload(dbKey, finalValue);
162-
await updateGuildLogs(messageComponent.guildId, payload);
160+
await updateConfig(messageComponent.guildId, dbKey, finalValue);
163161

164162
await messageComponent.editReply({ content: t(`${dbKey}.set`), components: [] });
165163
}
@@ -204,8 +202,7 @@ export async function dynamicRole(
204202

205203
if (!newValue) {
206204
// Unset
207-
const payload = getUpdatePayload(dbKey, null);
208-
await updateGuildLogs(messageComponent.guildId, payload);
205+
await updateConfig(messageComponent.guildId, dbKey, null);
209206
await messageComponent.editReply({ content: t(`${dbKey}.unset`), components: [] });
210207
} else {
211208
// Hierarchy Check
@@ -220,8 +217,7 @@ export async function dynamicRole(
220217
}
221218

222219
// Set
223-
const payload = getUpdatePayload(dbKey, newValue);
224-
await updateGuildLogs(messageComponent.guildId, payload);
220+
await updateConfig(messageComponent.guildId, dbKey, newValue);
225221

226222
await messageComponent.editReply({
227223
content: t(`${dbKey}.set`, {

apps/bot/src/utils/system/configHelper.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { CONFIG_SCHEMA, DbConfigKey, RelationName } from "@constants";
2-
import { GuildWithLogs } from "@repo/database";
2+
import { GuildWithLogs, prisma, updateGuildLogs } from "@repo/database";
33

4-
// Helper to check if a key is in a specific array (Type Guard)
54
function isKeyInGroup<K extends string>(key: string, group: readonly string[]): key is K {
65
return group.includes(key);
76
}
@@ -27,19 +26,21 @@ export function getCurrentValue(data: GuildWithLogs, key: DbConfigKey): string |
2726
return (nestedData as unknown as Record<string, string | null>)[key] ?? null;
2827
}
2928

30-
export function getUpdatePayload(key: DbConfigKey, value: string | null) {
29+
export async function updateConfig(guildId: string, key: DbConfigKey, value: string | null) {
3130
const relation = getRelation(key);
3231

33-
if (relation === "root") {
34-
return { [key]: value };
32+
switch (relation) {
33+
case "logConfig":
34+
// Use the existing updateGuildLogs function
35+
return updateGuildLogs(guildId, { [key]: value });
36+
case "registerConfig":
37+
case "welcomeConfig":
38+
case "root":
39+
default:
40+
// These fields are on the Guild model
41+
return prisma.guild.update({
42+
where: { id: guildId },
43+
data: { [key]: value },
44+
});
3545
}
36-
37-
return {
38-
[relation]: {
39-
upsert: {
40-
create: { [key]: value },
41-
update: { [key]: value },
42-
},
43-
},
44-
};
4546
}

0 commit comments

Comments
 (0)