import { CommandInteraction, CommandInteractionOptionResolver, TextChannel, SlashCommandBuilder, SlashCommandStringOption, SlashCommandIntegerOption, } from 'discord.js'; import {Command} from '../../classes/command'; import {logger} from '../../logger' import {getContest, updateSession} from '../../functions/database'; function isTextChannel(data: unknown): data is TextChannel{ return (data as TextChannel).name !== undefined; } type CIOR = CommandInteractionOptionResolver; class Code extends Command{ get name(){return "modify";} get description(){return "Modify a session timestamp.";} async execute(interaction: CommandInteraction): Promise{ if(!isTextChannel(interaction.channel)){ await interaction.reply({ content: `Channel name doesn't exist!` }); logger.error(`Channel name doesn't exist`); return; } try{ // parse const sessionId = (interaction.options as CIOR).getString('session'); const key = (interaction.options as CIOR).getString('key'); const channelId = interaction.channel.id; const contest = await getContest(channelId); let time = (interaction.options as CIOR).getInteger('time'); if(sessionId === null || key === null || contest === null){ logger.error(`Can't parse parameters in "modify"`); throw Error(); } if(time === null) time = new Date().getTime(); else time = time*1000*60 + contest.startTime; // update if (! await updateSession(sessionId, key, time)){ await interaction.reply({ content: `Session ${sessionId} doesn't exist` }); return; } await interaction.reply({ content: `The value of key ${key} in session ${sessionId} has been updated.` }); logger.log(`The value of key ${key} in session ${sessionId} has been updated.`); }catch(error: unknown){ logger.error(`Error occur while coding problem`); return; } } override build(): SlashCommandBuilder | Omit{ return new SlashCommandBuilder() .setName(this.name) .setDescription(this.description) .addStringOption((option: SlashCommandStringOption) => option .setName('session') .setDescription('The id of the session.') .setRequired(true) ) .addStringOption((option: SlashCommandStringOption) => option .setName('key') .setDescription('Which value should be modified.') .setRequired(true) .addChoices( {name: 'start', value: 'start'}, {name: 'end', value: 'end'}, ) ) .addIntegerOption((option: SlashCommandIntegerOption) => option .setName('time') .setDescription('The time which being updated, default is now.') ) } }; export const command = new Code();