-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
240 lines (202 loc) · 7.66 KB
/
index.js
File metadata and controls
240 lines (202 loc) · 7.66 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// for production
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("The Residency bot server is active.");
});
// Require connection to commands
const fs = require("node:fs");
const path = require("node:path");
const { Collection, TextChannel } = require("discord.js");
// Require the necessary discord.js classes
const { savetoSheet, savetoLogs } = require("./toSheet.js");
const { Client, Events, GatewayIntentBits } = require("discord.js");
//const { BOT_TOKEN } = require('./config.json');
const dotenv = require("dotenv");
dotenv.config();
const BOT_TOKEN = process.env.BOT_TOKEN;
// Create a new client instance
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates],
}); // REFER TO https://ziad87.net/intents/
// Create map for logs
const users = new Map();
// --- start: connect to commands --- //
client.commands = new Collection();
const foldersPath = path.join(__dirname, "commands");
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ("data" in command && "execute" in command) {
client.commands.set(command.data.name, command);
} else {
console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
);
}
}
}
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: "There was an error while executing this command!",
ephemeral: true,
});
} else {
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
}
});
// --- end: connect to commands --- ///
//TODO: make command to download logs.csv file
// --- start: activate when user joins/leaves a voice channel --- //
function formatTime(milliseconds) {
let hours = Math.floor(milliseconds / (1000 * 60 * 60));
let minutes = Math.floor((milliseconds % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((milliseconds % (1000 * 60)) / 1000);
return hours + "h " + minutes + "m " + seconds + "s";
}
function formatTimestamp(timestamp) {
// Convert milliseconds to seconds
const sec = Math.floor(timestamp / 1000);
// Create a new Date object from the seconds
const dateObj = new Date(sec * 1000);
// Calculate the Philippine time offset (UTC+8) in minutes
const philippinesOffset = 8 * 60;
// Adjust the date object to Philippine time
const philippineTime = new Date(
dateObj.getTime() + philippinesOffset * 60000
);
// Extract hours, minutes, and seconds (zero-padded for two digits)
const hours = philippineTime.getUTCHours().toString().padStart(2, "0");
const minutes = philippineTime.getUTCMinutes().toString().padStart(2, "0");
const seconds = philippineTime.getUTCSeconds().toString().padStart(2, "0");
// Format the time string (e.g., 08:49:04)
return `${hours}:${minutes}:${seconds}`;
}
// Function to write logs to CSV file / FOR CHECKING ONLY
function writeLogToCSV(username, totalTime, date) {
let logString = username + "," + formatTime(totalTime) + "," + date + "\n";
fs.appendFile("logs.csv", logString, (err) => {
if (err) {
console.error("Error appending data to file:", err);
} else {
console.log("Log entry added to logs.csv");
}
});
}
function getCurrentDate() {
const currentDate = new Date();
return currentDate.toLocaleDateString("en-US", {
month: "numeric",
day: "numeric",
year: "numeric",
timeZone: "Asia/Manila",
});
}
async function sendAlert(time, member, text) {
const textChannelID = "1238148714011299860";
try {
const logChannel = await client.channels.fetch(textChannelID);
logChannel.send(
`${member} ${text} residency channel at ${formatTimestamp(time)}`
);
} catch (error) {
console.error("Error fetching or sending message in channel:", error);
}
}
client.on(Events.VoiceStateUpdate, async (past, present) => {
//debugging
console.log("\nVoiceStateUpdate is activated");
let user = past.member || present.member;
let past_channel = past.channelId;
let present_channel = present.channelId;
let past_user = past.member.user;
let present_user = present.member.user;
console.log("present", present.channelId);
// TODO: activate only if user joined/left voice channel 'Residency'
if (
present_channel != null &&
(present.channelId == 1214250569125077003 ||
present.channelId == 1238149023135957044)
) {
// user joins a voice channel
console.log(present_user.username, "joined a voice channel");
// User joins RESIDENCY voice channel
if (
present.channelId == 1214250569125077003 ||
present.channelId == 1238149023135957044
) {
console.log(present_user.username, "joined a RESIDENCY voice channel");
users.set(user.id, new Date().getTime());
sendAlert(users.get(user.id), present_user.username, "joined");
}
} else if (
present_channel == null ||
past.channelId == 1214250569125077003 ||
past.channelId == 1238149023135957044
) {
// user left a voice channel
console.log(past_user.username, "left the voice channel");
// User left RESIDENCY voice channel
if (
past.channelId == 1214250569125077003 ||
past.channelId == 1238149023135957044
) {
if (users.get(user.id) != undefined) {
const timeOut = new Date().getTime();
const totalTime = new Date().getTime() - users.get(user.id);
const today = getCurrentDate();
sendAlert(timeOut, past_user.username, "left");
// FOR CHECKING ONLY
// past_user.username
//writeLogToCSV(past_user.username, totalTime, today);
savetoLogs(
past_user.username,
today,
formatTimestamp(users.get(user.id)),
formatTimestamp(timeOut),
formatTime(totalTime),
(error) => {
if (error) {
console.error("Callback error:", error);
} else {
// run after async writing on logs (latest log should be recorded)
savetoSheet();
}
}
);
}
}
}
});
// --- end: activate when user joins/leaves a voice channel --- //
// When the client is ready, run this code (only once).
// The distinction between `client: Client<boolean>` and `readyClient: Client<true>` is important for TypeScript developers.
// It makes some properties non-nullable.
client.once(Events.ClientReady, (readyClient) => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
// Log in to Discord with your client's token
client.login(BOT_TOKEN);
app.listen(3000, () => console.log("Server started at port 3000"));