-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorg'd commands, moved ending week to a command-based process rather…
… than an automated one
- Loading branch information
1 parent
a93c3b0
commit 9136f33
Showing
10 changed files
with
144 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,72 @@ | ||
import { SlashCommandBuilder, SlashCommandSubcommandGroupBuilder } from 'discord.js'; | ||
import endSeason from './subcommands/endSeason.js'; | ||
import extend from './subcommands/extend.js'; | ||
import type { SubCommand } from './subcommands/index.js'; | ||
import leaderboard from './subcommands/leaderboard.js'; | ||
import startSeason from './subcommands/start/season.js'; | ||
import startWeek from './subcommands/start/week.js'; | ||
import endSeason from './subcommands/season/end.js'; | ||
import startSeason from './subcommands/season/start.js'; | ||
import endWeek from './subcommands/week/end.js'; | ||
import extend from './subcommands/week/extend.js'; | ||
import startWeek from './subcommands/week/start.js'; | ||
import type { Command } from './index.js'; | ||
|
||
const seasonCommands = new Map<string, SubCommand>([ | ||
[startSeason.name, startSeason], | ||
[endSeason.name, endSeason], | ||
]); | ||
|
||
const weekCommands = new Map<string, SubCommand>([ | ||
[startWeek.name, startWeek], | ||
[endWeek.name, endWeek], | ||
[extend.name, extend], | ||
]); | ||
|
||
const miscCommands = new Map<string, SubCommand>([[leaderboard.name, leaderboard]]); | ||
|
||
export default { | ||
data: new SlashCommandBuilder() | ||
.setName('rmfp') | ||
.setDescription('Perform various RMFP-related tasks.') | ||
.addSubcommandGroup( | ||
new SlashCommandSubcommandGroupBuilder() | ||
.setName('start') | ||
.setDescription('Start a new RMFP session') | ||
.setName('season') | ||
.setDescription('Controls an RMFP season.') | ||
.addSubcommand(startSeason.subCommandOption) | ||
.addSubcommand(startWeek.subCommandOption), | ||
.addSubcommand(endSeason.subCommandOption), | ||
) | ||
.addSubcommandGroup( | ||
new SlashCommandSubcommandGroupBuilder() | ||
.setName('week') | ||
.setDescription('Controls an RMFP week.') | ||
.addSubcommand(startWeek.subCommandOption) | ||
.addSubcommand(extend.subCommandOption) | ||
.addSubcommand(endWeek.subCommandOption), | ||
) | ||
.addSubcommand(endSeason.subCommandOption) | ||
.addSubcommand(extend.subCommandOption) | ||
.addSubcommand(leaderboard.subCommandOption) | ||
.toJSON(), | ||
async execute(interaction) { | ||
if (!interaction.isChatInputCommand()) { | ||
return; | ||
} | ||
|
||
switch (interaction.options.getSubcommand()) { | ||
case startSeason.name: | ||
await startSeason.execute(interaction); | ||
break; | ||
case startWeek.name: | ||
await startWeek.execute(interaction); | ||
break; | ||
case endSeason.name: | ||
await endSeason.execute(interaction); | ||
break; | ||
case leaderboard.name: | ||
await leaderboard.execute(interaction); | ||
break; | ||
case extend.name: | ||
await extend.execute(interaction); | ||
break; | ||
default: | ||
break; | ||
if (interaction.options.getSubcommandGroup() === null) { | ||
const subCommand = miscCommands.get(interaction.options.getSubcommand()); | ||
await subCommand?.execute(interaction); | ||
return; | ||
} | ||
|
||
let subCommand: SubCommand | undefined; | ||
if (interaction.options.getSubcommandGroup() === 'season') { | ||
subCommand = seasonCommands.get(interaction.options.getSubcommand()); | ||
} else { | ||
subCommand = weekCommands.get(interaction.options.getSubcommand()); | ||
} | ||
|
||
if (subCommand === undefined) { | ||
console.error( | ||
`Could not find a subcommand matching for "${interaction.options.getSubcommandGroup()} ${interaction.options.getSubcommand()}"`, | ||
); | ||
return; | ||
} | ||
|
||
await subCommand.execute(interaction); | ||
}, | ||
} satisfies Command; |
10 changes: 5 additions & 5 deletions
10
src/commands/subcommands/endSeason.ts → src/commands/subcommands/season/end.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import process from 'node:process'; | ||
import type { Week } from '@prisma/client'; | ||
import type { Client } from 'discord.js'; | ||
import { isRMFPOwner } from '../../../common/isRMFPOwner.js'; | ||
import { prisma } from '../../../common/prisma.js'; | ||
import type { SubCommand } from '../index.js'; | ||
|
||
const closeRMFPWeek = async (week: Week, client: Client) => { | ||
const guild = await client.guilds.fetch(process.env.GUILD_ID!); | ||
const rmfpOwnerRole = guild.roles.cache.get(process.env.RMFP_OWNER_ROLE_ID!)!; | ||
if (rmfpOwnerRole === null) { | ||
console.error(`[Close RMFP Week] No RMFP owner role was detected!`); | ||
return; | ||
} | ||
|
||
const rmfpOwners = rmfpOwnerRole.members.values(); | ||
|
||
console.log(`[Close RMFP Week] RMFP Owners found: ${rmfpOwnerRole.members.size}`); | ||
|
||
const winners = await prisma.week.winners(week.number); | ||
const content = [ | ||
`The winner(s) of RMFP S${week.seasonNumber}W${week.number} are:`, | ||
...winners.map((winner, idx) => `${idx + 1}. <@${winner.userId}>'s [message](${winner.messageUrl})`), | ||
].join('\n'); | ||
|
||
for (const owner of rmfpOwners) { | ||
console.log(`Dispatching announcement message to: ${owner.user.username}`); | ||
await owner.send(content); | ||
} | ||
|
||
console.log('[Close RMFP Week] Marking week as ended...'); | ||
await prisma.week.update({ | ||
where: { | ||
id: week.id, | ||
}, | ||
data: { | ||
ended: true, | ||
}, | ||
}); | ||
console.log('[Close RMFP Week] Week has been ended.'); | ||
}; | ||
|
||
/** | ||
* Starts a new RMFP week with the provided theme. Informs the user that they must end an ongoing week, if there is one. | ||
*/ | ||
export default { | ||
subCommandOption: (subCommand) => subCommand.setName('end').setDescription('Ends the active week of RMFP.'), | ||
name: 'end', | ||
async execute(interaction) { | ||
if (!isRMFPOwner(interaction.guild, interaction.member)) { | ||
await interaction.reply({ | ||
content: 'Only the owner of RMFP may end the week.', | ||
ephemeral: true, | ||
}); | ||
return; | ||
} | ||
|
||
const currentSeason = await prisma.season.current(); | ||
|
||
if (currentSeason === null) { | ||
await interaction.reply({ | ||
content: | ||
"There's no RMFP season ongoing. You need to start a season (`/rmfp start season`) before running this command.", | ||
ephemeral: true, | ||
}); | ||
return; | ||
} | ||
|
||
const currentWeek = await prisma.week.current(); | ||
if (currentWeek === null) { | ||
await interaction.reply({ | ||
content: "There's no RMFP week ongoing. This command will do nothing.", | ||
ephemeral: true, | ||
}); | ||
return; | ||
} | ||
|
||
await interaction.reply({ content: 'Ending the current week!', ephemeral: true }); | ||
await closeRMFPWeek(currentWeek, interaction.client); | ||
}, | ||
} satisfies SubCommand; |
6 changes: 3 additions & 3 deletions
6
src/commands/subcommands/extend.ts → src/commands/subcommands/week/extend.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters