-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpingOfftopic.js
More file actions
77 lines (58 loc) · 2.46 KB
/
Copy pathpingOfftopic.js
File metadata and controls
77 lines (58 loc) · 2.46 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
77
// pingOfftopic.js
// Handles /pingofftopic command with cooldowns and thread check
const Globals = require('./Globals');
// Cooldown tables for /pingofftopic
const CooldownTracker = {
server: null, // timestamp of last server-wide ping
users: new Map(), // userId => timestamp of last personal ping
};
// Cooldowns in milliseconds
const Cooldowns = {
server: 15 * 60 * 1000, // 15 minutes
user: 60 * 60 * 1000, // 1 hour
};
// Main execute function
async function execute(interaction) {
const { channel, member } = interaction;
const now = Date.now();
try {
// Defer reply immediately to extend time window
if (!interaction.replied && !interaction.deferred) {
await interaction.deferReply({ flags: 64 });
}
// 1️⃣ Ensure channel is whitelisted
if (!Globals.Commands.PingOfftopic.includes(channel.id)) {
return interaction.editReply({ content: '❌ This command cannot be used here.' });
}
// 2️⃣ Ignore threads
if (channel.isThread()) {
return interaction.editReply({ content: '❌ Cannot use this command inside threads.' });
}
// 3️⃣ Server cooldown
if (CooldownTracker.server && now - CooldownTracker.server < Cooldowns.server) {
const remaining = Math.ceil((Cooldowns.server - (now - CooldownTracker.server)) / 60000);
return interaction.editReply({ content: `❌ Server cooldown active. Try again in ${remaining} min.` });
}
// 4️⃣ User cooldown
if (CooldownTracker.users.has(member.id)) {
const lastUsed = CooldownTracker.users.get(member.id);
if (now - lastUsed < Cooldowns.user) {
const remaining = Math.ceil((Cooldowns.user - lastUsed) / 60000);
return interaction.editReply({ content: `❌ You are on cooldown. Try again in ${remaining} min.` });
}
}
// 5️⃣ Send ping message
const offTopicRole = channel.guild.roles.cache.find(r => r.name === 'Off-topic Notice');
if (!offTopicRole) return interaction.editReply({ content: '❌ Off-topic role not found.' });
await channel.send(`${offTopicRole} ${member}`);
// 6️⃣ Update cooldowns
CooldownTracker.server = now;
CooldownTracker.users.set(member.id, now);
// 7️⃣ Confirm to user
await interaction.editReply({ content: '✅ Off-topic ping sent!' });
} catch (err) {
if (err.code === 10062) console.warn('⚠️ Interaction expired before reply could be sent.');
else throw err;
}
}
module.exports = { execute };