Skip to content

Commit

Permalink
feat: allow for reason when reporting messages (#392)
Browse files Browse the repository at this point in the history
* chore(deps): bump pnpm version

* feat: cache home guild, report channel, private category

* feat: request a reason when reporting a message

* chore: fix lint issues (:
  • Loading branch information
Naomi Carrigan authored Jul 20, 2023
1 parent 056a462 commit 79fbfc0
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 64 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"engines": {
"node": "18.16.1",
"pnpm": "8.6.7"
"pnpm": "8.6.9"
},
"license": "MIT",
"dependencies": {
Expand Down
44 changes: 2 additions & 42 deletions src/commands/privateChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
CategoryChannel,
ChannelType,
GuildChannelCreateOptions,
PermissionFlagsBits,
Expand Down Expand Up @@ -41,50 +40,15 @@ export const privateChannel: PrivilegedCommand = {

const target = interaction.options.getUser("target", true);

let category = guild.channels.cache.find(
(c) =>
c.id === Bot.config.private_category &&
c.type === ChannelType.GuildCategory
);
if (!category) {
category = await guild.channels.create({
name: "Private Channel",
type: ChannelType.GuildCategory,
permissionOverwrites: [
{
id: guild.id,
deny: [
PermissionFlagsBits.ViewChannel,
PermissionFlagsBits.ReadMessageHistory,
PermissionFlagsBits.SendMessages,
],
},
{
id: modRole.id,
allow: [
PermissionFlagsBits.ViewChannel,
PermissionFlagsBits.ReadMessageHistory,
PermissionFlagsBits.SendMessages,
],
},
{
id: Bot.config.bot_id,
allow: [
PermissionFlagsBits.ViewChannel,
PermissionFlagsBits.ReadMessageHistory,
PermissionFlagsBits.SendMessages,
],
},
],
});
}
const category = Bot.privateCategory;

// Create channel
const channelName = `private-${target.username}`;

const channelOpts: GuildChannelCreateOptions = {
name: channelName,
type: ChannelType.GuildText,
parent: category,
permissionOverwrites: [
{
id: target.id,
Expand Down Expand Up @@ -125,10 +89,6 @@ export const privateChannel: PrivilegedCommand = {
],
};

if (category) {
channelOpts.parent = category as CategoryChannel;
}

const newChannel = await guild.channels.create(channelOpts);

await createLogFile(Bot, newChannel.id);
Expand Down
50 changes: 30 additions & 20 deletions src/contexts/report.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
Guild,
Message,
EmbedBuilder,
ApplicationCommandType,
ButtonBuilder,
ButtonStyle,
ActionRowBuilder,
TextInputBuilder,
TextInputStyle,
ModalBuilder,
} from "discord.js";

import { Context } from "../interfaces/Context";
Expand All @@ -18,31 +20,25 @@ export const report: Context = {
},
run: async (Bot, interaction) => {
try {
await interaction.deferReply({ ephemeral: true });
const guild = interaction.guild as Guild;
const guild = interaction.guild;
if (!guild) {
await interaction.editReply("You cannot report DM messages!");
await interaction.reply({
content: "You cannot report DM messages!",
ephemeral: true,
});
return;
}
const message = interaction.options.getMessage("message") as Message;

if (!message) {
await interaction.editReply(
"Something broke horribly. Please ping Naomi."
);
await interaction.reply({
content: `The message could not be loaded. Please try again. If the issue persists, ping <@!465650873650118659>`,
ephemeral: true,
});
return;
}

const reportChannel = await guild.channels
.fetch(Bot.config.report_channel)
.catch(() => null);

if (!reportChannel || !("send" in reportChannel)) {
await interaction.editReply(
"Something broke horribly. Please ping Naomi."
);
return;
}
const reportChannel = Bot.reportChannel;

const author = message.author;

Expand Down Expand Up @@ -79,10 +75,24 @@ export const report: Context = {
text: `ID: ${author.id}`,
});

await reportChannel.send({ embeds: [reportEmbed], components: [row] });
await interaction.editReply(
"This message has been flagged. Thanks for keeping the freeCodeCamp community safe!"
const log = await reportChannel.send({
embeds: [reportEmbed],
components: [row],
});

const reason = new TextInputBuilder()
.setLabel("Why are you reporting this message?")
.setCustomId("reason")
.setStyle(TextInputStyle.Paragraph)
.setRequired(true);
const modalRow = new ActionRowBuilder<TextInputBuilder>().addComponents(
reason
);
const modal = new ModalBuilder()
.setCustomId(`report-${log.id}`)
.setTitle("Message Report")
.addComponents(modalRow);
await interaction.showModal(modal);
} catch (err) {
await errorHandler(Bot, err);
}
Expand Down
29 changes: 29 additions & 0 deletions src/events/handlers/handleInteractionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,33 @@ export const handleInteractionCreate = async (
});
}
}

if (interaction.isModalSubmit()) {
if (interaction.customId.startsWith(`report-`)) {
const messageId = interaction.customId.split("-")[1];
const reportChannel = Bot.reportChannel;
const message = await reportChannel.messages.fetch(messageId);
const embed = message.embeds[0];
const reason = interaction.fields.getTextInputValue("reason");
await message.edit({
embeds: [
{
title: embed.title || "lost it oopsie",
description: embed.description || "lost it oopsie",
fields: [
...embed.fields,
{
name: "Reason",
value: reason,
},
],
},
],
});
await interaction.reply({
content: "Thank you for reporting!",
ephemeral: true,
});
}
}
};
45 changes: 45 additions & 0 deletions src/events/handlers/handleReady.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ChannelType } from "discord.js";

import { Camperbot } from "../../interfaces/Camperbot";
import { errorHandler } from "../../utils/errorHandler";

Expand All @@ -9,6 +11,49 @@ import { errorHandler } from "../../utils/errorHandler";
export const handleReady = async (Bot: Camperbot) => {
try {
await Bot.config.debug_hook.send("Bot Ready!");
const homeGuild = await Bot.guilds.fetch(Bot.config.home_guild);
if (!homeGuild) {
await Bot.config.debug_hook.send("The home guild could not be loaded.");
return;
}
if (!Bot.homeGuild) {
Bot.homeGuild = homeGuild;
}
const reportChannel = await homeGuild.channels.fetch(
Bot.config.report_channel
);
if (!reportChannel) {
await Bot.config.debug_hook.send(
"The report channel could not be loaded."
);
return;
}
if (!reportChannel.isTextBased()) {
await Bot.config.debug_hook.send("The report channel is not text based.");
return;
}
if (!Bot.reportChannel) {
Bot.reportChannel = reportChannel;
}
const privateCategory = await homeGuild.channels.fetch(
Bot.config.private_category
);
if (!privateCategory) {
await Bot.config.debug_hook.send(
"The private category could not be loaded."
);
return;
}
if (privateCategory.type !== ChannelType.GuildCategory) {
await Bot.config.debug_hook.send(
"The private category is not a category."
);
return;
}
if (!Bot.privateCategory) {
Bot.privateCategory = privateCategory;
}
await Bot.config.debug_hook.send("All channels loaded.");
} catch (err) {
await errorHandler(Bot, err);
}
Expand Down
12 changes: 11 additions & 1 deletion src/interfaces/Camperbot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { PrismaClient } from "@prisma/client";
import { Client, Snowflake, WebhookClient } from "discord.js";
import {
CategoryChannel,
Client,
Guild,
Snowflake,
TextBasedChannel,
WebhookClient,
} from "discord.js";

import { Context } from "./Context";
import { QuoteList } from "./Quotes";
Expand All @@ -23,4 +30,7 @@ export interface Camperbot extends Client {
contexts: Context[];
private_logs: { [key: string]: string };
db: PrismaClient;
homeGuild: Guild;
reportChannel: TextBasedChannel;
privateCategory: CategoryChannel;
}

0 comments on commit 79fbfc0

Please sign in to comment.