import { CommandInteraction, CommandInteractionOptionResolver, TextChannel, SlashCommandBuilder, SlashCommandStringOption, SlashCommandUserOption, SlashCommandIntegerOption, PermissionFlagsBits, } from 'discord.js'; import {Command} from '../../classes/command'; import {logger} from '../../logger' import {getContest, putCode} 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 "code";} get description(){return "Add timestamp to code a problem";} 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{ let problemId = (interaction.options as CIOR).getString('problem'); const estimate = (interaction.options as CIOR).getInteger('estimate') ?? -1; const specifiedUser = (interaction.options as CIOR).getUser('user'); const contestName = interaction.channel.name; const channelId = interaction.channel.id; const contest = await getContest(channelId); const time = new Date(); if(problemId === null){ logger.error('option error'); return; } problemId = problemId.toUpperCase(); if(contest === null){ await interaction.reply({ content: `The contest in this channel didn't start!` }); logger.error(`Contest ${contestName} didn't start`); return; } const user = specifiedUser ?? interaction.user; const sessionId: string = await putCode( user, problemId, channelId, time.valueOf(), estimate*1000*60 + contest.startTime ); const content = `Problem ${problemId} code by ${user.username}, estimate in ${estimate} min. (session id: ${sessionId})`; await interaction.reply({content: logger.log(content)}); }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('problem') .setDescription('The id of the problem.') .setMinLength(1).setMaxLength(1) .setRequired(true)) .addIntegerOption((option: SlashCommandIntegerOption) => option .setName('estimate') .setDescription('The estimate coding time of the problem.') .setRequired(true)) .addUserOption((option: SlashCommandUserOption) => option .setName('user') .setDescription('Who read the problem, default is yourself')) .setDefaultMemberPermissions(PermissionFlagsBits.SendMessages); } }; export const command = new Code();