Skip to content

Commit

Permalink
chore: finish temp voice module
Browse files Browse the repository at this point in the history
  • Loading branch information
louiszn committed Oct 20, 2024
1 parent 14c8faf commit 6bc74bc
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 53 deletions.
Binary file modified bun.lockb
Binary file not shown.
109 changes: 76 additions & 33 deletions src/commands/modules/TempVoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ export default class extends Command {
.addChannelTypes(ChannelType.GuildVoice),
),
)
.addSubcommand((subcommand) =>
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")
Expand Down Expand Up @@ -192,6 +204,10 @@ export default class extends Command {
target: "setup",
permissions: [PermissionFlagsBits.ManageChannels],
},
{
name: "name",
target: "name",
},
{
name: "lock",
target: "lock",
Expand Down Expand Up @@ -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);
Expand All @@ -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,
);

Expand All @@ -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);
Expand All @@ -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,
);

Expand All @@ -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);
Expand All @@ -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,
);

Expand All @@ -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);
Expand All @@ -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,
);

Expand Down Expand Up @@ -548,7 +591,7 @@ export default class extends Command {
}

await tempVoice.updateOne({ ownerId: user.id });

const newChannelData = await modules.tempVoice.getChannelData(
user.id,
guildId,
Expand All @@ -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);
Expand Down Expand Up @@ -611,7 +654,7 @@ export default class extends Command {
const { channel } = member.voice;

const newChannelData = await modules.tempVoice.getChannelData(
user.id,
userConfig.userId,
guildId,
);

Expand All @@ -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);
Expand Down Expand Up @@ -667,7 +710,7 @@ export default class extends Command {
const { channel } = member.voice;

const newChannelData = await modules.tempVoice.getChannelData(
user.id,
userConfig.userId,
guildId,
);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -754,7 +797,7 @@ export default class extends Command {
const { channel } = member.voice;

const newChannelData = await modules.tempVoice.getChannelData(
user.id,
userConfig.userId,
guildId,
);

Expand All @@ -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);
Expand Down Expand Up @@ -810,7 +853,7 @@ export default class extends Command {
const { channel } = member.voice;

const newChannelData = await modules.tempVoice.getChannelData(
user.id,
userConfig.userId,
guildId,
);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -897,7 +940,7 @@ export default class extends Command {
const { channel } = member.voice;

const newChannelData = await modules.tempVoice.getChannelData(
user.id,
userConfig.userId,
guildId,
);

Expand All @@ -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);
Expand Down Expand Up @@ -955,7 +998,7 @@ export default class extends Command {
const { channel } = member.voice;

const newChannelData = await modules.tempVoice.getChannelData(
user.id,
userConfig.userId,
guildId,
);

Expand Down
6 changes: 3 additions & 3 deletions src/events/modules/TempVoice/VoiceStateUpdate.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand Down Expand Up @@ -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,
});
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
23 changes: 12 additions & 11 deletions src/managers/modules/TempVoiceManager.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<GuildChannelEditOptions | null> {
public async getChannelData(
userId: string,
guildId: string,
): Promise<GuildChannelEditOptions | null> {
const userConfig = await this.getUserConfig(userId);

if (!userConfig) {
Expand Down
10 changes: 5 additions & 5 deletions src/models/TempVoiceConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] },
}),
);

0 comments on commit 6bc74bc

Please sign in to comment.