fix: Starboard
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
import {
|
||||
ChannelType,
|
||||
EmbedBuilder,
|
||||
PermissionFlagsBits,
|
||||
type Emoji,
|
||||
type GuildBasedChannel,
|
||||
type Message,
|
||||
type PartialMessage,
|
||||
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 { t, type Locale } from '@nexumi/shared';
|
||||
import { ensureGuild } from '../../guild.js';
|
||||
import { logger } from '../../logger.js';
|
||||
import type { BotContext } from '../../types.js';
|
||||
@@ -73,6 +76,27 @@ export function shouldIgnoreMessage(message: Message, config: StarboardConfig):
|
||||
return false;
|
||||
}
|
||||
|
||||
export function botCanPostStarboard(channel: GuildBasedChannel, meId: string): channel is TextChannel {
|
||||
if (!channel.isTextBased() || channel.isDMBased()) {
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
channel.type !== ChannelType.GuildText &&
|
||||
channel.type !== ChannelType.GuildAnnouncement
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
const permissions = channel.permissionsFor(meId);
|
||||
if (!permissions) {
|
||||
return false;
|
||||
}
|
||||
return (
|
||||
permissions.has(PermissionFlagsBits.ViewChannel) &&
|
||||
permissions.has(PermissionFlagsBits.SendMessages) &&
|
||||
permissions.has(PermissionFlagsBits.EmbedLinks)
|
||||
);
|
||||
}
|
||||
|
||||
export async function countMatchingStars(
|
||||
message: Message,
|
||||
config: StarboardConfig
|
||||
@@ -109,6 +133,10 @@ function resolveImageUrl(message: Message): string | undefined {
|
||||
return embedImage ?? undefined;
|
||||
}
|
||||
|
||||
function authorDisplayName(message: Message): string {
|
||||
return message.member?.displayName || message.author.displayName || message.author.username;
|
||||
}
|
||||
|
||||
export function buildStarboardEmbed(
|
||||
message: Message,
|
||||
starCount: number,
|
||||
@@ -119,7 +147,7 @@ export function buildStarboardEmbed(
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(0x6366f1)
|
||||
.setAuthor({
|
||||
name: message.author.tag,
|
||||
name: authorDisplayName(message),
|
||||
iconURL: message.author.displayAvatarURL()
|
||||
})
|
||||
.setDescription(content.slice(0, 4096))
|
||||
@@ -143,6 +171,17 @@ export function buildStarboardEmbed(
|
||||
return embed;
|
||||
}
|
||||
|
||||
async function fetchStarboardTextChannel(
|
||||
context: BotContext,
|
||||
channelId: string
|
||||
): Promise<TextChannel | null> {
|
||||
const channel = await context.client.channels.fetch(channelId).catch(() => null);
|
||||
if (!channel || !channel.isTextBased() || channel.isDMBased()) {
|
||||
return null;
|
||||
}
|
||||
return channel as TextChannel;
|
||||
}
|
||||
|
||||
async function removeStarboardEntry(context: BotContext, entryId: string): Promise<void> {
|
||||
const entry = await context.prisma.starboardEntry.findUnique({ where: { id: entryId } });
|
||||
if (!entry) {
|
||||
@@ -152,9 +191,13 @@ async function removeStarboardEntry(context: BotContext, entryId: string): Promi
|
||||
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);
|
||||
const channel = await fetchStarboardTextChannel(context, config.channelId);
|
||||
if (channel) {
|
||||
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');
|
||||
@@ -163,6 +206,82 @@ async function removeStarboardEntry(context: BotContext, entryId: string): Promi
|
||||
await context.prisma.starboardEntry.delete({ where: { id: entryId } }).catch(() => undefined);
|
||||
}
|
||||
|
||||
export async function removeStarboardEntryBySource(
|
||||
context: BotContext,
|
||||
guildId: string,
|
||||
sourceMessageId: string
|
||||
): Promise<void> {
|
||||
const entry = await context.prisma.starboardEntry.findUnique({
|
||||
where: {
|
||||
guildId_sourceMessageId: { guildId, sourceMessageId }
|
||||
}
|
||||
});
|
||||
if (entry) {
|
||||
await removeStarboardEntry(context, entry.id);
|
||||
}
|
||||
}
|
||||
|
||||
async function createOrRecreateStarboardPost(
|
||||
context: BotContext,
|
||||
message: Message,
|
||||
config: StarboardConfig,
|
||||
locale: Locale,
|
||||
starCount: number,
|
||||
starboardChannel: TextChannel,
|
||||
existingId?: string
|
||||
): Promise<void> {
|
||||
const embed = buildStarboardEmbed(message, starCount, config, locale);
|
||||
const starboardMessage = await starboardChannel.send({ embeds: [embed] });
|
||||
|
||||
if (existingId) {
|
||||
await context.prisma.starboardEntry.update({
|
||||
where: { id: existingId },
|
||||
data: {
|
||||
starboardMessageId: starboardMessage.id,
|
||||
starCount,
|
||||
sourceChannelId: message.channel.id
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await context.prisma.starboardEntry.create({
|
||||
data: {
|
||||
guildId: message.guild!.id,
|
||||
sourceChannelId: message.channel.id,
|
||||
sourceMessageId: message.id,
|
||||
starboardMessageId: starboardMessage.id,
|
||||
starCount
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
// Race: another reaction created the row first — keep the newer post, drop ours.
|
||||
logger.warn({ error, messageId: message.id }, 'Starboard entry create raced; cleaning up');
|
||||
await starboardMessage.delete().catch(() => undefined);
|
||||
const winner = await context.prisma.starboardEntry.findUnique({
|
||||
where: {
|
||||
guildId_sourceMessageId: {
|
||||
guildId: message.guild!.id,
|
||||
sourceMessageId: message.id
|
||||
}
|
||||
}
|
||||
});
|
||||
if (winner) {
|
||||
await context.prisma.starboardEntry.update({
|
||||
where: { id: winner.id },
|
||||
data: { starCount }
|
||||
});
|
||||
const existingMsg = await starboardChannel.messages
|
||||
.fetch(winner.starboardMessageId)
|
||||
.catch(() => null);
|
||||
if (existingMsg) {
|
||||
await existingMsg.edit({ embeds: [embed] }).catch(() => undefined);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function processStarboardMessage(context: BotContext, message: Message): Promise<void> {
|
||||
if (!message.guild) {
|
||||
return;
|
||||
@@ -194,8 +313,13 @@ export async function processStarboardMessage(context: BotContext, message: Mess
|
||||
return;
|
||||
}
|
||||
|
||||
const starboardChannel = await fetchStarboardTextChannel(context, config.channelId);
|
||||
if (!starboardChannel) {
|
||||
logger.warn({ guildId: message.guild.id, channelId: config.channelId }, 'Starboard channel missing');
|
||||
return;
|
||||
}
|
||||
|
||||
const embed = buildStarboardEmbed(message, starCount, config, locale);
|
||||
const starboardChannel = (await context.client.channels.fetch(config.channelId)) as TextChannel;
|
||||
|
||||
if (existing) {
|
||||
try {
|
||||
@@ -206,21 +330,28 @@ export async function processStarboardMessage(context: BotContext, message: Mess
|
||||
data: { starCount }
|
||||
});
|
||||
} catch (error) {
|
||||
logger.warn({ error, messageId: message.id }, 'Failed to update starboard message');
|
||||
logger.warn({ error, messageId: message.id }, 'Starboard message missing; recreating');
|
||||
await createOrRecreateStarboardPost(
|
||||
context,
|
||||
message,
|
||||
config,
|
||||
locale,
|
||||
starCount,
|
||||
starboardChannel,
|
||||
existing.id
|
||||
);
|
||||
}
|
||||
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
|
||||
}
|
||||
});
|
||||
await createOrRecreateStarboardPost(
|
||||
context,
|
||||
message,
|
||||
config,
|
||||
locale,
|
||||
starCount,
|
||||
starboardChannel
|
||||
);
|
||||
}
|
||||
|
||||
export async function processStarboardReaction(
|
||||
@@ -249,3 +380,41 @@ export async function processStarboardReaction(
|
||||
|
||||
await processStarboardMessage(context, message as Message);
|
||||
}
|
||||
|
||||
export async function processStarboardSourceDelete(
|
||||
context: BotContext,
|
||||
message: Message | PartialMessage
|
||||
): Promise<void> {
|
||||
if (!message.guildId || !message.id) {
|
||||
return;
|
||||
}
|
||||
await removeStarboardEntryBySource(context, message.guildId, message.id);
|
||||
}
|
||||
|
||||
export async function processStarboardSourceUpdate(
|
||||
context: BotContext,
|
||||
message: Message | PartialMessage
|
||||
): Promise<void> {
|
||||
if (!message.guildId || !message.id) {
|
||||
return;
|
||||
}
|
||||
|
||||
const entry = await context.prisma.starboardEntry.findUnique({
|
||||
where: {
|
||||
guildId_sourceMessageId: {
|
||||
guildId: message.guildId,
|
||||
sourceMessageId: message.id
|
||||
}
|
||||
}
|
||||
});
|
||||
if (!entry) {
|
||||
return;
|
||||
}
|
||||
|
||||
const full = message.partial ? await message.fetch().catch(() => null) : message;
|
||||
if (!full || !full.guild) {
|
||||
return;
|
||||
}
|
||||
|
||||
await processStarboardMessage(context, full as Message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user