-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
182 lines (150 loc) · 5.08 KB
/
Copy pathdatabase.js
File metadata and controls
182 lines (150 loc) · 5.08 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const Database = require('better-sqlite3');
const dbPath = process.env.SQLITE_PATH || 'honeypots.db';
const db = new Database(dbPath);
db.pragma('foreign_keys = ON');
db.prepare(`
CREATE TABLE IF NOT EXISTS servers (
guild_id TEXT PRIMARY KEY,
channel_id TEXT NOT NULL,
message_json TEXT,
message_id TEXT
)
`).run();
// Add per-server honeypot action (kick default) to existing databases
const serverColumns = db.prepare(`PRAGMA table_info(servers)`).all();
if (!serverColumns.some(col => col.name === 'action')) {
db.prepare(`ALTER TABLE servers ADD COLUMN action TEXT NOT NULL DEFAULT 'kick'`).run();
}
db.prepare(`
CREATE TABLE IF NOT EXISTS users (
user_id TEXT PRIMARY KEY,
username TEXT NOT NULL,
banned_status INTEGER NOT NULL DEFAULT 0,
isAdmin INTEGER NOT NULL DEFAULT 0
)
`).run();
db.prepare(`
CREATE TABLE IF NOT EXISTS image_spam_settings (
guild_id TEXT PRIMARY KEY,
enabled INTEGER NOT NULL DEFAULT 1,
announce INTEGER NOT NULL DEFAULT 1
)
`).run();
// Your inmutable audit log table
db.prepare(`
CREATE TABLE IF NOT EXISTS Bans (
id INTEGER PRIMARY KEY,
guild_id TEXT NOT NULL,
user_id TEXT NOT NULL,
username TEXT NOT NULL,
banned_at TEXT NOT NULL DEFAULT (datetime('now'))
)
`).run();
const insertServer = db.prepare(`
INSERT INTO servers (guild_id, channel_id)
VALUES (?, ?)
ON CONFLICT(guild_id) DO UPDATE SET
channel_id = excluded.channel_id
`);
const insertServerMessage = db.prepare(`
UPDATE servers SET message_json = ?, message_id = ? WHERE guild_id = ?
`);
// Inserts every single ban event into the history log
const insertBanHistory = db.prepare(`
INSERT INTO Bans (guild_id, user_id, username)
VALUES (?, ?, ?)
`);
// Updates or creates the user's current global ban status
const upsertUserStatus = db.prepare(`
INSERT INTO users (user_id, username, banned_status)
VALUES (?, ?, 1)
ON CONFLICT(user_id) DO UPDATE SET banned_status = 1
`);
const updateUserUnban = db.prepare(`
UPDATE users SET banned_status = 0 WHERE user_id = ?
`);
const checkAdminStatus = db.prepare(`
SELECT isAdmin FROM users WHERE user_id = ?
`);
const checkHoneypotChannel = db.prepare(`
SELECT channel_id, message_id, action FROM servers WHERE guild_id = ?
`);
const getAllServers = db.prepare(`
SELECT guild_id, action FROM servers
`);
const updateHoneypotActionStmt = db.prepare(`
UPDATE servers SET action = ? WHERE guild_id = ?
`);
const deleteServer = db.prepare(`
DELETE FROM servers WHERE guild_id = ?
`);
const upsertImageSpamSettings = db.prepare(`
INSERT INTO image_spam_settings (guild_id, enabled, announce)
VALUES (?, ?, ?)
ON CONFLICT(guild_id) DO UPDATE SET
enabled = excluded.enabled,
announce = excluded.announce
`);
const getImageSpamSettingsStmt = db.prepare(`
SELECT enabled, announce FROM image_spam_settings WHERE guild_id = ?
`);
function addHoneypot(guildId, channelId, userId) {
// Discord IDs should always be passed as Strings
if (checkAdminStatus.get(userId)?.isAdmin !== 1) {
console.log(`[WARNING] Attempted to set a honeypot by a non-admin user: ${userId}. Action aborted.`);
return false;
}
insertServer.run(guildId, channelId);
return true;
}
function updateHoneypotMessage(guildId, messageJson, messageId) {
insertServerMessage.run(messageJson, messageId ? String(messageId) : null, guildId);
}
function registerBan(guildId, userId, username) {
if (checkAdminStatus.get(userId)?.isAdmin === 1) {
console.log(`[WARNING] Attempted to ban an admin user: ${username} (${userId}). Action aborted.`);
return false;
}
// 1. Save to the permanent history log (Will never be deleted)
insertBanHistory.run(guildId, userId, username);
// 2. Set current real-time status to Banned (1)
upsertUserStatus.run(userId, username);
return true;
}
function removeBan(userIdBanned) {
// Keeps the logs in 'Bans' intact, only flips the current status to Unbanned (0)
updateUserUnban.run(userIdBanned);
}
function updateHoneypotAction(guildId, action) {
updateHoneypotActionStmt.run(action === 'ban' ? 'ban' : 'kick', guildId);
}
function removeHoneypot(guildId) {
return deleteServer.run(guildId).changes > 0;
}
function checkAdmin(userId){
if (checkAdminStatus.get(userId)?.isAdmin !== 1) {
console.log(`[WARNING] Admin-only operation attempted by non-admin user: ${userId}.`);
return false;
}
return true;
}
function getImageSpamSettings(guildId) {
return getImageSpamSettingsStmt.get(guildId) || { enabled: 1, announce: 1 };
}
function setImageSpamSettings(guildId, enabled, announce) {
upsertImageSpamSettings.run(guildId, enabled ? 1 : 0, announce ? 1 : 0);
return true;
}
module.exports = {
addHoneypot,
updateHoneypotMessage,
registerBan,
removeBan,
checkHoneypotChannel,
getAllServers,
removeHoneypot,
checkAdmin,
getImageSpamSettings,
setImageSpamSettings,
updateHoneypotAction
};