From 83bc69b0f47fe229c98f797e5aef2f385c01146b Mon Sep 17 00:00:00 2001 From: jbcarreon123 Date: Tue, 20 Feb 2024 09:10:19 +0800 Subject: [PATCH] all: idk --- src/bot/commands/misc/button-test.ts | 33 +++++++++++++++++++++++ src/bot/commands/misc/configure.ts | 28 +++++++++++++++---- src/bot/components/buttons/test-button.ts | 6 ++--- src/bot/database/schema.ts | 7 ++--- src/bot/events/channelDelete.ts | 19 +++++++++++++ src/bot/events/voiceStateUpdate.ts | 26 +++++++++++++----- src/bot/index.ts | 6 ++--- src/templates/selectmenu.ts | 25 +++++++++++++++++ 8 files changed, 129 insertions(+), 21 deletions(-) create mode 100644 src/bot/commands/misc/button-test.ts create mode 100644 src/bot/events/channelDelete.ts create mode 100644 src/templates/selectmenu.ts diff --git a/src/bot/commands/misc/button-test.ts b/src/bot/commands/misc/button-test.ts new file mode 100644 index 0000000..353d22d --- /dev/null +++ b/src/bot/commands/misc/button-test.ts @@ -0,0 +1,33 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +const { + SlashCommandBuilder, + EmbedBuilder, + Interaction, + ActionRowBuilder, +} = require("discord.js"); +const { config } = require("../../config"); +const logger = require("../../../utils/logger"); +const { data } = require("../../components/buttons/test-button.ts") + +module.exports = { + data: new SlashCommandBuilder() + .setName("button-test") + .setDescription(`temp command`), + async execute(interaction: typeof Interaction) { + // Grab client latency + const latency = Math.abs(Date.now() - interaction.createdAt); + + // Row + const row = new ActionRowBuilder().addComponents(data) + + // Reply + await interaction.reply({ content: "test", components: [row] }); + + logger.info(`Res ping -> latency of ${latency}ms`); + }, +}; diff --git a/src/bot/commands/misc/configure.ts b/src/bot/commands/misc/configure.ts index 1aae50c..c036009 100644 --- a/src/bot/commands/misc/configure.ts +++ b/src/bot/commands/misc/configure.ts @@ -18,7 +18,7 @@ const { GuildConfig } = require('../../database/schema'); module.exports = { data: new SlashCommandBuilder() .setName("configure") - .setDescription(`Database test`) + .setDescription("Configure Liberation") .addChannelOption(option => option.setName('channel') .setDescription('The channel for the temp vc thing') @@ -27,9 +27,10 @@ module.exports = { .addChannelOption(option => option.setName('category') .setDescription('The category for the temp vc thing') - .setRequired(true) - .addChannelTypes(ChannelType.GuildCategory)) - .setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild), + .addChannelTypes(ChannelType.GuildCategory) + .setRequired(true)) + .setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild) + .setDMPermission(false), async execute(interaction: typeof Interaction) { const channel = interaction.options.getChannel('channel') const category = interaction.options.getChannel('category') @@ -42,6 +43,23 @@ module.exports = { guildConfig.save(); - await interaction.reply('done'); + const configureReply = new EmbedBuilder() + .setColor(config.colors.primary) + .setTitle("Liberation configured!") + .addFields([ + { + name: "Create a Temp VC channel", + value: `${channel}`, + inline: true + }, + { + name: "Temp VC category", + value: `${category}`, + inline: true + } + ]); + + + await interaction.reply({ embeds: [ configureReply ], ephemeral: true }); }, }; diff --git a/src/bot/components/buttons/test-button.ts b/src/bot/components/buttons/test-button.ts index 7818915..df2897d 100644 --- a/src/bot/components/buttons/test-button.ts +++ b/src/bot/components/buttons/test-button.ts @@ -2,11 +2,11 @@ const { ButtonBuilder, ButtonStyle, Interaction } = require("discord.js"); const config = require("../../config.ts"); module.exports = { - button: new ButtonBuilder() + data: new ButtonBuilder() .setLabel("Poke") .setCustomId("test-button") - .setStyle(ButtonStyle), + .setStyle(ButtonStyle.Primary), async execute(interaction: typeof Interaction) { - await interaction.reply("hello"); + await interaction.update("hello"); }, }; diff --git a/src/bot/database/schema.ts b/src/bot/database/schema.ts index 208ee60..8f974ec 100644 --- a/src/bot/database/schema.ts +++ b/src/bot/database/schema.ts @@ -3,13 +3,13 @@ import * as mongoose from "mongoose"; const guildConfigSchema = new mongoose.Schema( { guildId: {type: String, required: true}, - tempVoiceEnabled: {type: Boolean, required: true}, + // tempVoiceEnabled: {type: Boolean, required: true}, tempVoiceChannelId: {type: String}, tempVoiceCategoryId: {type: String}, - tempTextEnabled: {type: Boolean, required: true}, + // tempTextEnabled: {type: Boolean, required: true}, tempTextChannelId: {type: String}, tempTextCategoryId: {type: String}, - ticketsEnabled: {type: Boolean, required: true}, + // ticketsEnabled: {type: Boolean, required: true}, ticketsMessageId: {type: String}, ticketsCategoryId: {type: String}, } @@ -36,6 +36,7 @@ const tempTextSchema = new mongoose.Schema( const ticketSchema = new mongoose.Schema( { ticketId: {type: String}, + isClosed: {type: Boolean, required: true}, guildId: {type: String, required: true}, ticketChannelId: {type: String, required: true}, ownerId: {type: String, required: true} diff --git a/src/bot/events/channelDelete.ts b/src/bot/events/channelDelete.ts new file mode 100644 index 0000000..4ab4cbc --- /dev/null +++ b/src/bot/events/channelDelete.ts @@ -0,0 +1,19 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +const { Events, VoiceChannel } = require("discord.js"); +const { config } = require("../config"); +const { GuildConfig, TempVoice } = require("../database/schema") +const logger = require("../../utils/logger"); + +module.exports = { + name: Events.ChannelDelete, + async execute(channel: typeof VoiceChannel) { + await TempVoice.deleteMany({ voiceChannelId: channel.id }); + + logger.info(`Voice channel deleted. ID: ${channel.id}`); + }, +}; diff --git a/src/bot/events/voiceStateUpdate.ts b/src/bot/events/voiceStateUpdate.ts index d95f7f9..40aeb20 100644 --- a/src/bot/events/voiceStateUpdate.ts +++ b/src/bot/events/voiceStateUpdate.ts @@ -17,13 +17,12 @@ module.exports = { `${member.user.username} joined ${newState.channel.name}` ); - // TODO: add party creation logic GuildConfig.find({ guildId: guild.id }) .then(async res => { - if (res) { + if (res) { const pGuild = res[0] - if (pGuild.tempVoiceChannelId == newState.channelId) { + if (pGuild && pGuild.tempVoiceChannelId == newState.channelId) { const channel = await guild.channels.create({ name: `${member.user.username}'s party`, type: ChannelType.GuildVoice }) await channel.setParent(pGuild.tempVoiceCategoryId) @@ -35,19 +34,32 @@ module.exports = { tempVoice.save() - member.voice.setChannel(channel, 'Created party VC') + await member.voice.setChannel(channel, 'Created party VC') + .catch(error => { + logger.warn(`Error occured while switching user to party VC: ${error}`) + }); } - } + } }) } - // Left - if (oldState.channelId && !newState.channelId) { + // Left & Switched + if (oldState.channelId) { logger.info( `${member.user.username} left ${oldState.channel.name}` ); // TODO: add party deletion logic + TempVoice.find({ voiceChannelId: oldState.channelId }) + .then(async res => { + if (res) { + if (res[0] && res[0].voiceChannelId === oldState.channelId) { + if (oldState.channel.members.size < 1) { + await oldState.channel.delete(); + } + } + } + }) } }, }; diff --git a/src/bot/index.ts b/src/bot/index.ts index a1721d0..b79206c 100644 --- a/src/bot/index.ts +++ b/src/bot/index.ts @@ -56,13 +56,13 @@ for (const file of buttonFiles) { // Load select menus logger.info("Loading select menus"); -client.buttons = new Map(); +client.selectMenus = new Map(); const selectMenusPath = path.join(import.meta.dir, "./components/selectmenus"); const selectMenuFiles = fs - .readdirSync(buttonsPath) + .readdirSync(selectMenusPath) .filter((file: string) => file.endsWith(".ts")); -for (const file of buttonFiles) { +for (const file of selectMenuFiles) { const selectMenu = require(`${selectMenusPath}/${file}`); client.selectMenus.set(selectMenu.data.customId, selectMenu); } diff --git a/src/templates/selectmenu.ts b/src/templates/selectmenu.ts new file mode 100644 index 0000000..735d88b --- /dev/null +++ b/src/templates/selectmenu.ts @@ -0,0 +1,25 @@ +const { EmbedBuilder, Interaction, StringSelectMenuBuilder, StringSelectMenuOptionBuilder } = require("discord.js"); +const config = require("../../config.ts"); + +module.exports = { + selectMenu: () => { + const select = new StringSelectMenuBuilder() + .setCustomId('') + .setPlaceholder('') + .addOptions( + new StringSelectMenuOptionBuilder() + .setLabel('') + .setDescription('') + .setValue(''), + new StringSelectMenuOptionBuilder() + .setLabel('') + .setDescription('') + .setValue(''), + ); + + return select + }, + async execute(interaction: typeof Interaction) { + // Logic + }, +};