Skip to content

Commit 46032d9

Browse files
author
xyzjesper
committed
Fixed tempVoice remove and delete from VC Channels
1 parent b2d0b73 commit 46032d9

File tree

4 files changed

+102
-78
lines changed

4 files changed

+102
-78
lines changed
Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,72 @@
1-
import { ButtonStyle, ChatInputCommandInteraction, MessageFlags, PermissionFlagsBits } from "discord.js";
2-
import { ExtendedClient } from "../../../../types/client.js";
3-
import { convertToEmojiPng } from "../../../../helper/emojis.js";
4-
import { PermissionType } from "../../../../enums/permissionType.js";
5-
import { database } from "../../../../main/database.js";
1+
import {ButtonStyle, ChatInputCommandInteraction, MessageFlags, PermissionFlagsBits} from "discord.js";
2+
import {ExtendedClient} from "../../../../types/client.js";
3+
import {convertToEmojiPng} from "../../../../helper/emojis.js";
4+
import {PermissionType} from "../../../../enums/permissionType.js";
5+
import {database} from "../../../../main/database.js";
66

77
export default {
8-
subCommand: "tempvoice.remove", options: {
9-
once: false,
10-
permission: PermissionType.TempVoice,
11-
cooldown: 3000,
12-
botPermissions: [PermissionFlagsBits.ManageChannels, PermissionFlagsBits.SendMessages, PermissionFlagsBits.ViewChannel],
13-
userPermissions: [PermissionFlagsBits.ManageGuild],
14-
userHasOnePermission: true,
15-
isGuildOwner: false,
16-
},
17-
/**
18-
*
19-
* @param {ChatInputCommandInteraction} interaction
20-
* @param {ExtendedClient} client
21-
*/
22-
async execute(
23-
interaction: ChatInputCommandInteraction,
24-
client: ExtendedClient
25-
) {
26-
await interaction.deferReply({
27-
flags: MessageFlags.Ephemeral
28-
});
29-
if (!client.user) throw new Error("Client user not found");
30-
if (!interaction.guild) throw new Error("Guild not found");
31-
if (!interaction.member) throw new Error("Member not found");
8+
subCommand: "tempvoice.remove", options: {
9+
once: false,
10+
permission: PermissionType.TempVoice,
11+
cooldown: 3000,
12+
botPermissions: [PermissionFlagsBits.ManageChannels, PermissionFlagsBits.SendMessages, PermissionFlagsBits.ViewChannel],
13+
userPermissions: [PermissionFlagsBits.ManageGuild],
14+
userHasOnePermission: true,
15+
isGuildOwner: false,
16+
},
17+
/**
18+
*
19+
* @param {ChatInputCommandInteraction} interaction
20+
* @param {ExtendedClient} client
21+
*/
22+
async execute(
23+
interaction: ChatInputCommandInteraction,
24+
client: ExtendedClient
25+
) {
26+
await interaction.deferReply({
27+
flags: MessageFlags.Ephemeral
28+
});
29+
if (!client.user) throw new Error("Client user not found");
30+
if (!interaction.guild) throw new Error("Guild not found");
31+
if (!interaction.member) throw new Error("Member not found");
3232

33-
const channel = interaction.options.getChannel("channel")?.id;
33+
const channel = interaction.options.getChannel("channel")?.id;
3434

35-
await database.tempVoices
36-
.deleteMany({
37-
where: {
38-
JointoCreateChannel: channel
35+
const data = await database.tempVoices
36+
.findFirst({
37+
include: {
38+
TempVoiceChannels: true
39+
},
40+
where: {
41+
JointoCreateChannel: channel
42+
}
43+
})
44+
45+
for (const channels of data.TempVoiceChannels) {
46+
const tempVoiceChannel = await interaction.guild.channels.fetch(channels.ChannelId)
47+
await tempVoiceChannel.delete()
48+
await interaction.editReply({
49+
content: `## ${await convertToEmojiPng("check", client.user?.id)} Deleted old TempVC Channel <#${channels.ChannelId}>!`
50+
});
3951
}
40-
})
41-
.then(async () => {
42-
if (!client.user) throw new Error("Client user not found");
43-
interaction.editReply({
44-
content: `## ${await convertToEmojiPng("check", client.user?.id)} You have removed the Tempvoice Channel`
45-
});
46-
});
47-
}
52+
53+
await database.tempVoiceChannels
54+
.deleteMany({
55+
where: {
56+
TempVoiceId: data.UUID
57+
}
58+
}).then(async (result) => {
59+
await database.tempVoices
60+
.deleteMany({
61+
where: {
62+
JointoCreateChannel: channel
63+
}
64+
})
65+
66+
if (!client.user) throw new Error("Client user not found");
67+
await interaction.editReply({
68+
content: `## ${await convertToEmojiPng("check", client.user?.id)} You have removed the Tempvoice Channel`
69+
});
70+
})
71+
}
4872
};

src/modules/tempvoice/events/tempVoice.ts

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,29 @@ export default {
2424
newState: VoiceState,
2525
client: ExtendedClient
2626
) {
27-
2827
const {guild, member} = newState;
28+
const oldChannel = oldState.channel;
29+
const newChannel = newState.channel;
30+
31+
const data = await database.tempVoiceChannels.findFirst({
32+
where: {
33+
ChannelId: oldChannel?.id
34+
}
35+
});
36+
if (data &&
37+
data?.ChannelId &&
38+
oldChannel?.id == data.ChannelId &&
39+
oldChannel.members.size == 0 &&
40+
(!newChannel || newChannel.id !== data.ChannelId)
41+
) {
42+
oldChannel.delete("Your Channel Deleted").catch((error) => {
43+
});
44+
await database.tempVoiceChannels.deleteMany({
45+
where: {
46+
OwnerId: oldState.member?.id
47+
}
48+
});
49+
}
2950

3051
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
3152
new ButtonBuilder()
@@ -74,24 +95,23 @@ export default {
7495
.setStyle(ButtonStyle.Secondary)
7596
);
7697

77-
const oldChannel = oldState.channel;
78-
const newChannel = newState.channel;
79-
if (oldChannel === newChannel) return;
80-
8198
const vcdata = await database.tempVoices.findMany({
8299
where: {
83100
GuildId: guild.id
84101
}
85102
});
86-
if (!vcdata) return;
87-
if (vcdata.length <= 0) return;
88103

89-
vcdata.forEach(async (jointocreateDocument) => {
90-
if (newState.channelId == jointocreateDocument.JointoCreateChannel) {
104+
105+
for (const jointocreateDocument of vcdata) {
106+
if (newState.channelId == jointocreateDocument?.JointoCreateChannel) {
91107
const parent = guild.channels.cache.get(
92108
jointocreateDocument.JointoCreateCategory as string
93109
);
94110

111+
if (!vcdata) return;
112+
if (vcdata.length <= 0) return;
113+
if (oldChannel == newChannel) return;
114+
95115
// Add Placeholder like {count} to the name
96116
const name = jointocreateDocument.Name
97117
? jointocreateDocument.Name
@@ -130,7 +150,7 @@ export default {
130150
]
131151
})
132152
.then(async (channel) => {
133-
member?.voice.setChannel(channel);
153+
await member?.voice.setChannel(channel);
134154

135155
if (!client.user) throw new Error("Client user is not defined");
136156

@@ -221,7 +241,7 @@ export default {
221241
});
222242
}
223243

224-
return await database.tempVoiceChannels.create({
244+
await database.tempVoiceChannels.create({
225245
data: {
226246
GuildId: guild.id,
227247
ChannelId: channel.id,
@@ -231,28 +251,6 @@ export default {
231251
});
232252
});
233253
}
234-
});
235-
236-
const data = await database.tempVoiceChannels.findFirst({
237-
where: {
238-
ChannelId: oldChannel?.id
239-
}
240-
});
241-
if (!data) return;
242-
243-
if (
244-
data.ChannelId &&
245-
oldChannel?.id == data.ChannelId &&
246-
oldChannel.members.size == 0 &&
247-
(!newChannel || newChannel.id !== data.ChannelId)
248-
) {
249-
oldChannel.delete("Your Channel Deleted").catch((error) => {
250-
});
251-
await database.tempVoiceChannels.deleteMany({
252-
where: {
253-
OwnerId: oldState.member?.id
254-
}
255-
});
256254
}
257255
},
258256

src/modules/utility/buttons/utility-export-guild.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ export default {
115115
.setContent(`## ${await convertToEmojiPng("export", client.user.id)} Download your GuildData Export ${new Date().toDateString()}`)
116116
)
117117
.addFileComponents(
118-
new FileBuilder().setURL(`attachment://UserData-${interaction.user.displayName}.json`).setSpoiler(true)
118+
new FileBuilder().setURL(`attachment://GuildData-${interaction.guild.name}.json`).setSpoiler(true)
119119
)
120120
],
121121
files: [
122-
new AttachmentBuilder(Buffer.from(string)).setName(`UserData-${interaction.user.displayName}.json`),
122+
new AttachmentBuilder(Buffer.from(string)).setName(`GuildData-${interaction.guild.name}.json`),
123123
]
124124
})
125125

src/modules/utility/commands/subCommand/utility.export.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ export default {
5050
new ButtonBuilder()
5151
.setEmoji("<:export:1321939859228721172>")
5252
.setLabel("Export Guild Data")
53+
.setDisabled(!(interaction.guild.ownerId == interaction.user.id))
5354
.setStyle(ButtonStyle.Secondary)
5455
.setCustomId("utility-export-guild"),
5556
new ButtonBuilder()
5657
.setEmoji("<:export:1321939859228721172>")
57-
.setLabel("Delete Guild Data")
58+
.setLabel("Delete Guild Data (SOON)")
59+
.setDisabled(true)
5860
.setStyle(ButtonStyle.Danger)
5961
.setCustomId("utility-export-guild-delete"),
6062
)

0 commit comments

Comments
 (0)