Add help command, tweaks to multi-message output

This commit is contained in:
Erik 2024-05-02 20:55:54 +03:00
parent 1ef645c27d
commit 8a9e10d815
6 changed files with 66 additions and 12 deletions

View File

@ -105,7 +105,8 @@
"CallExpression": {"arguments": "first"}, "CallExpression": {"arguments": "first"},
"ArrayExpression": "first", "ArrayExpression": "first",
"ObjectExpression": "first", "ObjectExpression": "first",
"ImportDeclaration": "first" "ImportDeclaration": "first",
"MemberExpression": 1
} }
], ],
"init-declarations": "warn", "init-declarations": "warn",

View File

@ -1,6 +1,6 @@
{ {
"name": "navys-music-bot", "name": "navys-music-bot",
"version": "1.2.2", "version": "1.3.0",
"description": "A bot that plays music on discord", "description": "A bot that plays music on discord",
"main": "build/index.js", "main": "build/index.js",
"repository": "https://git.corgi.wtf/Navy.gif/music-bot.git", "repository": "https://git.corgi.wtf/Navy.gif/music-bot.git",

View File

@ -122,24 +122,33 @@ class DiscordClient extends Client
this.shutdown(); 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 = []; const out = [];
let tmp = ''; 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); out.push(tmp);
tmp = word; tmp = '';
if (codeBlock)
tmp += '```';
tmp += segment;
} }
else else
{ {
tmp += ' ' + word; tmp += delimiter + segment;
} }
} }
if (tmp.match(/```/gu)?.length === 1)
tmp += '```';
out.push(tmp); out.push(tmp);
const [ first, ...rest ] = out; const [ first, ...rest ] = out;
try try
{ {
@ -232,6 +241,15 @@ class DiscordClient extends Client
return this.#musicPlayer; 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) => process.once('message', (msg: IPCMessage) =>

View File

@ -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: <https://git.corgi.wtf/Navy.gif/music-bot>
Use \`${this.client.prefix}list commands\` to list available commands.
Use \`${this.client.prefix}<command> --help\` to display command specific help.
`;
}
}
export default HelpCommand;

View File

@ -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 [ song ] = map.get('song') ?? [];
const [ artist ] = map.get('artist') ?? []; const [ artist ] = map.get('artist') ?? [];
@ -38,9 +38,15 @@ class SearchCommand extends Command
if (!results.length) if (!results.length)
return 'No results found'; return 'No results found';
return ` 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')}\`\`\` **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;
} }
} }

View File

@ -32,6 +32,7 @@ class Controller
// this.#options = options; // this.#options = options;
const { shardList, totalShards, execArgv, respawn, debug } = Controller.parseShardOptions(options.shardOptions); const { shardList, totalShards, execArgv, respawn, debug } = Controller.parseShardOptions(options.shardOptions);
process.env.BOT_VERSION = version;
options.discord.rootDir = options.rootDir; options.discord.rootDir = options.rootDir;
options.discord.logger = options.logger; options.discord.logger = options.logger;