diff --git a/src/commands/mod/sus.ts b/src/commands/mod/sus.ts index 633965a..456cbd0 100644 --- a/src/commands/mod/sus.ts +++ b/src/commands/mod/sus.ts @@ -38,6 +38,7 @@ import { } from '#utils/database/sus'; import { checkStaff } from '#utils/checker'; import IDs from '#utils/ids'; +import { createSusLogEmbed } from '#utils/embeds'; // TODO add a check when they join the server to give the user the sus role again @@ -219,34 +220,7 @@ export class SusCommand extends Subcommand { } // Creates the embed to display the sus note - const noteEmbed = new EmbedBuilder() - .setColor('#0099ff') - .setTitle(`${notes.length} sus notes for ${user.username}`) - .setThumbnail(user.displayAvatarURL()); - - // Add up to 10 of the latest sus notes to the embed - for ( - let i = notes.length > 10 ? notes.length - 10 : 0; - i < notes.length; - i += 1 - ) { - // Get mod name - let mod = notes[i].modId; - const modMember = guild.members.cache.get(mod); - if (modMember !== undefined) { - mod = modMember.displayName; - } - - // Add sus note to embed - noteEmbed.addFields({ - name: `Sus ID: ${ - notes[i].id - } | Moderator: ${mod} | Date: `, - value: notes[i].note, - }); - } + const noteEmbed = createSusLogEmbed(notes, user, guild); // Sends the notes to the user await interaction.reply({ @@ -549,11 +523,6 @@ export class SusCommand extends Subcommand { await user.roles.add(IDs.roles.restrictions.sus); } - // Checks if the user is xlevra to send a very kind message - if (mod.id === '259624904746467329') { - await message.reply('Fuck you for making me add this feature 🤬'); - } - await message.react('✅'); } } diff --git a/src/commands/mod/warning/warnings.ts b/src/commands/mod/warning/warnings.ts index cdddcea..6855570 100644 --- a/src/commands/mod/warning/warnings.ts +++ b/src/commands/mod/warning/warnings.ts @@ -23,6 +23,7 @@ import type { Message, Guild, User } from 'discord.js'; import IDs from '#utils/ids'; import { fetchWarnings } from '#utils/database/warnings'; import { checkStaff } from '#utils/checker'; +import { createWarningsEmbed } from '#utils/embeds'; export class WarningsCommand extends Command { public constructor(context: Command.LoaderContext, options: Command.Options) { @@ -152,34 +153,7 @@ export class WarningsCommand extends Command { } // Creates an embed to display the warnings - const embed = new EmbedBuilder() - .setColor('#FF6700') - .setTitle(`${warnings.length} restrictions for ${user.tag}`) - .setThumbnail(user.displayAvatarURL()) - .setFooter({ text: `ID: ${user.id}` }); - - // Add up to 10 of the latest warnings to the embed - for ( - let i = warnings.length > 10 ? warnings.length - 10 : 0; - i < warnings.length; - i += 1 - ) { - // Get mod names - let mod = warnings[i].modId; - const modMember = guild.members.cache.get(mod); - if (modMember !== undefined) { - mod = modMember.displayName; - } - - let warnTitle = `ID: ${warnings[i].id} | Moderator: ${mod} | `; - - warnTitle += `Date: `; - - embed.addFields({ - name: warnTitle, - value: warnings[i].note, - }); - } + const embed = createWarningsEmbed(warnings, user, guild); info.embeds.push(embed); return info; diff --git a/src/listeners/modMail.ts b/src/listeners/modMail.ts index 8e909fe..139082d 100644 --- a/src/listeners/modMail.ts +++ b/src/listeners/modMail.ts @@ -18,15 +18,24 @@ */ import { Listener } from '@sapphire/framework'; -import { ChannelType, EmbedBuilder } from 'discord.js'; -import type { GuildChannel } from 'discord.js'; +import { ChannelType } from 'discord.js'; +import type { GuildChannel, EmbedBuilder } from 'discord.js'; import { setTimeout } from 'timers/promises'; import IDs from '#utils/ids'; import { checkActive, getRestrictions } from '#utils/database/restriction'; import { findNotes } from '#utils/database/sus'; +import { + createRestrictLogEmbed, + createSusLogEmbed, + createWarningsEmbed, +} from '#utils/embeds'; +import { fetchWarnings } from '#utils/database/warnings'; export class ModMailCreateListener extends Listener { - public constructor(context: Listener.LoaderContext, options: Listener.Options) { + public constructor( + context: Listener.LoaderContext, + options: Listener.Options, + ) { super(context, { ...options, event: 'channelCreate', @@ -51,6 +60,16 @@ export class ModMailCreateListener extends Listener { // Get the user's ID const userId = topic[2]; + // Gets user who created ModMail + let user = guild.client.users.cache.get(userId); + + if (user === undefined) { + user = await guild.client.users.fetch(userId); + if (user === undefined) { + return; + } + } + // Check if the user is currently restricted on the database if (!(await checkActive(userId))) return; @@ -60,81 +79,21 @@ export class ModMailCreateListener extends Listener { // Creation of embeds // Restriction Logs - const restrictEmbed = new EmbedBuilder() - .setColor('#FF6700') - .setTitle(`${restrictions.length} restrictions`) - .setFooter({ text: `ID: ${userId}` }); + const embeds: EmbedBuilder[] = []; + embeds.push(createRestrictLogEmbed(restrictions, user, guild)); - // Add up to 10 of the latest restrictions to the embed - for ( - let i = restrictions.length > 10 ? restrictions.length - 10 : 0; - i < restrictions.length; - i += 1 - ) { - // Get mod names - let restMod = restrictions[i].modId; - const restModMember = guild.members.cache.get(restMod); - if (restModMember !== undefined) { - restMod = restModMember.displayName; - } - let endRestMod = restrictions[i].endModId; - if (endRestMod !== null) { - const endRestModMember = guild.members.cache.get(endRestMod); - if (endRestModMember !== undefined) { - endRestMod = endRestModMember.displayName; - } - } + // Warnings + const warnings = await fetchWarnings(userId); - let restTitle = `Restriction: ${i + 1} | Restricted by: ${restMod} | `; - - if (endRestMod !== null) { - restTitle += `Unrestricted by: ${endRestMod} | `; - } else { - restTitle += 'Currently Restricted | '; - } - - restTitle += `Date: `; - - restrictEmbed.addFields({ - name: restTitle, - value: restrictions[i].reason, - }); - } + embeds.push(createWarningsEmbed(warnings, user, guild)); // Sus Notes const notes = await findNotes(userId, true); - const susEmbed = new EmbedBuilder() - .setColor('#0099ff') - .setTitle(`${notes.length} sus notes`); - - // Add up to 10 of the latest sus notes to the embed - for ( - let i = notes.length > 10 ? notes.length - 10 : 0; - i < notes.length; - i += 1 - ) { - // Get mod name - const modGuildMember = guild.members.cache.get(notes[i].modId); - let mod = notes[i].modId; - if (modGuildMember !== undefined) { - mod = modGuildMember.displayName; - } - // Add sus note to embed - susEmbed.addFields({ - name: `Sus ID: ${ - notes[i].id - } | Moderator: ${mod} | Date: `, - value: notes[i].note, - }); - } + embeds.push(createSusLogEmbed(notes, user, guild)); // Set a timeout for 1 second and then send the 2 embeds await setTimeout(1000); - await channel.send({ embeds: [restrictEmbed, susEmbed] }); + await channel.send({ embeds: embeds }); } } diff --git a/src/utils/database/restriction.ts b/src/utils/database/restriction.ts index 837641b..e8852e5 100644 --- a/src/utils/database/restriction.ts +++ b/src/utils/database/restriction.ts @@ -1,5 +1,6 @@ import { container } from '@sapphire/framework'; import type { Snowflake } from 'discord.js'; +import { Prisma } from '@prisma/client'; export async function restrict( userId: Snowflake, @@ -71,6 +72,8 @@ export async function getRestrictions(userId: Snowflake) { return restrictions; } +export type RestrictionLogs = Prisma.PromiseReturnType; + export async function checkActive(userId: Snowflake) { const restriction = await container.database.restrict.findFirst({ where: { diff --git a/src/utils/database/sus.ts b/src/utils/database/sus.ts index 528d6a0..c4bcf9a 100644 --- a/src/utils/database/sus.ts +++ b/src/utils/database/sus.ts @@ -1,4 +1,5 @@ import { container } from '@sapphire/framework'; +import { Prisma } from '@prisma/client'; export async function addToDatabase( userId: string, @@ -39,6 +40,8 @@ export async function findNotes(userId: string, active: boolean) { return note; } +export type SusNotes = Prisma.PromiseReturnType; + // Get one note from the id export async function getNote(noteId: number) { // Query to get the specific user's sus notes diff --git a/src/utils/database/warnings.ts b/src/utils/database/warnings.ts index 4823820..e36d4fa 100644 --- a/src/utils/database/warnings.ts +++ b/src/utils/database/warnings.ts @@ -1,5 +1,6 @@ import { container } from '@sapphire/framework'; import type { Snowflake } from 'discord.js'; +import { Prisma } from '@prisma/client'; export async function addWarn( userId: Snowflake, @@ -51,6 +52,8 @@ export async function fetchWarnings(userId: Snowflake) { return warnings; } +export type Warnings = Prisma.PromiseReturnType; + export async function deleteWarning(warningId: number) { await container.database.warning.delete({ where: { diff --git a/src/utils/embeds.ts b/src/utils/embeds.ts new file mode 100644 index 0000000..9fa4e45 --- /dev/null +++ b/src/utils/embeds.ts @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +/* + Animal Rights Advocates Discord Bot + Copyright (C) 2024 Anthony Berg + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +import type { Guild, User } from 'discord.js'; +import { EmbedBuilder } from 'discord.js'; +import type { SusNotes } from '#utils/database/sus'; +import { RestrictionLogs } from '#utils/database/restriction'; +import { Warnings } from '#utils/database/warnings'; + +export function createSusLogEmbed(notes: SusNotes, user: User, guild: Guild) { + const embed = new EmbedBuilder() + .setColor('#0099ff') + .setTitle(`${notes.length} sus notes for ${user.username}`) + .setThumbnail(user.displayAvatarURL()); + + // Add up to 10 of the latest sus notes to the embed + for ( + let i = notes.length > 10 ? notes.length - 10 : 0; + i < notes.length; + i += 1 + ) { + // Get mod name + let mod = notes[i].modId; + const modMember = guild.members.cache.get(mod); + if (modMember !== undefined) { + mod = modMember.displayName; + } + + // Add sus note to embed + embed.addFields({ + name: `Sus ID: ${notes[i].id} | Moderator: ${mod} | Date: `, + value: notes[i].note, + }); + } + + return embed; +} + +export function createRestrictLogEmbed( + restrictions: RestrictionLogs, + user: User, + guild: Guild, +) { + const embed = new EmbedBuilder() + .setColor('#FF6700') + .setTitle(`${restrictions.length} restrictions for ${user.tag}`) + .setThumbnail(user.displayAvatarURL()) + .setFooter({ text: `ID: ${user.id}` }); + + // Add up to 10 of the latest restrictions to the embed + for ( + let i = restrictions.length > 10 ? restrictions.length - 10 : 0; + i < restrictions.length; + i += 1 + ) { + // Get mod names + let restMod = restrictions[i].modId; + const restModMember = guild.members.cache.get(restMod); + if (restModMember !== undefined) { + restMod = restModMember.displayName; + } + let endRestMod = restrictions[i].endModId; + if (endRestMod !== null) { + const endRestModMember = guild.members.cache.get(endRestMod); + if (endRestModMember !== undefined) { + endRestMod = endRestModMember.displayName; + } + } + + let restTitle = `Restriction: ${i + 1} | Restricted by: ${restMod} | `; + + if (endRestMod !== null) { + restTitle += `Unrestricted by: ${endRestMod} | `; + } else { + restTitle += 'Currently Restricted | '; + } + + restTitle += `Date: `; + + embed.addFields({ + name: restTitle, + value: restrictions[i].reason, + }); + } + + return embed; +} + +export function createWarningsEmbed( + warnings: Warnings, + user: User, + guild: Guild, +) { + const embed = new EmbedBuilder() + .setColor('#FF6700') + .setTitle(`${warnings.length} warnings for ${user.tag}`) + .setThumbnail(user.displayAvatarURL()) + .setFooter({ text: `ID: ${user.id}` }); + + // Add up to 10 of the latest warnings to the embed + for ( + let i = warnings.length > 10 ? warnings.length - 10 : 0; + i < warnings.length; + i += 1 + ) { + // Get mod names + let mod = warnings[i].modId; + const modMember = guild.members.cache.get(mod); + if (modMember !== undefined) { + mod = modMember.displayName; + } + + let warnTitle = `ID: ${warnings[i].id} | Moderator: ${mod} | `; + + warnTitle += `Date: `; + + embed.addFields({ + name: warnTitle, + value: warnings[i].note, + }); + } + + return embed; +}