Skip to content

Commit

Permalink
Add command for emote searching
Browse files Browse the repository at this point in the history
  • Loading branch information
twobiers committed Jul 19, 2024
1 parent 8c303b0 commit 9143d17
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/commands/emoteSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {
AttachmentBuilder,
BaseMessageOptions,
EmbedBuilder,
InteractionReplyOptions,
MessagePayload,
RawFile,
SlashCommandBuilder,
SlashCommandStringOption,
type CommandInteraction,
} from "discord.js";

import type { ApplicationCommand } from "./command.js";
import type { BotContext } from "../context.js";
import * as emoteLogging from "../service/emoteLogging.js";
import type { Emote } from "src/storage/db/model.js";
import { formatDateTime } from "src/utils/dateUtils.js";

function buildSingleEmoteResponse(
emote: Emote,
context: BotContext,
): [EmbedBuilder, AttachmentBuilder] {
const filename = `${emote.name}.webp`;
const file = new AttachmentBuilder(Buffer.from(emote.data)).setName(filename);
const embed = new EmbedBuilder()
.setColor(0x24283b)
.setAuthor({
name: context.client.user.username,
iconURL: context.client.user.avatarURL() ?? undefined,
})
.setThumbnail(`attachment://${filename}`)
.setTitle(emote.name)
.addFields(
{ name: "Erstellt", value: formatDateTime(new Date(emote.createdAt)), inline: true },
{ name: "Bearbeitet", value: formatDateTime(new Date(emote.updatedAt)), inline: true },
{
name: "Gelöscht",
value: emote.deletedAt ? formatDateTime(new Date(emote.deletedAt)) : "-",
inline: true,
},
);

return [embed, file];
}

export default class EmoteStatsCommand implements ApplicationCommand {
name = "emote-suche";
description = "Sucht in der Emote Datenbank nach einem oder mehreren passenden Emotes.";

applicationCommand = new SlashCommandBuilder()
.setName(this.name)
.addStringOption(
new SlashCommandStringOption()
.setRequired(true)
.setName("query")
.setDescription("Wonach du suchen willst"),
)
.setDescription(this.description);

async handleInteraction(command: CommandInteraction, context: BotContext): Promise<void> {
if (!command.isChatInputCommand()) {
return;
}

const query = command.options.getString("query", true);
const emotes = await emoteLogging.getMatchingEmotes(query, 5);

if (emotes.length === 0) {
await command.reply({
content: "Hab keine gefunden, dicker",
});
return;
}

const responses = emotes.map(emote => buildSingleEmoteResponse(emote, context));
const embeds = responses.map(r => r[0]);
const files = responses.map(r => r[1]);

await command.reply({
embeds,
files,
});
}
}
8 changes: 8 additions & 0 deletions src/service/emoteLogging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ export async function persistCurrentGuildEmotes(context: BotContext) {
export async function getGlobalStats(limit: number) {
return dbEmote.getGlobalUsage(limit | 0);
}

export async function getMatchingEmotes(query: string, limit: number) {
if (query.trim().length === 0) {
return [];
}

return dbEmote.searchEmote(query, limit | 0);
}
13 changes: 13 additions & 0 deletions src/storage/emote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,16 @@ export async function getGlobalUsage(limit: number, ctx = db()) {
.limit(limit)
.execute();
}

export async function searchEmote(
searchQuery: string,
limit: number,
ctx = db(),
): Promise<Emote[]> {
return await ctx
.selectFrom("emote")
.where("emote.name", "ilike", `%${searchQuery}%`)
.limit(limit)
.selectAll()
.execute();
}

0 comments on commit 9143d17

Please sign in to comment.