-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (90 loc) · 3.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const { Client, Events, GatewayIntentBits } = require('discord.js');
const { token, originalVoiceChannelId } = require('./config.json');
const { handleCommand } = require('./features/customCommands');
// Create a new client instance
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildVoiceStates, // Add intent for voice state
],
});
// Store the dynamically created voice channel IDs
const dynamicVoiceChannels = new Set();
// When the client is ready, run this code (only once)
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
client.once(Events.ClientReady, (c) => {
console.log(`Ready! Logged in as ${c.user.tag}`);
});
client.on(Events.MessageCreate, async (message) => {
if (message.author.bot) return;
if (message.content.startsWith('!')) {
try {
const replyMessage = await handleCommand(message);
if (replyMessage) {
message.reply(replyMessage);
}
}
catch (err) {
console.error(err);
message.reply('發生錯誤,指令處理失敗。');
}
}
});
client.on(Events.MessageDelete, (message) => {
if (message.author.bot) return;
console.log(`${message.author.username}刪除了${message.content}`);
});
client.on(Events.MessageUpdate, (message) => {
if (message.author.bot) return;
console.log(
`${message.author.username}更新了${message.content}改為${message.reactions.message.content}`,
);
});
// Listen for voice state update event
client.on(Events.VoiceStateUpdate, async (oldState, newState) => {
// Check if the user joined the specified voice channel
if (newState.channelId === originalVoiceChannelId && oldState.channelId !== originalVoiceChannelId) {
const member = newState.member;
const guild = newState.guild;
const category = newState.channel.parent;
// Get the permission settings of the category where the original voice channel is located
const categoryPermissions = category.permissionOverwrites.cache;
// Create new voice channel permission settings
const newChannelPermissions = categoryPermissions.map((permission) => ({
id: permission.id,
allow: permission.allow,
deny: permission.deny,
}));
// Create a new voice channel in the same category
const newChannel = await guild.channels.create({
name: `${member.user.username}'s Channel`,
type: 2, // 2 represents a voice channel
parent: category,
permissionOverwrites: newChannelPermissions,
});
// Move the user to the new voice channel
await member.voice.setChannel(newChannel);
// Add the ID of the newly created voice channel to the stored set
dynamicVoiceChannels.add(newChannel.id);
}
// Check if the user switched from a dynamically created voice channel to another channel
if (dynamicVoiceChannels.has(oldState.channelId) && newState.channelId !== oldState.channelId) {
const channel = oldState.channel;
if (channel.members.size === 0) {
// If there are no other users in the channel, delete it
try {
await channel.delete();
// Remove the channel ID from the stored set
dynamicVoiceChannels.delete(channel.id);
}
catch (error) {
console.error('刪除頻道時發生錯誤:', error);
}
}
}
});
// Log in to Discord with your client's token
client.login(token);