From b8d537df76ae8e5db8997e7ba90057d77f5b3360 Mon Sep 17 00:00:00 2001 From: David Malchin Date: Sat, 19 Oct 2024 02:47:23 +0300 Subject: [PATCH] Add `playerlist` command --- package-lock.json | 13 ++ package.json | 1 + server/components/loader.ts | 2 + server/components/standalone/index.ts | 8 ++ .../standalone/playerlist/action-row.ts | 114 ++++++++++++++++++ .../standalone/playerlist/command.ts | 27 +++++ .../components/standalone/playerlist/embed.ts | 49 ++++++++ 7 files changed, 214 insertions(+) create mode 100644 server/components/standalone/index.ts create mode 100644 server/components/standalone/playerlist/action-row.ts create mode 100644 server/components/standalone/playerlist/command.ts create mode 100644 server/components/standalone/playerlist/embed.ts diff --git a/package-lock.json b/package-lock.json index 56d8238..56d6976 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "dependencies": { "@discordjs/collection": "1.5.2", "@discordjs/core": "1.0.0", + "@discordjs/formatters": "0.3.1", "@discordjs/rest": "2.0.0", "@discordjs/util": "1.0.0", "@discordjs/ws": "1.0.0" @@ -55,6 +56,18 @@ "node": ">=16.9.0" } }, + "node_modules/@discordjs/formatters": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/@discordjs/formatters/-/formatters-0.3.1.tgz", + "integrity": "sha512-M7X4IGiSeh4znwcRGcs+49B5tBkNDn4k5bmhxJDAUhRxRHTiFAOTVUNQ6yAKySu5jZTnCbSvTYHW3w0rAzV1MA==", + "license": "Apache-2.0", + "dependencies": { + "discord-api-types": "^0.37.41" + }, + "engines": { + "node": ">=16.9.0" + } + }, "node_modules/@discordjs/rest": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/@discordjs/rest/-/rest-2.0.0.tgz", diff --git a/package.json b/package.json index f1089ee..4d717f9 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "dependencies": { "@discordjs/collection": "1.5.2", "@discordjs/core": "1.0.0", + "@discordjs/formatters": "0.3.1", "@discordjs/rest": "2.0.0", "@discordjs/util": "1.0.0", "@discordjs/ws": "1.0.0" diff --git a/server/components/loader.ts b/server/components/loader.ts index 7c5193f..d27cef2 100644 --- a/server/components/loader.ts +++ b/server/components/loader.ts @@ -9,6 +9,7 @@ import { client, gateway, rest, zdiscordBridge } from '../utils/env.js'; import { isStatefulInteraction } from '../utils/stateful.js'; import core from './core/index.js'; import ping from './ping/index.js'; +import standalone from './standalone/index.js'; import { ApplicationCommand, Component, @@ -86,6 +87,7 @@ function loadComponent({ export function loadComponents() { loadComponent(core); loadComponent(ping); + loadComponent(standalone); if (zdiscordBridge) { loadComponent(zdiscord); console.log('zdiscord bridge enabled'); diff --git a/server/components/standalone/index.ts b/server/components/standalone/index.ts new file mode 100644 index 0000000..093834a --- /dev/null +++ b/server/components/standalone/index.ts @@ -0,0 +1,8 @@ +import { Component } from '../types.js'; +import { playerlistButtonBase } from './playerlist/action-row.js'; +import { playerlistCommand } from './playerlist/command.js'; + +export default { + commands: [playerlistCommand], + messageComponents: [playerlistButtonBase], +} satisfies Component; diff --git a/server/components/standalone/playerlist/action-row.ts b/server/components/standalone/playerlist/action-row.ts new file mode 100644 index 0000000..af39363 --- /dev/null +++ b/server/components/standalone/playerlist/action-row.ts @@ -0,0 +1,114 @@ +import { + APIActionRowComponent, + APIButtonComponentWithCustomId, + APIMessageActionRowComponent, + ButtonStyle, + ComponentType, +} from '@discordjs/core'; +import { createStatefulInteraction } from '../../../utils/stateful.js'; +import { Button } from '../../types.js'; +import { playerlistEmbed, playerlistPageCount } from './embed.js'; + +export enum PlayerlistButtonType { + First, + Previous, + Reload, + Next, + Last, +} + +const typeData: Record< + PlayerlistButtonType, + { + emoji: string; + state: (page: number) => string; + } +> = { + [PlayerlistButtonType.First]: { + emoji: '⏪', + state: () => 'first', + }, + [PlayerlistButtonType.Previous]: { + emoji: '⬅️', + state: (page) => `${page - 1}`, + }, + [PlayerlistButtonType.Reload]: { + emoji: '🔃', + state: (page) => `${page}`, + }, + [PlayerlistButtonType.Next]: { + emoji: '➡️', + state: (page) => `${page + 1}`, + }, + [PlayerlistButtonType.Last]: { + emoji: '⏩', + state: () => 'last', + }, +}; + +export const playerlistButtonBase = createStatefulInteraction