all repos — stealth-developers @ c48217d1371dc5f4f190db6987bb480b524fff83

apps/bot/src/components/modals/configModal.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
import { MessageFlags } from "discord.js";
import { option } from "@purrkit/router";

import db, { eq, getGuildByDiscordId, guilds } from "@stealth-developers/db";
import { kitten } from "@/client";
import { errorMessage } from "@/lib";

export const configTextModal = kitten.modal("config-text", {
	options: {
		key: option.string({ required: true }),
	},
	run: async (interaction, { key }) => {
		const value = interaction.fields.getTextInputValue("text-value");

		if (!interaction.guildId)
			return errorMessage(interaction, "This interaction can only be used in a server.");

		const guild = await getGuildByDiscordId(interaction.guildId);
		if (!guild)
			return errorMessage(interaction, "Failed to update configuration: Guild not found.");

		const [res] = await db
			.update(guilds)
			.set({ [key]: value })
			.where(eq(guilds.id, guild.id))
			.returning();

		if (!res) return errorMessage(interaction, "Failed to update configuration.");

		return interaction.reply({
			content: "Configuration updated successfully!",
			flags: [MessageFlags.Ephemeral],
		});
	},
});