confoss/src/commands/setup.ts

184 lines
5.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
import {
ActionRowBuilder,
2024-10-17 19:15:52 -04:00
ButtonBuilder,
ButtonStyle,
2024-10-12 13:00:37 -04:00
ChannelSelectMenuBuilder,
ChannelType,
CommandInteraction,
ComponentType,
2024-10-12 15:26:28 -04:00
PermissionFlagsBits,
2024-10-13 11:06:08 -04:00
SlashCommandBuilder
2024-10-12 13:00:37 -04:00
} from "discord.js";
import { dt } from "../main";
import Logger from "../utils/Logger";
2024-10-23 15:57:03 -04:00
import { messageOpts } from "../constants";
2024-10-12 13:00:37 -04:00
2024-10-12 18:50:34 -04:00
const logger = new Logger("(/) setup");
2024-10-12 13:00:37 -04:00
export const data = new SlashCommandBuilder()
.setName("setup")
2024-10-12 15:26:28 -04:00
.setDescription("Setup the bot.")
.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild);
2024-10-12 13:00:37 -04:00
export async function execute(interaction: CommandInteraction) {
const { id: guildId } = interaction.guild!;
const { displayName: username } = interaction.user;
if (dt.checkSetup(guildId)) {
return interaction
.reply({
content: "This guild has already been set up!",
...messageOpts
})
.catch(err => logger.error("An error occured during setup:", err));
2024-10-12 13:00:37 -04:00
}
let confessChannel: string, logChannel: string;
const channelList = new ChannelSelectMenuBuilder()
.addChannelTypes(ChannelType.GuildText)
.setCustomId("channels")
.setPlaceholder("Choose a channel");
2024-10-17 19:15:52 -04:00
const skipButton = new ButtonBuilder()
.setCustomId("skipModChannel")
.setLabel("Skip")
.setStyle(ButtonStyle.Secondary);
2024-10-17 19:15:52 -04:00
2024-10-12 13:00:37 -04:00
const channelRow =
new ActionRowBuilder<ChannelSelectMenuBuilder>().addComponents(channelList);
const buttonRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
skipButton
);
2024-10-17 19:15:52 -04:00
2024-10-12 18:50:34 -04:00
try {
const response = await interaction.reply({
content: `# Let's get started, ${username}!\nFirst, let's choose a channel for your confessions.`,
2024-10-23 15:57:03 -04:00
...messageOpts,
2024-10-13 11:06:08 -04:00
components: [channelRow]
2024-10-12 13:00:37 -04:00
});
2024-10-12 18:50:34 -04:00
const collector = response.createMessageComponentCollector({
2024-10-12 13:00:37 -04:00
componentType: ComponentType.ChannelSelect,
2024-10-13 11:06:08 -04:00
time: 45_000
2024-10-12 13:00:37 -04:00
});
2024-10-13 11:06:08 -04:00
collector.on("collect", async i => {
[confessChannel] = i.values;
2024-10-12 13:00:37 -04:00
2024-10-12 18:50:34 -04:00
await i.update({
content: "Awesome!",
2024-10-13 11:06:08 -04:00
components: []
2024-10-12 13:00:37 -04:00
});
2024-10-12 18:50:34 -04:00
collector.stop();
const logChannelList = new ChannelSelectMenuBuilder()
.addChannelTypes(ChannelType.GuildText)
.setCustomId("logChannels")
.setPlaceholder("Choose a channel.");
const logChannelRow =
new ActionRowBuilder<ChannelSelectMenuBuilder>().addComponents(
2024-10-13 11:06:08 -04:00
logChannelList
2024-10-12 18:50:34 -04:00
);
const logResponse = await interaction.followUp({
content: "# Now, select a logging channel, for moderation purposes.",
2024-10-23 15:57:03 -04:00
...messageOpts,
2024-10-17 19:15:52 -04:00
components: [logChannelRow, buttonRow]
2024-10-12 18:50:34 -04:00
});
const logCollector = logResponse.createMessageComponentCollector({
componentType: ComponentType.ChannelSelect,
2024-10-13 11:06:08 -04:00
time: 45_000
2024-10-12 13:00:37 -04:00
});
2024-10-17 19:15:52 -04:00
const skipCollector = logResponse.createMessageComponentCollector({
componentType: ComponentType.Button,
time: 45_000
});
2024-10-17 19:15:52 -04:00
let skipped = false;
2024-10-13 11:06:08 -04:00
logCollector.on("collect", async ij => {
[logChannel] = ij.values;
2024-10-12 18:50:34 -04:00
await ij.update({
content: "Setup Complete!",
2024-10-13 11:06:08 -04:00
components: []
2024-10-12 18:50:34 -04:00
});
dt.setup(guildId, {
2024-10-12 18:50:34 -04:00
confessChannel: confessChannel,
modChannel: logChannel,
2024-10-13 11:06:08 -04:00
bans: []
2024-10-12 18:50:34 -04:00
});
logCollector.stop();
2024-10-17 19:15:52 -04:00
skipCollector.stop();
2024-10-12 18:50:34 -04:00
});
2024-10-17 19:15:52 -04:00
skipCollector.on("collect", async ik => {
if (ik.customId === "skipModChannel") {
await ik.update({
content: "Setup complete!",
components: []
});
2024-10-17 19:15:52 -04:00
dt.setup(guildId!, {
confessChannel: confessChannel,
bans: []
});
skipped = true;
logCollector.stop();
skipCollector.stop();
}
});
2024-10-17 19:15:52 -04:00
2024-10-13 11:06:08 -04:00
logCollector.on("end", content => {
2024-10-17 19:15:52 -04:00
// If there is no content and the channel hasn't been skipped, follow up with an error message.
!content.size &&
!skipped &&
2024-10-12 18:50:34 -04:00
interaction.followUp({
content: "No channel selected. Please try again.",
2024-10-23 15:57:03 -04:00
...messageOpts,
2024-10-13 11:06:08 -04:00
components: []
2024-10-12 18:50:34 -04:00
});
});
2024-10-12 13:00:37 -04:00
});
2024-10-13 11:06:08 -04:00
collector.on("end", collected => {
2024-10-12 18:50:34 -04:00
// Same as above logCollector end
!collected.size &&
2024-10-12 13:00:37 -04:00
interaction.followUp({
2024-10-12 18:50:34 -04:00
content: "No channel selected. Try again.",
2024-10-23 15:57:03 -04:00
...messageOpts,
2024-10-13 11:06:08 -04:00
components: []
2024-10-12 13:00:37 -04:00
});
});
2024-10-12 18:50:34 -04:00
} catch (err) {
2024-10-25 12:20:07 -04:00
logger.error("An error occured during setup:", err);
2024-10-12 18:50:34 -04:00
}
2024-10-12 13:00:37 -04:00
}