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

91 lines
2.9 KiB
TypeScript

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<void>{
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<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup">{
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();