From c5040c81a827e7834162aee0b41f2eec8e04d1c2 Mon Sep 17 00:00:00 2001 From: crylia Date: Thu, 12 Sep 2024 05:18:50 +0200 Subject: [PATCH] Put updateGlobalMessage into the blacklist feature --- src/discord/discordClient.js | 82 ++---------------------------------- src/features/blacklist.js | 80 +++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 79 deletions(-) diff --git a/src/discord/discordClient.js b/src/discord/discordClient.js index 6f6dab8..dd8fe3d 100644 --- a/src/discord/discordClient.js +++ b/src/discord/discordClient.js @@ -1,4 +1,4 @@ -const { Client, GatewayIntentBits, EmbedBuilder, Partials, Routes } = require('discord.js') +const { Client, GatewayIntentBits, Partials, Routes } = require('discord.js') const { REST } = require('@discordjs/rest') const { SlashCommandBuilder } = require('@discordjs/builders') const { @@ -11,6 +11,7 @@ const { handleBlacklistAdd, handleBlacklistCheck, handleBlacklistShow, + updateGlobalMessage, } = require('../features/blacklist') const { @@ -36,83 +37,6 @@ const client = new Client({ ] }) -const createBlacklistEmbeds = (playerEntries, maxChars = 30) => { - const embeds = [] - let embed = new EmbedBuilder() - .setColor('#0099ff') - .setTitle('Blacklist (Page 1)') - .setDescription('Players who have been blacklisted and the reasons.') - - let fieldCount = 0 - let pageIndex = 1 - - playerEntries.forEach(([playerName, reason]) => { - let splitReason = [] - while (reason.length > maxChars) { - let splitIndex = reason.lastIndexOf(' ', maxChars) - if (splitIndex === -1) splitIndex = maxChars - splitReason.push(reason.substring(0, splitIndex)) - reason = reason.substring(splitIndex + 1) - } - splitReason.push(reason) - - const nameField = playerName || '\u200B' - const valueField = splitReason.join('\n').trim() || '\u200B' - - if (valueField !== '\u200B') { - embed.addFields({ name: nameField, value: valueField, inline: true }) - fieldCount++ - } - - if (fieldCount >= 25) { - embeds.push(embed) - pageIndex++ - embed = new EmbedBuilder() - .setColor('#0099ff') - .setTitle(`Blacklist (Page ${pageIndex})`) - .setDescription('Players who have been blacklisted and the reasons.') - fieldCount = 0 - } - }) - - if (fieldCount > 0) { - embeds.push(embed) - } - - return embeds -} - -const updateGlobalMessage = async () => { - try { - let targetChannel = null - - for (const [_, oauthGuild] of await client.guilds.fetch()) { - targetChannel = (await (await oauthGuild.fetch()).channels.fetch()).find(ch => ch.name === 'blacklist') - break - } - - if (!targetChannel) { - console.error('Channel with name "blacklist" not found.') - return - } - - const messages = await targetChannel.messages.fetch({ limit: 100 }) - await Promise.all(messages.map(msg => msg.delete())) - - const blacklistEntries = await handleBlacklistShow() - - const playerEntries = blacklistEntries.map(entry => [entry.name, entry.reason]) - - const embeds = createBlacklistEmbeds(playerEntries) - - for (const embed of embeds) - await targetChannel.send({ embeds: [embed] }) - - } catch (error) { - console.error('Error updating global message:', error) - } -} - client.on('interactionCreate', async interaction => { if (!interaction.isCommand()) return @@ -218,7 +142,7 @@ client.once('ready', async () => { console.log(`Logged in as ${client.user.tag} `) startBirthdayCheckCron(client) startEventCheckCron(client) - updateGlobalMessage() + updateGlobalMessage(client) initReactionPerRole(client) }) diff --git a/src/features/blacklist.js b/src/features/blacklist.js index a308d8f..a885e25 100644 --- a/src/features/blacklist.js +++ b/src/features/blacklist.js @@ -1,4 +1,5 @@ const { CreateBlacklist, ReadBlacklist, UpdateBlacklist, DeleteBlacklist } = require('../database/blacklistdb') +const { EmbedBuilder } = require('discord.js') const handleBlacklistAdd = async (reportedUser, reason, reportedByUser) => { if (!reportedUser || !reason || !reportedByUser) return false @@ -18,8 +19,87 @@ const handleBlacklistCheck = async (user) => { const handleBlacklistShow = async () => await ReadBlacklist() +const createBlacklistEmbeds = (playerEntries, maxChars = 30) => { + const embeds = [] + let embed = new EmbedBuilder() + .setColor('#0099ff') + .setTitle('Blacklist (Page 1)') + .setDescription('Players who have been blacklisted and the reasons.') + + let fieldCount = 0 + let pageIndex = 1 + + playerEntries.forEach(([playerName, reason]) => { + let splitReason = [] + while (reason.length > maxChars) { + let splitIndex = reason.lastIndexOf(' ', maxChars) + if (splitIndex === -1) splitIndex = maxChars + splitReason.push(reason.substring(0, splitIndex)) + reason = reason.substring(splitIndex + 1) + } + splitReason.push(reason) + + const nameField = playerName || '\u200B' + const valueField = splitReason.join('\n').trim() || '\u200B' + + if (valueField !== '\u200B') { + embed.addFields({ name: nameField, value: valueField, inline: true }) + fieldCount++ + } + + if (fieldCount >= 25) { + embeds.push(embed) + pageIndex++ + embed = new EmbedBuilder() + .setColor('#0099ff') + .setTitle(`Blacklist (Page ${pageIndex})`) + .setDescription('Players who have been blacklisted and the reasons.') + fieldCount = 0 + } + }) + + if (fieldCount > 0) { + embeds.push(embed) + } + + return embeds +} + +const updateGlobalMessage = async (client) => { + try { + let targetChannel = null + + for (const [_, oauthGuild] of await client.guilds.fetch()) { + targetChannel = (await (await oauthGuild.fetch()).channels.fetch()).find(ch => ch.name === 'blacklist') + break + } + + if (!targetChannel) { + console.error('Channel with name "blacklist" not found.') + return + } + + const messages = await targetChannel.messages.fetch({ limit: 100 }) + await Promise.all(messages.map(msg => msg.delete())) + + const blacklistEntries = await handleBlacklistShow() + + const playerEntries = blacklistEntries.map(entry => [entry.name, entry.reason]) + + const embeds = createBlacklistEmbeds(playerEntries) + + for (const embed of embeds) + await targetChannel.send({ embeds: [embed] }) + + } catch (error) { + console.error('Error updating global message:', error) + } +} + + module.exports = { handleBlacklistAdd, handleBlacklistCheck, handleBlacklistShow, + updateGlobalMessage, }