-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
149 lines (127 loc) · 4.18 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
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
const fs = require("fs");
const { Client, Collection, Intents } = require("discord.js");
const { token } = require("./config.json");
const client = new Client({
partials: ["CHANNEL"],
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.DIRECT_MESSAGES],
});
const logger = require("./logger");
const sqlite3 = require("sqlite3").verbose();
const { open } = require("sqlite");
let db;
// Initialize the database
async function initializeDatabase() {
db = await open({
filename: "bot_logs.db",
driver: sqlite3.Database,
});
await db.exec(`
CREATE TABLE IF NOT EXISTS command_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
command_name TEXT NOT NULL,
timestamp DATETIME NOT NULL
)
`);
}
// Function to log commands
async function logCommand(commandName) {
const timestamp = new Date().toISOString();
await db.run(
"INSERT INTO command_logs (command_name, timestamp) VALUES (?, ?)",
[commandName, timestamp]
);
}
client.on("ready", () => logger.info("The bot is online"));
client.on("debug", (m) => logger.debug(m));
client.on("warn", (m) => logger.warn(m));
client.on("error", (m) => logger.error(m));
client.commands = new Collection();
const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
}
client.once("ready", async () => {
console.log("Ready!");
await initializeDatabase();
const cron = require("node-cron");
const task = cron.schedule("*/15 * * * * *", () => {
const fs = require("fs");
// Read users.json file
fs.readFile("remindLog.json", function (err, data) {
// Check for errors
if (err) throw err;
const existData = JSON.parse(data);
// Grab current UNIX timestamp (timezone agnostic)
const curTime = Date.now();
// Converting to JSON
const deleteLog = [];
let deleteIter = 0;
let listingChange = false;
for (i = 0; i < existData.remindData.length; i++) {
const remindTime = existData.remindData[i].remindTime;
const channel = existData.remindData[i].channel;
const userID = existData.remindData[i].user;
const message = existData.remindData[i].message;
const hours = existData.remindData[i].hours;
const minutes = existData.remindData[i].minutes;
if (remindTime <= curTime) {
const notify = `<@!${userID}>: ${message}
Reminder set ${hours} hour(s) and ${minutes} minutes ago.`;
// Send message to user channel they requested from
client.channels.cache.get(channel.toString()).send(notify);
deleteLog[deleteIter] = i;
deleteIter++;
listingChange = true;
}
}
if (listingChange === true) {
// Remove deleted rows from Array
for (i = deleteLog.length - 1; i > -1; i--) {
listingChange = true;
const removeData = existData.remindData.splice(deleteLog[i], 1);
console.log("PRUNED REMINDLOG ENTRY");
}
fs.writeFile(
"remindLog.json",
JSON.stringify(existData, null, 1),
function (err) {
if (err) throw err;
console.log("Updated remind database.");
}
);
}
});
});
task.start();
});
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand() && !interaction.isButton()) return;
if (interaction.isCommand()) {
logger.info({
command: interaction.commandName,
user: interaction.user.username + "#" + interaction.user.discriminator,
options: interaction.options,
});
// Log the command to SQLite
await logCommand(interaction.commandName);
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction, client);
} catch (error) {
console.error(error);
return interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
}
if (interaction.isButton()) {
logger.info("button clicked");
return;
}
});
client.login(token);