Files
mafuyu-kirisu/commands/contests/read.ts
konchin 2d7361e937
Some checks failed
release-tag / release-image (push) Failing after 1m14s
initial commit
2024-10-11 19:49:58 +08:00

90 lines
3.1 KiB
TypeScript

import {
CommandInteraction,
CommandInteractionOptionResolver,
TextChannel,
SlashCommandBuilder,
SlashCommandStringOption,
SlashCommandUserOption,
SlashCommandIntegerOption,
} from 'discord.js';
import {Command} from '../../classes/command';
import {logger} from '../../logger'
import {getContest, putRead} from '../../functions/database';
function isTextChannel(data: unknown): data is TextChannel{
return (data as TextChannel).name !== undefined;
}
type CIOR = CommandInteractionOptionResolver;
class Read extends Command{
get name(){return "read";}
get description(){return "Add timestamp to read a problem";}
async execute(interaction: CommandInteraction): Promise<void>{
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('Options error in "read".');
return;
}
const user = specifiedUser ?? interaction.user;
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 sessionId: string = await putRead(
user, problemId, channelId, time.valueOf(), estimate*1000*60 + contest.startTime
);
let content = `Problem ${problemId} read by ${user.username}`;
if(estimate !== -1) content += `, estimate in ${estimate} min`;
content += `. (session id: ${sessionId})`;
await interaction.reply({content: logger.log(content)});
}catch(error: unknown){
logger.error(`Error occur while reading problem`);
return;
}
}
override build(): SlashCommandBuilder |
Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup">{
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.')
)
.addUserOption((option: SlashCommandUserOption) =>
option
.setName('user')
.setDescription('Who read the problem, default is yourself')
)
}
};
export const command = new Read();