From 8a9e10d8157fa086204490800dcad10ca9e6fafe Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Thu, 2 May 2024 20:55:54 +0300 Subject: [PATCH] Add help command, tweaks to multi-message output --- .eslintrc.json | 3 ++- package.json | 2 +- src/client/DiscordClient.ts | 30 +++++++++++++++++++----- src/client/components/commands/Help.ts | 28 ++++++++++++++++++++++ src/client/components/commands/Search.ts | 14 +++++++---- src/middleware/Controller.ts | 1 + 6 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 src/client/components/commands/Help.ts diff --git a/.eslintrc.json b/.eslintrc.json index 3fd4e62..3eeadc5 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -105,7 +105,8 @@ "CallExpression": {"arguments": "first"}, "ArrayExpression": "first", "ObjectExpression": "first", - "ImportDeclaration": "first" + "ImportDeclaration": "first", + "MemberExpression": 1 } ], "init-declarations": "warn", diff --git a/package.json b/package.json index 95ab54e..82f4d68 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "navys-music-bot", - "version": "1.2.2", + "version": "1.3.0", "description": "A bot that plays music on discord", "main": "build/index.js", "repository": "https://git.corgi.wtf/Navy.gif/music-bot.git", diff --git a/src/client/DiscordClient.ts b/src/client/DiscordClient.ts index 85fab0c..8a2e976 100644 --- a/src/client/DiscordClient.ts +++ b/src/client/DiscordClient.ts @@ -122,24 +122,33 @@ class DiscordClient extends Client this.shutdown(); } - async sendSplitMessage (message: Message, text: string, opts: MessageReplyOptions) + async sendSplitMessage (message: Message, text: string, opts?: MessageReplyOptions, delimiter = ' ') { - const words = text.split(' '); + const segments = text.split(delimiter); const out = []; let tmp = ''; - for (const word of words) + for (const segment of segments) { - if (tmp.length + word.length > 2000) + if (tmp.length + segment.length >= 1900) { + const codeBlock = tmp.match(/```/ug)?.length === 1; + if (codeBlock) + tmp += '```'; out.push(tmp); - tmp = word; + tmp = ''; + if (codeBlock) + tmp += '```'; + tmp += segment; } else { - tmp += ' ' + word; + tmp += delimiter + segment; } } + if (tmp.match(/```/gu)?.length === 1) + tmp += '```'; out.push(tmp); + const [ first, ...rest ] = out; try { @@ -232,6 +241,15 @@ class DiscordClient extends Client return this.#musicPlayer; } + get version () + { + const version = process.env.BOT_VERSION; + const dev = process.env.NODE_ENV === 'development'; + if (!version) + return 'dev'; + return version + (dev ? '-dev' : ''); + } + } process.once('message', (msg: IPCMessage) => diff --git a/src/client/components/commands/Help.ts b/src/client/components/commands/Help.ts new file mode 100644 index 0000000..028699a --- /dev/null +++ b/src/client/components/commands/Help.ts @@ -0,0 +1,28 @@ +import { stripIndents } from 'common-tags'; +import Command from '../../../interfaces/Command.js'; +import DiscordClient from '../../DiscordClient.js'; + +class HelpCommand extends Command +{ + constructor (client: DiscordClient) + { + super(client, { + name: 'help', + description: 'Display a brief help message' + }); + } + + async execute () + { + return stripIndents` + **${this.client.user?.username} v${this.client.version}** + A simple music bot. + Source: + + Use \`${this.client.prefix}list commands\` to list available commands. + Use \`${this.client.prefix} --help\` to display command specific help. + `; + } +} + +export default HelpCommand; \ No newline at end of file diff --git a/src/client/components/commands/Search.ts b/src/client/components/commands/Search.ts index 7ef9e44..3f29852 100644 --- a/src/client/components/commands/Search.ts +++ b/src/client/components/commands/Search.ts @@ -24,7 +24,7 @@ class SearchCommand extends Command }); } - async execute (_message: Message, { map }: CommandArgs) + async execute (message: Message, { map }: CommandArgs) { const [ song ] = map.get('song') ?? []; const [ artist ] = map.get('artist') ?? []; @@ -38,9 +38,15 @@ class SearchCommand extends Command if (!results.length) return 'No results found'; - return ` - **Search results:**\n\`\`\`${results.map(result => `\t- [id: ${result.id}] ${result.title} - ${result.artist} (Album: ${result.album ?? 'Unknown'}) [${result.year ?? 'Unknown year'}]`).join('\n')}\`\`\` - `; + await this.client.sendSplitMessage( + message, + ` + **Search results:**\n\`\`\`${results.map(result => `\t- [id: ${result.id}] ${result.title} - ${result.artist} (Album: ${result.album ?? 'Unknown'}) [${result.year ?? 'Unknown year'}]`).join('\n')}\`\`\` + `, + { allowedMentions: { repliedUser: false } }, + '\n' + ); + return null; } } diff --git a/src/middleware/Controller.ts b/src/middleware/Controller.ts index 5a74c7a..0df807f 100644 --- a/src/middleware/Controller.ts +++ b/src/middleware/Controller.ts @@ -32,6 +32,7 @@ class Controller // this.#options = options; const { shardList, totalShards, execArgv, respawn, debug } = Controller.parseShardOptions(options.shardOptions); + process.env.BOT_VERSION = version; options.discord.rootDir = options.rootDir; options.discord.logger = options.logger;