From 6bc74bc7d4cf0a0272ade500ae5c46979ed49125 Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 21 Oct 2024 00:24:44 +0700 Subject: [PATCH] chore: finish temp voice module --- bun.lockb | Bin 24169 -> 24169 bytes src/commands/modules/TempVoice.ts | 109 ++++++++++++------ .../modules/TempVoice/VoiceStateUpdate.ts | 6 +- src/index.ts | 6 +- src/managers/modules/TempVoiceManager.ts | 23 ++-- src/models/TempVoiceConfig.ts | 10 +- 6 files changed, 101 insertions(+), 53 deletions(-) diff --git a/bun.lockb b/bun.lockb index 33906d4adb344d410d9d68929b85676286537554..07ade839f1ec3fd32b64b22712ee8810f31f113a 100644 GIT binary patch delta 22 ccmaF4hw + subcommand + .setName("name") + .setDescription("Đổi tên kênh hiện tại") + .addStringOption((option) => + option + .setName("name") + .setDescription("Tên kênh mà cậu muốn đổi") + .setRequired(true) + .setMaxLength(15), + ), + ) .addSubcommand((subcommand) => subcommand .setName("lock") @@ -192,6 +204,10 @@ export default class extends Command { target: "setup", permissions: [PermissionFlagsBits.ManageChannels], }, + { + name: "name", + target: "name", + }, { name: "lock", target: "lock", @@ -364,8 +380,8 @@ export default class extends Command { }; } - protected async _lock(interaction: Command.ChatInput) { - const { member, client, user, guildId } = interaction; + protected async _name(interaction: Command.ChatInput) { + const { member, client, guildId, options } = interaction; const { modules, config } = client; const data = await this.validate(interaction); @@ -375,15 +391,48 @@ export default class extends Command { } const { userConfig } = data; + const name = options.getString("name"); + + await userConfig.updateOne({ name }); + + const { channel } = member.voice; - await userConfig?.updateOne({ - lock: true, + const newChannelData = await modules.tempVoice.getChannelData( + userConfig.userId, + guildId, + ); + + if (newChannelData) { + await channel!.edit(newChannelData); + } + + await interaction.reply({ + embeds: [ + new EmbedBuilder() + .setDescription(`✏️ Đã đổi tên kênh thành công!`) + .setColor(config.colors.default), + ], }); + } + + protected async _lock(interaction: Command.ChatInput) { + const { member, client, guildId } = interaction; + const { modules, config } = client; + + const data = await this.validate(interaction); + + if (!data) { + return; + } + + const { userConfig } = data; + + await userConfig.updateOne({ lock: true }); const { channel } = member.voice; const newChannelData = await modules.tempVoice.getChannelData( - user.id, + userConfig.userId, guildId, ); @@ -401,7 +450,7 @@ export default class extends Command { } protected async _unlock(interaction: Command.ChatInput) { - const { member, client, user, guildId } = interaction; + const { member, client, guildId } = interaction; const { modules, config } = client; const data = await this.validate(interaction); @@ -412,14 +461,12 @@ export default class extends Command { const { userConfig } = data; - await userConfig.updateOne({ - lock: false, - }); + await userConfig.updateOne({ lock: false }); const { channel } = member.voice; const newChannelData = await modules.tempVoice.getChannelData( - user.id, + userConfig.userId, guildId, ); @@ -437,7 +484,7 @@ export default class extends Command { } protected async _hide(interaction: Command.ChatInput) { - const { member, client, user, guildId } = interaction; + const { member, client, guildId } = interaction; const { modules, config } = client; const data = await this.validate(interaction); @@ -448,14 +495,12 @@ export default class extends Command { const { userConfig } = data; - await userConfig.updateOne({ - hide: true, - }); + await userConfig.updateOne({ hide: true }); const { channel } = member.voice; const newChannelData = await modules.tempVoice.getChannelData( - user.id, + userConfig.userId, guildId, ); @@ -473,7 +518,7 @@ export default class extends Command { } protected async _unhide(interaction: Command.ChatInput) { - const { member, client, user, guildId } = interaction; + const { member, client, guildId } = interaction; const { modules, config } = client; const data = await this.validate(interaction); @@ -484,14 +529,12 @@ export default class extends Command { const { userConfig } = data; - await userConfig.updateOne({ - hide: false, - }); + await userConfig.updateOne({ hide: false }); const { channel } = member.voice; const newChannelData = await modules.tempVoice.getChannelData( - user.id, + userConfig.userId, guildId, ); @@ -548,7 +591,7 @@ export default class extends Command { } await tempVoice.updateOne({ ownerId: user.id }); - + const newChannelData = await modules.tempVoice.getChannelData( user.id, guildId, @@ -568,7 +611,7 @@ export default class extends Command { } protected async _whitelist_add(interaction: Command.ChatInput) { - const { member, client, user, guildId, options } = interaction; + const { member, client, guildId, options } = interaction; const { modules, config } = client; const data = await this.validate(interaction); @@ -611,7 +654,7 @@ export default class extends Command { const { channel } = member.voice; const newChannelData = await modules.tempVoice.getChannelData( - user.id, + userConfig.userId, guildId, ); @@ -629,7 +672,7 @@ export default class extends Command { } protected async _whitelist_remove(interaction: Command.ChatInput) { - const { member, client, user, guildId, options } = interaction; + const { member, client, guildId, options } = interaction; const { modules, config } = client; const data = await this.validate(interaction); @@ -667,7 +710,7 @@ export default class extends Command { const { channel } = member.voice; const newChannelData = await modules.tempVoice.getChannelData( - user.id, + userConfig.userId, guildId, ); @@ -711,7 +754,7 @@ export default class extends Command { } protected async _blacklist_add(interaction: Command.ChatInput) { - const { member, client, user, guildId, options } = interaction; + const { member, client, guildId, options } = interaction; const { modules, config } = client; const data = await this.validate(interaction); @@ -754,7 +797,7 @@ export default class extends Command { const { channel } = member.voice; const newChannelData = await modules.tempVoice.getChannelData( - user.id, + userConfig.userId, guildId, ); @@ -772,7 +815,7 @@ export default class extends Command { } protected async _blacklist_remove(interaction: Command.ChatInput) { - const { member, client, user, guildId, options } = interaction; + const { member, client, guildId, options } = interaction; const { modules, config } = client; const data = await this.validate(interaction); @@ -810,7 +853,7 @@ export default class extends Command { const { channel } = member.voice; const newChannelData = await modules.tempVoice.getChannelData( - user.id, + userConfig.userId, guildId, ); @@ -854,7 +897,7 @@ export default class extends Command { } protected async _manager_add(interaction: Command.ChatInput) { - const { member, client, user, guildId, options } = interaction; + const { member, client, guildId, options } = interaction; const { modules, config } = client; const data = await this.validate(interaction); @@ -897,7 +940,7 @@ export default class extends Command { const { channel } = member.voice; const newChannelData = await modules.tempVoice.getChannelData( - user.id, + userConfig.userId, guildId, ); @@ -917,7 +960,7 @@ export default class extends Command { } protected async _manager_remove(interaction: Command.ChatInput) { - const { member, client, user, guildId, options } = interaction; + const { member, client, guildId, options } = interaction; const { modules, config } = client; const data = await this.validate(interaction); @@ -955,7 +998,7 @@ export default class extends Command { const { channel } = member.voice; const newChannelData = await modules.tempVoice.getChannelData( - user.id, + userConfig.userId, guildId, ); diff --git a/src/events/modules/TempVoice/VoiceStateUpdate.ts b/src/events/modules/TempVoice/VoiceStateUpdate.ts index 32f4943..dec7d2d 100644 --- a/src/events/modules/TempVoice/VoiceStateUpdate.ts +++ b/src/events/modules/TempVoice/VoiceStateUpdate.ts @@ -1,4 +1,4 @@ -import { ChannelType, PermissionFlagsBits, type VoiceState } from "discord.js"; +import { ChannelType, type GuildChannelCreateOptions, type VoiceState } from "discord.js"; import Listener from "../../Listener"; import TempVoiceCreator from "../../../models/TempVoiceCreator"; @@ -23,7 +23,7 @@ export default class extends Listener { private async createVoiceChannel(state: VoiceState) { const { member, channel, guild, client } = state; - const { redis } = client; + const { redis, modules } = client; const creator = await TempVoiceCreator.findOne({ channelId: channel?.id, @@ -60,7 +60,7 @@ export default class extends Listener { await redis.set(cooldownKey, "", "EX", 15); const tempChannel = await state.guild.channels.create({ - name: `${user.username}`, + ...await modules.tempVoice.getChannelData(user.id, guild.id) as GuildChannelCreateOptions, type: ChannelType.GuildVoice, parent: channel.parent, }); diff --git a/src/index.ts b/src/index.ts index 938acc2..dc89d35 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,17 @@ import { Client, GatewayIntentBits } from "discord.js"; -import { loadCommands, loadEvents } from "./utils/loader"; + import Redis from "ioredis"; import mongoose from "mongoose"; +import { loadCommands, loadEvents } from "./utils/loader"; + import CommandManager from "./managers/CommandManager"; import config from "./config"; import ModuleManager from "./managers/ModuleManager"; +process.on("uncaughtException", console.error); + const client = new Client({ intents: [ GatewayIntentBits.Guilds, diff --git a/src/managers/modules/TempVoiceManager.ts b/src/managers/modules/TempVoiceManager.ts index d938741..89ff412 100644 --- a/src/managers/modules/TempVoiceManager.ts +++ b/src/managers/modules/TempVoiceManager.ts @@ -1,4 +1,10 @@ -import { BaseGuildVoiceChannel, Client, PermissionFlagsBits, VoiceChannel, type GuildChannelEditOptions, type OverwriteData } from "discord.js"; +import { + Client, + PermissionFlagsBits, + type GuildChannelEditOptions, + type OverwriteData, +} from "discord.js"; + import TempVoiceConfig from "../../models/TempVoiceConfig"; export default class TempVoiceManager { @@ -18,22 +24,17 @@ export default class TempVoiceManager { let userConfig = await TempVoiceConfig.findOne({ userId }); if (!userConfig) { - userConfig = new TempVoiceConfig({ - userId, - lock: false, - hide: false, - whitelisted: [], - blacklisted: [], - managers: [], - }); - + userConfig = new TempVoiceConfig({ userId }); await userConfig.save(); } return userConfig; } - public async getChannelData(userId: string, guildId: string): Promise { + public async getChannelData( + userId: string, + guildId: string, + ): Promise { const userConfig = await this.getUserConfig(userId); if (!userConfig) { diff --git a/src/models/TempVoiceConfig.ts b/src/models/TempVoiceConfig.ts index 8737c92..0e09ffa 100644 --- a/src/models/TempVoiceConfig.ts +++ b/src/models/TempVoiceConfig.ts @@ -5,10 +5,10 @@ export default model( new Schema({ userId: { type: String, required: true }, name: { type: String, required: false }, - lock: { type: Boolean, required: true }, - hide: { type: Boolean, required: true }, - whitelisted: { type: [String], required: true }, - blacklisted: { type: [String], required: true }, - managers: { type: [String], required: true }, + lock: { type: Boolean, default: false }, + hide: { type: Boolean, default: false }, + whitelisted: { type: [String], default: [] }, + blacklisted: { type: [String], default: [] }, + managers: { type: [String], default: [] }, }), );