confoss/src/main.ts

210 lines
6.1 KiB
TypeScript
Raw Normal View History

2024-10-12 13:00:37 -04:00
/*
* Confoss: Anonymous confessions for Discord, free as in freedom and price!
* Copyright (C) 2024 powermaker450
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2024-10-12 18:51:09 -04:00
*/
2024-10-12 13:00:37 -04:00
2024-10-13 13:22:49 -04:00
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
ComponentType,
EmbedBuilder,
Events,
Interaction,
ModalSubmitInteraction,
TextChannel
} from "discord.js";
2024-10-12 13:00:37 -04:00
import { BotClient, BOT_TOKEN, deployCommands } from "./bot";
import { commands } from "./commands";
import { StoreMan } from "./storeman";
import Logger from "./utils/Logger";
import getRandomColor from "./utils/getRandomColor";
import { submit } from "./modals";
2024-10-12 13:00:37 -04:00
export const dt = new StoreMan(StoreMan.checkFile());
const logger = new Logger("Main");
2024-10-13 11:06:08 -04:00
BotClient.once("ready", client => {
2024-10-12 13:00:37 -04:00
logger.log(`We're ready! Logged in as ${client.user.tag}`);
});
// Deploy the commands for a new guild
2024-10-13 11:06:08 -04:00
BotClient.on("guildCreate", async guild => {
2024-10-12 13:00:37 -04:00
await deployCommands({ guildId: guild.id });
});
// Delete the data for a guild after it is removed
2024-10-13 11:06:08 -04:00
BotClient.on("guildDelete", guild => {
2024-10-12 13:00:37 -04:00
logger.log(`${guild.name} didn't want us anymore... :(`);
dt.clearSettings(guild.id);
});
BotClient.on(Events.InteractionCreate, async interaction => {
2024-10-12 13:00:37 -04:00
if (!interaction.isCommand()) {
return;
}
const { commandName } = interaction;
if (commands[commandName as keyof typeof commands]) {
commands[commandName as keyof typeof commands].execute(interaction);
}
});
BotClient.on(Events.MessageDelete, async message => {
2024-10-13 15:33:33 -04:00
const guildId = message.guild?.id!;
const messageId = message.id;
const confessions = dt.getGuildInfo(guildId)?.confessions!;
for (const confession of confessions) {
2024-10-13 15:33:33 -04:00
if (confession.messageId === messageId) {
dt.adminDelConfession(guildId, confession.id);
}
}
});
BotClient.on(Events.InteractionCreate, async interaction => {
if (!interaction.isModalSubmit()) {
return;
}
if (interaction.customId === "submitConfession") {
2024-10-13 13:22:49 -04:00
const messageContent: string =
interaction.fields.getTextInputValue("confessionInput");
const attachment: string = interaction.fields.getTextInputValue(
"confessionAttachment"
);
try {
if (dt.isBanned(interaction.guild?.id!, interaction.user.id)) {
return interaction.reply({
content: "You are banned from confessions in this server!",
ephemeral: true
});
}
if (!dt.getGuildInfo(interaction.guild?.id!)) {
return interaction.reply({
content:
"The bot hasn't been set up yet! Ask the server admins to set it up.",
ephemeral: true
});
}
const confessChannel = dt.getGuildInfo(interaction.guild?.id!)?.settings
.confessChannel;
const adminChannel = dt.getGuildInfo(interaction.guild?.id!)?.settings
.modChannel;
2024-10-13 13:22:49 -04:00
const isAttachment = (text: string) =>
text && (text.startsWith("http://") || text.startsWith("https://"));
const color = getRandomColor();
const messageId = StoreMan.genId();
const userConfessionEmbed = new EmbedBuilder()
.setColor(color)
.setTitle(`Anonymous Confession \`${messageId}\``)
// @ts-ignore
.setDescription(messageContent);
2024-10-13 13:22:49 -04:00
isAttachment(attachment) && userConfessionEmbed.setImage(attachment);
const adminConfessionEmbed = new EmbedBuilder()
.setColor(color)
.setTitle(`Anonymous Confession \`${messageId}\``)
// @ts-ignore
.setDescription(messageContent)
.addFields(
{
name: "Author",
value: interaction.user.displayName
},
{
name: "Author ID",
value: interaction.user.id
}
);
2024-10-13 13:22:49 -04:00
isAttachment(attachment) && adminConfessionEmbed.setImage(attachment);
const submitConfessionButton = new ButtonBuilder()
.setCustomId("submitConfession")
.setLabel("Submit a Confession")
.setStyle(ButtonStyle.Primary);
2024-10-13 13:22:49 -04:00
const actionRow = new ActionRowBuilder<ButtonBuilder>().setComponents(
submitConfessionButton
);
const message = await (
BotClient.channels.cache.get(confessChannel!) as TextChannel
).send({
embeds: [userConfessionEmbed],
components: [actionRow]
});
2024-10-13 13:22:49 -04:00
const collector = message.createMessageComponentCollector({
componentType: ComponentType.Button
});
collector.on("collect", i => {
if (i.customId === "submitConfession") {
i.showModal(submit);
}
});
await (BotClient.channels.cache.get(adminChannel!) as TextChannel).send({
embeds: [adminConfessionEmbed]
});
dt.addConfession(
message,
messageId,
interaction.user.displayName,
interaction.user.id,
2024-10-13 13:22:49 -04:00
messageContent,
attachment
);
2024-10-13 13:22:49 -04:00
const confessionsLength = dt.getGuildInfo(interaction.guild?.id!)
?.confessions.length!;
if (confessionsLength >= 2) {
2024-10-13 13:22:49 -04:00
await (
BotClient.channels.cache.get(confessChannel!) as TextChannel
).messages
.fetch(
dt.getGuildInfo(interaction.guild?.id!)?.confessions[
confessionsLength - 2
].messageId!
)
.then(message => {
message.edit({ components: [] });
});
}
return interaction.reply({
content: "Confession sent!",
ephemeral: true
});
} catch (err) {
logger.error("An error occured:", err);
}
}
2024-10-13 13:22:49 -04:00
});
2024-10-12 13:00:37 -04:00
BotClient.login(BOT_TOKEN);