-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (52 loc) · 2.46 KB
/
Copy pathindex.js
File metadata and controls
62 lines (52 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
import { Client, GatewayIntentBits, SlashCommandBuilder, REST, Routes, PermissionFlagsBits } from 'discord.js';
import Database from 'better-sqlite3';
// 1. Initialize SQLite
const db = new Database('lockdown.db');
db.prepare("CREATE TABLE IF NOT EXISTS lockdowns (user_id TEXT PRIMARY KEY, expires_at INTEGER)").run();
// 2. Setup Client
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers]
});
const TOKEN = process.env.DISCORD_TOKEN;
const CLIENT_ID = process.env.CLIENT_ID;
// 3. Register Slash Command
const lockdownCmd = new SlashCommandBuilder()
.setName('lockdown')
.setDescription('Lock a user out until a specific date.')
.setDefaultMemberPermissions(PermissionFlagsBits.KickMembers)
.addStringOption(option => option.setName('user_id').setDescription('Target ID').setRequired(true))
.addIntegerOption(option => option.setName('days').setDescription('Days to lock').setRequired(true));
client.once('ready', async () => {
const rest = new REST({ version: '10' }).setToken(TOKEN);
await rest.put(Routes.applicationCommands(CLIENT_ID), { body: [lockdownCmd.toJSON()] });
console.log('Bot is online and lockdown command registered!');
});
// 4. Handle Command Execution
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'lockdown') {
const userId = interaction.options.getString('user_id');
const days = interaction.options.getInteger('days');
const expiry = Date.now() + (days * 24 * 60 * 60 * 1000);
db.prepare("INSERT OR REPLACE INTO lockdowns (user_id, expires_at) VALUES (?, ?)").run(userId, expiry);
await interaction.reply(`Locked ${userId} for ${days} days.`);
}
});
// 5. The Enforcement Engine (The Kick Guard)
client.on('guildMemberAdd', async member => {
const row = db.prepare("SELECT expires_at FROM lockdowns WHERE user_id = ?").get(member.id);
if (row) {
if (Date.now() < row.expires_at) {
try {
await member.kick("User is currently on an active security lockdown.");
console.log(`Kicked ${member.id} due to lockdown.`);
} catch (e) {
console.error("Could not kick member:", e);
}
} else {
// Lockdown expired, clean up the DB
db.prepare("DELETE FROM lockdowns WHERE user_id = ?").run(member.id);
}
}
});
client.login(TOKEN);