Merge branch 'warnings'

This commit is contained in:
Tarrgon
2025-05-29 09:42:30 -04:00
13 changed files with 280 additions and 20 deletions
+8
View File
@@ -0,0 +1,8 @@
import { Channel, GuildBasedChannel, GuildChannel, GuildTextBasedChannel, TextBasedChannel, VoiceBasedChannel } from 'discord.js';
import { Database } from '../shared/Database';
export async function channelIsInStaffCategory(channel: GuildBasedChannel) {
const staffCategories = await Database.getGuildStaffCategories(channel.guildId);
return staffCategories.includes(channel.parentId!);
}
+2
View File
@@ -1,5 +1,6 @@
export * from './array-utils';
export * from './audit-log-utils';
export * from './channel-utils';
export * from './commands';
export * from './e621-ticket-listener';
export * from './e621-utils';
@@ -11,3 +12,4 @@ export * from './string-utils';
export * from './ticket-utils';
export * from './wait';
export * from './whois';
export * from './warning-utils';
+51
View File
@@ -0,0 +1,51 @@
import { ActionRowBuilder, APIEmbedField, ButtonBuilder, ButtonStyle, EmbedBuilder, GuildMember, MessageActionRowComponentBuilder, time } from 'discord.js';
import { Database } from '../shared/Database';
import { Warning } from '../types';
type MessageContent = { content: string, components: ActionRowBuilder<ButtonBuilder>[] };
const WARNINGS_PER_PAGE = 5;
function getWarningText(warning: Warning): string {
const timestamp = time(new Date(warning.timestamp));
return `### Warning from <@${warning.mod_id}> (${timestamp}):\n${warning.reason}`;
}
export async function getWarningMessage(userId: string, page: number): Promise<MessageContent | null> {
const warnings = await Database.getWarnings(userId);
page = page - 1;
const maxPage = Math.floor(warnings.length / WARNINGS_PER_PAGE);
if (warnings.length == 0) return null;
const warningTexts: string[] = [];
for (let i = page * WARNINGS_PER_PAGE; i < page * WARNINGS_PER_PAGE + WARNINGS_PER_PAGE; i++) {
if (i >= warnings.length) break;
warningTexts.push(getWarningText(warnings[i]));
}
const prevPage = new ButtonBuilder()
.setLabel('Previous Page')
.setCustomId(`warning-previous_${userId}_${page + 1}`)
.setDisabled(page == 0)
.setStyle(ButtonStyle.Primary);
const nextPage = new ButtonBuilder()
.setLabel('Next Page')
.setCustomId(`warning-next_${userId}_${page + 1}`)
.setDisabled(page >= maxPage)
.setStyle(ButtonStyle.Primary);
const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents(prevPage, nextPage);
return {
content: `<@${userId}>'s Warnings\n` + warningTexts.join('\n\n') + `\n\n-# Page ${page + 1}/${maxPage + 1}`,
components: [row]
};
}