-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
225 lines (180 loc) · 6.8 KB
/
index.js
File metadata and controls
225 lines (180 loc) · 6.8 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
require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');
const { google } = require('googleapis');
const { DateTime } = require('luxon');
const fs = require('fs');
const path = require("path");
const {
DISCORD_BOT_TOKEN,
GOOGLE_SHEET_ID,
GOOGLE_SERVICE_ACCOUNT_JSON,
SHEET_TAB_NAME="Queue",
TIMEZONE= "America/Toronto",
POLL_SECONDS='60',
} = process.env;
// Load roles and their respective IDs from roles.json
function loadRoleMap() {
const rolesPath = path.join(__dirname, "roles.json");
if (!fs.existsSync(rolesPath)) return {};
try {
return JSON.parse(fs.readFileSync(rolesPath, "utf8"));
} catch (e) {
console.warn("roles.json exists but could not be parsed:", e.message);
return {};
}
}
const ROLE_MAP = loadRoleMap();
/**
* Replaces @RoleName occurrences with <@&ROLE_ID>.
* Only replaces roles that exist in roles.json.
*/
function replaceRoleMentions(content) {
let out = String(content);
for (const [roleName, roleId] of Object.entries(ROLE_MAP)) {
// Replace @Dev, @Design, etc. as standalone tokens.
// Avoid matching inside emails/words by using boundaries.
const re = new RegExp(`(^|\\s)@${escapeRegex(roleName)}s?(?=\\b)`, "gi");
out = out.replace(re, `$1<@&${roleId}>`);
}
return out;
}
function escapeRegex(s) {
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
// ------------ Google Sheets Setup ------------
function getGoogleAuth() {
// Prefer JSON from env (For Heroku). Fallback to file for local dev.
const jsonFromEnv = process.env.GOOGLE_SERVICE_ACCOUNT_KEY;
if (jsonFromEnv) {
const creds = JSON.parse(jsonFromEnv);
return new google.auth.GoogleAuth({
credentials: creds,
scopes: ["https://www.googleapis.com/auth/spreadsheets"],
});
}
const keyFile = process.env.GOOGLE_SERVICE_ACCOUNT_JSON;
if (!keyFile || !fs.existsSync(keyFile)) {
throw new Error(
`No GOOGLE_SERVICE_ACCOUNT_KEY env var set and JSON file not found at: ${keyFile}`
);
}
return new google.auth.GoogleAuth({
keyFile,
scopes: ["https://www.googleapis.com/auth/spreadsheets"],
});
}
async function getSheetsClient() {
const auth = await getGoogleAuth().getClient();
return google.sheets({ version: 'v4', auth });
}
// ------------ Discord Bot Setup ------------
const client = new Client({
intents: [GatewayIntentBits.Guilds],
});
function normalizeBool(v) {
if ( typeof v === "boolean") return v;
if (!v) return false;
const s = String(v).trim().toLowerCase();
return ['1', 'true', 'yes', 'on'].includes(s);
}
function parseSendAt(sendAtRaw) {
const raw = String(sendAtRaw).trim().replace(/\u00A0/g, " "); // remove weird non-breaking spaces
// Try 24h with 1-2 digit hour
let dt = DateTime.fromFormat(raw, "yyyy-MM-dd H:mm", { zone: TIMEZONE });
if (dt.isValid) return dt;
// Fallback: some sheets return seconds
dt = DateTime.fromFormat(raw, "yyyy-MM-dd H:mm:ss", { zone: TIMEZONE });
if (dt.isValid) return dt;
// Fallback: if Google returns something like "2026-01-18 1:45 AM"
dt = DateTime.fromFormat(raw, "yyyy-MM-dd h:mm a", { zone: TIMEZONE });
if (dt.isValid) return dt;
return null;
}
async function pollAndSend() {
const now = DateTime.now().setZone(TIMEZONE);
const sheets = await getSheetsClient();
// Read all rows from the queue sheet
const range = `${SHEET_TAB_NAME}!A:F`;
const res = await sheets.spreadsheets.values.get({
spreadsheetId: GOOGLE_SHEET_ID,
range,
});
const rows = res.data.values || [];
if (rows.length < 2 ) return; // No data
const header = rows[0].map(h => String(h).trim().toLowerCase());
const idx = {
sendAt: header.indexOf('send_at'),
channelId: header.indexOf('channel_id'),
message: header.indexOf('message'),
sent: header.indexOf('sent'),
sentAt: header.indexOf('sent_at'),
};
// Basic header validation
for (const [k, v] of Object.entries(idx)) {
if (v === -1) {
throw new Error(`Missing required column "${k}" in sheet header`);
}
}
// Process each row (starting from row 2)
for (let i = 1; i < rows.length; i++) {
const row = rows[i];
const sendAtRaw = row[idx.sendAt];
const channelId = row[idx.channelId];
const message = row[idx.message];
const sentRaw = row[idx.sent];
const alreadSent = normalizeBool(sentRaw);
if (alreadSent) continue; // Skip already sent
if (!sendAtRaw || !channelId || !message) continue; // Incomplete row
const sendAt = parseSendAt(sendAtRaw);
if (!sendAt) {
console.warn(`Invalid send_at format in row ${i + 1}: "${sendAtRaw}"`);
continue;
}
if (sendAt <= now) {
// Time to send the message
try {
const channel = await client.channels.fetch(String(channelId).trim());
if (!channel || !channel.isTextBased()) {
console.log(`Row ${i + 1}: Invalid or non-text channel ID "${channelId}"`);
continue;
}
const finalMessage = replaceRoleMentions(message);
await channel.send({
content: finalMessage,
allowedMentions: {
parse: ["everyone", "roles"], // allows @everyone and <@&roleId> pings
},
});
// Rate limit delay
await new Promise(r => setTimeout(r, 1200));
// Mark as sent
const sentAtStr = now.toFormat('yyyy-MM-dd HH:mm');
const updateRange = `${SHEET_TAB_NAME}!D${i + 1}:E${i + 1}`;
await sheets.spreadsheets.values.update({
spreadsheetId: GOOGLE_SHEET_ID,
range: updateRange,
valueInputOption: "USER_ENTERED",
requestBody: { values: [["TRUE", sentAtStr]] },
});
console.log(`Row ${i + 1}: Message sent to channel ${channelId}`);
console.log(`Row ${i + 1}: Marked as TRUE and sent at ${sentAtStr}`);
} catch (err) {
console.log(`Row ${i + 1}: Error sending message to channel ${channelId}:`, err.message);
}
}
}
}
// ------------ Main Execution ------------
client.once('clientReady', async () => {
console.log(`Logged in as ${client.user.tag}!`);
console.log(`Polling every ${POLL_SECONDS} seconds...`);
// Poll immediately, then at intervals
await pollAndSend();
setInterval(() => {
pollAndSend().catch(err => console.error('Error in pollAndSend:', err));
}, Number(POLL_SECONDS) * 1000);
});
client.login(DISCORD_BOT_TOKEN).catch(err => {
console.error('Failed to login to Discord:', err);
process.exit(1);
});