-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (54 loc) · 2.62 KB
/
index.js
File metadata and controls
77 lines (54 loc) · 2.62 KB
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
// Require the necessary discord.js classes
const { Client, Events, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] });
// Create a map to store the last time a user was in a voice channel
const userLastVoiceChannelTimeMap = new Map();
// Create a map to store the messages sent to the text channel when users join a voice channel
const userTextChannelMessageMap = new Map();
// 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}`);
});
// Listen for the voiceStateUpdate event
client.on(Events.VoiceStateUpdate, (oldState, newState) => {
// Find the voice channel by name
const textChannel = client.channels.cache.find(channel => channel.name === 'conexiones');
// If the user joined a voice channel
if (!oldState.channelId && newState.channelId) {
// If the voice channel is found
if (textChannel) {
const voiceChannel = client.channels.cache.get(newState.channelId);
// Get the last time the user was in a voice channel
const userLastVoiceChannelTime = userLastVoiceChannelTimeMap.get(newState.member.user.id);
// If the user was last in a voice channel less than 1 hour ago, do not send a notification
if (userLastVoiceChannelTime && Date.now() - userLastVoiceChannelTime < 3600000) { //milisegundos 3600000 1h
return;
}
// Send a message to the text channel
const message = textChannel.send(`${newState.member.user.tag} se ha conectado al canal de voz ${voiceChannel.name}`);
// Store the message in the map
userTextChannelMessageMap.set(newState.member.user.id, message.id);
} else {
// If the voice channel is not found, log an error
console.error(`Could not find voice channel with name 'Channel Name'`);
}
}
else if (oldState.channelId && !newState.channelId) {
// Update the user's last voice channel time
userLastVoiceChannelTimeMap.set(newState.member.user.id, Date.now());
/*
// Get the message that was sent to the text channel when the user joined the voice channel
const message = userTextChannelMessageMap.get(newState.member.user.id);
// If the message exists, delete it
if (message) {
message.delete();
userTextChannelMessageMap.delete(newState.member.user.id, message);
}
*/
}
});
// Iniciamos el cliente
client.login(token);