Skip to content

Commit

Permalink
feat(random): add random level command (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
wopian authored May 12, 2023
1 parent ac38562 commit 64d47b5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command } from './command.js'
import { about } from './commands/about.js'
import { level } from './commands/level.js'
import { random } from './commands/random.js'
import { rankings } from './commands/rankings.js'
import { recent } from './commands/recent.js'
import { user } from './commands/user.js'
Expand All @@ -9,6 +10,7 @@ import { verify } from './commands/verify.js'
export const commands: Command[] = [
about,
level,
random,
rankings,
recent,
user,
Expand Down
38 changes: 38 additions & 0 deletions src/commands/random.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { getRandomLevels } from '@zeepkist/gtr-api'
import { ApplicationCommandType, CommandInteraction } from 'discord.js'

import { Command } from '../command.js'
import { errorReply } from '../components/errorReply.js'
import { paginatedLevel } from '../components/paginated/paginatedLevel.js'
import { log } from '../utils/log.js'

export const random: Command = {
name: 'random',
description: 'Get a random Zeepkist level',
type: ApplicationCommandType.ChatInput,
ephemeral: false,
run: async (interaction: CommandInteraction) => {
const levels = await getRandomLevels({
Limit: 1
})

if (levels.levels.length === 0) {
return errorReply(
interaction,
'No levels found',
'We could not find any levels.'
)
}

const level = levels.levels[0]

log.info(`Got random level ${level.id} (${level.name})`)

await paginatedLevel({
interaction,
action: 'first',
query: { id: level.id }
})
return
}
}
21 changes: 13 additions & 8 deletions src/components/paginated/paginatedLevel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLevels, getRecords } from '@zeepkist/gtr-api'
import { getLevel, getLevels, getRecords } from '@zeepkist/gtr-api'
import {
ActionRowBuilder,
ButtonBuilder,
Expand Down Expand Up @@ -39,13 +39,17 @@ export const paginatedLevel = async (properties: PaginatedData) => {
)}`,
interaction
)
const { levels } = await getLevels({
Author: data.query?.author,
Name: data.query?.name,
WorkshopId: data.query?.workshopId
})
if (levels.length === 1) {
level = levels[0]
if (data.query?.id) {
level = await getLevel(data.query?.id)
} else {
const { levels } = await getLevels({
Author: data.query?.author,
Name: data.query?.name,
WorkshopId: data.query?.workshopId
})
if (levels.length === 1) {
level = levels[0]
}
}
}

Expand All @@ -58,6 +62,7 @@ export const paginatedLevel = async (properties: PaginatedData) => {
})

log.info('Creating embed', interaction)

const embed = new EmbedBuilder().setTitle(`${level.name}`).setAuthor({
name: level.author
})
Expand Down

0 comments on commit 64d47b5

Please sign in to comment.