import discord, { CommandInteraction, CommandInteractionOptionResolver, SlashCommandBuilder, SlashCommandSubcommandBuilder, SlashCommandStringOption, SlashCommandChannelOption, InteractionResponse, SlashCommandSubcommandsOnlyBuilder, } from 'discord.js'; import {HydratedDocument} from 'mongoose'; import {Command} from '../../classes/command'; import {logger} from '../../logger'; import {Guild, guildModel} from '../../models/Guild'; type CIOR = CommandInteractionOptionResolver; async function log(interaction: CommandInteraction): Promise{ const feature: string | null = (interaction.options as CIOR).getString('feature'); if(!feature) throw Error('feature not exist'); const logChannel = (interaction.options as CIOR).getChannel('channel'); if(!logChannel || !('send' in logChannel)) throw Error('logChannel not exist or illegal'); if(!interaction.guild || !interaction.guild.id || !interaction.guild.name) throw Error('guild not exist'); let guild: HydratedDocument | null = null; switch(feature){ case 'giveaway': await guildModel.findOneAndUpdate( {id: interaction.guild.id}, {$set: { name: interaction.guild.name, giveawayLogChannelId: logChannel.id }}, {new: true, upsert: true} ); break; case 'autorole': await guildModel.findOneAndUpdate( {id: interaction.guild.id}, {$set: { name: interaction.guild.name, autoroleLogChannelId: logChannel.id }}, {new: true, upsert: true} ); break; } await interaction.reply({ content: logger.log(`The log channel of ${feature} has been set to ${logChannel}`) }); ; } class SetVariables extends Command{ get name(){return "set";} get description(){return "Set guild variables.";} async execute(interaction: CommandInteraction): Promise{ try{ const subcommand: string = (interaction.options as CIOR).getSubcommand(); switch(subcommand){ case 'log': await log(interaction); break; default: throw Error('subcommand not exist'); } }catch(err: unknown){ let message; if(err instanceof Error) message = err.message; else message = String(message); logger.error(`While executing "/set", ${message}`); await interaction.reply({content: `While executing "/set", ${message}`}); } } override build(): SlashCommandSubcommandsOnlyBuilder{ return new SlashCommandBuilder() .setName(this.name) .setDescription(this.description) .addSubcommand((subcommand: SlashCommandSubcommandBuilder) => subcommand .setName('log') .setDescription('Set log of features.') .addStringOption((option: SlashCommandStringOption) => option .setName('feature') .setDescription('Specify the to be set feature.') .setRequired(true) .addChoices( {name: 'giveaway', value: 'giveaway'}, {name: 'autorole', value: 'autorole'}, ) ) .addChannelOption((option: SlashCommandChannelOption) => option .setName('channel') .setDescription('The channel that log send to.') .setRequired(true) ) ) } }; export const command = new SetVariables();