feat(arabot): move embeds to separate file and add warnings to ModMail

This commit is contained in:
Anthony Berg 2024-01-04 17:57:15 +00:00
parent 0bb10b55ed
commit e5f2c9436e
7 changed files with 185 additions and 131 deletions

View File

@ -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: <t:${Math.floor(
notes[i].time.getTime() / 1000,
)}>`,
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('✅');
}
}

View File

@ -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: <t:${Math.floor(warnings[i].time.getTime() / 1000)}>`;
embed.addFields({
name: warnTitle,
value: warnings[i].note,
});
}
const embed = createWarningsEmbed(warnings, user, guild);
info.embeds.push(embed);
return info;

View File

@ -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: <t:${Math.floor(
restrictions[i].startTime.getTime() / 1000,
)}>`;
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: <t:${Math.floor(
notes[i].time.getTime() / 1000,
)}>`,
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 });
}
}

View File

@ -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<typeof getRestrictions>;
export async function checkActive(userId: Snowflake) {
const restriction = await container.database.restrict.findFirst({
where: {

View File

@ -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<typeof findNotes>;
// Get one note from the id
export async function getNote(noteId: number) {
// Query to get the specific user's sus notes

View File

@ -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<typeof fetchWarnings>;
export async function deleteWarning(warningId: number) {
await container.database.warning.delete({
where: {

143
src/utils/embeds.ts Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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: <t:${Math.floor(
notes[i].time.getTime() / 1000,
)}>`,
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: <t:${Math.floor(
restrictions[i].startTime.getTime() / 1000,
)}>`;
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: <t:${Math.floor(warnings[i].time.getTime() / 1000)}>`;
embed.addFields({
name: warnTitle,
value: warnings[i].note,
});
}
return embed;
}