Adds icons to group list and adds new playdate

This commit is contained in:
Michel Fedde 2025-05-27 19:20:10 +02:00
parent 2f826fbf36
commit 8ac59567b9
4 changed files with 55 additions and 6 deletions

View file

@ -3,7 +3,13 @@ import {
Interaction,
CommandInteraction,
ChatInputCommandInteraction,
MessageFlags, GuildMemberRoleManager, InteractionReplyOptions, GuildMember, EmbedBuilder, AutocompleteInteraction
MessageFlags,
GuildMemberRoleManager,
InteractionReplyOptions,
GuildMember,
EmbedBuilder,
AutocompleteInteraction,
formatEmoji, roleMention, time
} from "discord.js";
import {AutocompleteCommand, ChatInteractionCommand, Command} from "./Command";
import {GroupModel} from "../../Models/GroupModel";
@ -17,6 +23,9 @@ import {GroupConfigurationRenderer} from "../../Groups/GroupConfigurationRendere
import {GroupConfigurationHandler} from "../../Groups/GroupConfigurationHandler";
import {GroupConfigurationTransformers} from "../../Groups/GroupConfigurationTransformers";
import {GroupConfigurationRepository} from "../../Repositories/GroupConfigurationRepository";
import {IconCache} from "../../Icons/IconCache";
import {PlaydateRepository} from "../../Repositories/PlaydateRepository";
import playdate from "../../Database/tables/Playdate";
export class GroupCommand implements Command, ChatInteractionCommand, AutocompleteCommand {
private static GOODBYE_MESSAGES: string[] = [
@ -109,15 +118,29 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
const repo = Container.get<GroupRepository>(GroupRepository.name);
const groups = repo.findGroupsByMember(<GuildMember>interaction.member);
const playdateRepo = Container.get<PlaydateRepository>(PlaydateRepository.name);
const iconCache = Container.get<IconCache>(IconCache.name);
const embed = new EmbedBuilder()
.setTitle("Your groups on this server:")
.setFields(
groups.map(group => {
const nextPlaydate = playdateRepo.getNextPlaydateForGroup(group);
const values = [
`${iconCache.getEmoji("people_group_solid")} ${roleMention(group.role.roleid)}`
];
if (nextPlaydate) {
values.push(
`${iconCache.getEmoji("calendar_days_solid")} ${time(nextPlaydate.from_time, "F")}`
)
}
return {
name: group.name,
value: `
Role: <@&${group.role.roleid}>
`
value: values.join("\n")
}
})
)

View file

@ -1,4 +1,4 @@
import {Routes} from "discord.js";
import {formatEmoji, Routes, Snowflake} from "discord.js";
import {DiscordClient} from "../Discord/DiscordClient";
export class IconCache {
@ -10,7 +10,7 @@ export class IconCache {
}
public get(iconName: string): string | null {
public get(iconName: string): Snowflake | null {
if (!this.existingIcons?.has(iconName)) {
return null;
}
@ -18,6 +18,15 @@ export class IconCache {
return this.existingIcons?.get(iconName) ?? null;
}
public getEmoji(iconName: string): string {
const id = this.get(iconName);
return formatEmoji({
id: id,
name: iconName
});
}
public async set(iconName: string, pngBuffer: Buffer) {
const pngBase64 = pngBuffer.toString("base64");
const iconDataUrl = `data:image/png;base64,${pngBase64}`;

View file

@ -58,6 +58,22 @@ export class PlaydateRepository extends Repository<PlaydateModel, DBPlaydate> {
return finds.map((playdate) => this.convertToModelType(playdate, group));
}
getNextPlaydateForGroup(group: GroupModel): PlaydateModel | null {
let sql = `SELECT * FROM ${this.schema.name} WHERE groupid = ? AND time_from > ? ORDER BY time_from ASC LIMIT 1`;
const find = this.database.fetch<number, DBPlaydate>(
sql,
group.id,
Date.now()
)
if (!find) {
return null;
}
return this.convertToModelType(find, group)
}
protected convertToModelType(intermediateModel: DBPlaydate | undefined, fixedGroup: Nullable<GroupModel> = null): PlaydateModel {
if (!intermediateModel) {
throw new Error("Unable to convert the playdate model");