odin/src/commands/guilds/enablefeature.ts
2024-12-27 00:45:23 +01:00

48 lines
1.9 KiB
TypeScript

import { SlashCommandBuilder, ChatInputCommandInteraction, PermissionFlagsBits } from "npm:discord.js";
import { connectToDb, getGuild, setFeature, addGuild } from "../../libs/mysql.ts";
import { errorEmbed, successEmbed } from "../../libs/discord.ts";
export default {
data: new SlashCommandBuilder()
.setName("enablefeature")
.setDescription("Enable a feature of the bot.")
.setDMPermission(false)
.addStringOption(option =>
option.setName("feature")
.setDescription("The feature to be enabled.")
.setRequired(true)
.addChoices([
{name: "Welcome message", value: "welcome_message"},
{name: "Goodbye message", value: "bye_message"},
])),
async execute(interaction: ChatInputCommandInteraction) {
await interaction.deferReply()
const connection = await connectToDb();
const guild_id = interaction.guild?.id ? interaction.guild?.id : ""
const guild = await getGuild(connection, guild_id)
if (!guild[0]) {
await addGuild(connection, guild_id);
}
const member = interaction.guild?.members.cache.get(interaction.user.id)
if (!member?.permissions.has(PermissionFlagsBits.Administrator) && (guild[0].admin_role_id && !member?.roles.cache.has(guild[0]?.admin_role_id))) {
const embed = errorEmbed("You are not allowed to use that command.", interaction.client.user.displayAvatarURL());
return await interaction.editReply({embeds: [embed]});
}
const feature: string | null = interaction.options.getString("feature")
await setFeature(connection, guild_id, feature ? feature : "", "true");
connection.end()
const embed = successEmbed("The feature has been successfully enabled.", interaction.client.user.displayAvatarURL())
interaction.editReply({embeds: [embed]})
},
};