Merge branch 'codetheweb:master' into master

This commit is contained in:
Oliver Traber 2023-10-02 22:55:47 +02:00 committed by GitHub
commit 4dad1d9609
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 234 additions and 51 deletions

View file

@ -1,10 +1,11 @@
import {SlashCommandBuilder} from '@discordjs/builders';
import {AutocompleteInteraction, ChatInputCommandInteraction, EmbedBuilder} from 'discord.js';
import {APIEmbedField, AutocompleteInteraction, ChatInputCommandInteraction} from 'discord.js';
import {inject, injectable} from 'inversify';
import Command from '.';
import AddQueryToQueue from '../services/add-query-to-queue.js';
import {TYPES} from '../types.js';
import {prisma} from '../utils/db.js';
import {Pagination} from 'pagination.djs';
@injectable()
export default class implements Command {
@ -54,7 +55,8 @@ export default class implements Command {
),
);
constructor(@inject(TYPES.Services.AddQueryToQueue) private readonly addQueryToQueue: AddQueryToQueue) {}
constructor(@inject(TYPES.Services.AddQueryToQueue) private readonly addQueryToQueue: AddQueryToQueue) {
}
requiresVC = (interaction: ChatInputCommandInteraction) => interaction.options.getSubcommand() === 'use';
@ -87,14 +89,16 @@ export default class implements Command {
},
});
let results = query === '' ? favorites : favorites.filter(f => f.name.startsWith(query));
let results = query === '' ? favorites : favorites.filter(f => f.name.toLowerCase().startsWith(query.toLowerCase()));
if (subcommand === 'remove') {
// Only show favorites that user is allowed to remove
results = interaction.member?.user.id === interaction.guild?.ownerId ? results : results.filter(r => r.authorId === interaction.member!.user.id);
}
await interaction.respond(results.map(r => ({
// Limit results to 25 maximum per Discord limits
const trimmed = results.length > 25 ? results.slice(0, 25) : results;
await interaction.respond(trimmed.map(r => ({
name: r.name,
value: r.name,
})));
@ -135,18 +139,22 @@ export default class implements Command {
return;
}
const embed = new EmbedBuilder().setTitle('Favorites');
let description = '';
for (const favorite of favorites) {
description += `**${favorite.name}**: ${favorite.query} (<@${favorite.authorId}>)\n`;
const fields = new Array<APIEmbedField>(favorites.length);
for (let index = 0; index < favorites.length; index++) {
const favorite = favorites[index];
fields[index] = {
inline: false,
name: favorite.name,
value: `${favorite.query} (<@${favorite.authorId}>)`,
};
}
embed.setDescription(description);
await interaction.reply({
embeds: [embed],
});
await new Pagination(
interaction as ChatInputCommandInteraction<'cached'>,
{ephemeral: true, limit: 25})
.setFields(fields)
.paginateFields(true)
.render();
}
private async create(interaction: ChatInputCommandInteraction) {