Add giveaway, ticket, self-role, starboard, and suggestion features to the bot
- Introduced new models in the Prisma schema for giveaways, tickets, self-role panels, starboard configurations, and suggestions, enhancing the bot's functionality. - Implemented job handling for self-role expiration and ticket management, improving user experience. - Updated command localization to support new features in both German and English. - Enhanced the job processing system to include dedicated workers for managing tickets and self-role expirations. - Documented the new features and their setup processes in PHASE-TRACKING.md for clarity on implementation steps.
This commit is contained in:
251
apps/bot/src/modules/starboard/service.ts
Normal file
251
apps/bot/src/modules/starboard/service.ts
Normal file
@@ -0,0 +1,251 @@
|
||||
import {
|
||||
EmbedBuilder,
|
||||
type Emoji,
|
||||
type Message,
|
||||
type PartialMessageReaction,
|
||||
type MessageReaction,
|
||||
type TextChannel
|
||||
} from 'discord.js';
|
||||
import type { PrismaClient, StarboardConfig } from '@prisma/client';
|
||||
import { t } from '@nexumi/shared';
|
||||
import type { Locale } from '@nexumi/shared';
|
||||
import { ensureGuild } from '../../guild.js';
|
||||
import { logger } from '../../logger.js';
|
||||
import type { BotContext } from '../../types.js';
|
||||
import { getGuildLocale } from '../../i18n.js';
|
||||
|
||||
export async function getStarboardConfig(prisma: PrismaClient, guildId: string) {
|
||||
await ensureGuild(prisma, guildId);
|
||||
let config = await prisma.starboardConfig.findUnique({ where: { guildId } });
|
||||
if (!config) {
|
||||
config = await prisma.starboardConfig.create({ data: { guildId } });
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
export async function updateStarboardConfig(
|
||||
prisma: PrismaClient,
|
||||
guildId: string,
|
||||
data: {
|
||||
enabled: boolean;
|
||||
channelId: string;
|
||||
emoji: string;
|
||||
threshold: number;
|
||||
allowSelfStar: boolean;
|
||||
}
|
||||
) {
|
||||
await ensureGuild(prisma, guildId);
|
||||
return prisma.starboardConfig.upsert({
|
||||
where: { guildId },
|
||||
update: data,
|
||||
create: { guildId, ...data }
|
||||
});
|
||||
}
|
||||
|
||||
export function emojiMatches(configEmoji: string, reactionEmoji: Emoji): boolean {
|
||||
if (reactionEmoji.id) {
|
||||
return (
|
||||
configEmoji === reactionEmoji.identifier ||
|
||||
configEmoji === reactionEmoji.toString() ||
|
||||
configEmoji === `<:${reactionEmoji.name}:${reactionEmoji.id}>` ||
|
||||
configEmoji === `<a:${reactionEmoji.name}:${reactionEmoji.id}>`
|
||||
);
|
||||
}
|
||||
return configEmoji === reactionEmoji.name || configEmoji === reactionEmoji.toString();
|
||||
}
|
||||
|
||||
export function shouldIgnoreMessage(message: Message, config: StarboardConfig): boolean {
|
||||
if (message.author.bot) {
|
||||
return true;
|
||||
}
|
||||
if (!message.guild || !message.channel.isTextBased() || message.channel.isDMBased()) {
|
||||
return true;
|
||||
}
|
||||
if ('nsfw' in message.channel && message.channel.nsfw) {
|
||||
return true;
|
||||
}
|
||||
if (config.ignoredChannelIds.includes(message.channel.id)) {
|
||||
return true;
|
||||
}
|
||||
if (config.channelId && message.channel.id === config.channelId) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function countMatchingStars(
|
||||
message: Message,
|
||||
config: StarboardConfig
|
||||
): Promise<number> {
|
||||
const reaction = message.reactions.cache.find((r) => emojiMatches(config.emoji, r.emoji));
|
||||
if (!reaction || reaction.count === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const fetchedReaction = reaction.partial ? await reaction.fetch() : reaction;
|
||||
const users = await fetchedReaction.users.fetch();
|
||||
let count = 0;
|
||||
for (const [, user] of users) {
|
||||
if (user.bot) {
|
||||
continue;
|
||||
}
|
||||
if (!config.allowSelfStar && user.id === message.author.id) {
|
||||
continue;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
function resolveImageUrl(message: Message): string | undefined {
|
||||
const attachment = message.attachments.find((a) => {
|
||||
const type = a.contentType ?? '';
|
||||
return type.startsWith('image/') || /\.(png|jpe?g|gif|webp)$/i.test(a.name);
|
||||
});
|
||||
if (attachment) {
|
||||
return attachment.url;
|
||||
}
|
||||
const embedImage = message.embeds.find((e) => e.image?.url)?.image?.url;
|
||||
return embedImage ?? undefined;
|
||||
}
|
||||
|
||||
export function buildStarboardEmbed(
|
||||
message: Message,
|
||||
starCount: number,
|
||||
config: StarboardConfig,
|
||||
locale: Locale
|
||||
): EmbedBuilder {
|
||||
const content = message.content?.trim() || t(locale, 'starboard.noContent');
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(0x6366f1)
|
||||
.setAuthor({
|
||||
name: message.author.tag,
|
||||
iconURL: message.author.displayAvatarURL()
|
||||
})
|
||||
.setDescription(content.slice(0, 4096))
|
||||
.addFields({
|
||||
name: t(locale, 'starboard.field.stars'),
|
||||
value: `${config.emoji} **${starCount}**`,
|
||||
inline: true
|
||||
})
|
||||
.addFields({
|
||||
name: t(locale, 'starboard.field.source'),
|
||||
value: `[${t(locale, 'starboard.jumpLink')}](${message.url})`,
|
||||
inline: true
|
||||
})
|
||||
.setTimestamp(message.createdAt);
|
||||
|
||||
const imageUrl = resolveImageUrl(message);
|
||||
if (imageUrl) {
|
||||
embed.setImage(imageUrl);
|
||||
}
|
||||
|
||||
return embed;
|
||||
}
|
||||
|
||||
async function removeStarboardEntry(context: BotContext, entryId: string): Promise<void> {
|
||||
const entry = await context.prisma.starboardEntry.findUnique({ where: { id: entryId } });
|
||||
if (!entry) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const config = await getStarboardConfig(context.prisma, entry.guildId);
|
||||
if (config.channelId) {
|
||||
const channel = (await context.client.channels.fetch(config.channelId)) as TextChannel;
|
||||
const starboardMessage = await channel.messages.fetch(entry.starboardMessageId).catch(() => null);
|
||||
await starboardMessage?.delete().catch(() => undefined);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn({ error, entryId }, 'Failed to delete starboard message');
|
||||
}
|
||||
|
||||
await context.prisma.starboardEntry.delete({ where: { id: entryId } }).catch(() => undefined);
|
||||
}
|
||||
|
||||
export async function processStarboardMessage(context: BotContext, message: Message): Promise<void> {
|
||||
if (!message.guild) {
|
||||
return;
|
||||
}
|
||||
|
||||
const config = await getStarboardConfig(context.prisma, message.guild.id);
|
||||
if (!config.enabled || !config.channelId) {
|
||||
return;
|
||||
}
|
||||
if (shouldIgnoreMessage(message, config)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const starCount = await countMatchingStars(message, config);
|
||||
const locale = await getGuildLocale(context.prisma, message.guild.id);
|
||||
const existing = await context.prisma.starboardEntry.findUnique({
|
||||
where: {
|
||||
guildId_sourceMessageId: {
|
||||
guildId: message.guild.id,
|
||||
sourceMessageId: message.id
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (starCount < config.threshold) {
|
||||
if (existing) {
|
||||
await removeStarboardEntry(context, existing.id);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const embed = buildStarboardEmbed(message, starCount, config, locale);
|
||||
const starboardChannel = (await context.client.channels.fetch(config.channelId)) as TextChannel;
|
||||
|
||||
if (existing) {
|
||||
try {
|
||||
const starboardMessage = await starboardChannel.messages.fetch(existing.starboardMessageId);
|
||||
await starboardMessage.edit({ embeds: [embed] });
|
||||
await context.prisma.starboardEntry.update({
|
||||
where: { id: existing.id },
|
||||
data: { starCount }
|
||||
});
|
||||
} catch (error) {
|
||||
logger.warn({ error, messageId: message.id }, 'Failed to update starboard message');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const starboardMessage = await starboardChannel.send({ embeds: [embed] });
|
||||
await context.prisma.starboardEntry.create({
|
||||
data: {
|
||||
guildId: message.guild.id,
|
||||
sourceChannelId: message.channel.id,
|
||||
sourceMessageId: message.id,
|
||||
starboardMessageId: starboardMessage.id,
|
||||
starCount
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function processStarboardReaction(
|
||||
context: BotContext,
|
||||
reaction: MessageReaction | PartialMessageReaction
|
||||
): Promise<void> {
|
||||
if (reaction.partial) {
|
||||
await reaction.fetch();
|
||||
}
|
||||
|
||||
const message = reaction.message;
|
||||
if (message.partial) {
|
||||
await message.fetch();
|
||||
}
|
||||
if (!message.guild || !message.author) {
|
||||
return;
|
||||
}
|
||||
|
||||
const config = await getStarboardConfig(context.prisma, message.guild.id);
|
||||
if (!config.enabled || !config.channelId) {
|
||||
return;
|
||||
}
|
||||
if (!emojiMatches(config.emoji, reaction.emoji)) {
|
||||
return;
|
||||
}
|
||||
|
||||
await processStarboardMessage(context, message as Message);
|
||||
}
|
||||
Reference in New Issue
Block a user