Add vrctl parse and reminder
This commit is contained in:
62
events/vrcTlEvents.js
Normal file
62
events/vrcTlEvents.js
Normal file
@@ -0,0 +1,62 @@
|
||||
const { Events } = require('discord.js');
|
||||
const axios = require('axios').default;
|
||||
|
||||
const SWARM_EVENTS_CHANNEL_ID = '1536067515115905155';
|
||||
const VRC_TL_EVENT_REGEX = /https:\/\/vrc\.tl\/event\/(\d+)/g;
|
||||
const REMINDER_EMOTE = '🔔';
|
||||
|
||||
module.exports = {
|
||||
name: Events.MessageCreate,
|
||||
async execute(message) {
|
||||
if (message.author.bot) return;
|
||||
if (message.channel.id !== SWARM_EVENTS_CHANNEL_ID) return;
|
||||
|
||||
const matches = [...message.content.matchAll(VRC_TL_EVENT_REGEX)];
|
||||
if (matches.length === 0) return;
|
||||
|
||||
const eventIds = [...new Set(matches.map(match => match[1]))];
|
||||
|
||||
for (const eventId of eventIds) {
|
||||
try {
|
||||
const { data } = await axios.get(`https://vrc.tl/api/v1/events/${eventId}`);
|
||||
const event = data.events?.[0];
|
||||
if (!event) continue;
|
||||
|
||||
const performersById = new Map(data.performers.map(performer => [performer.id, performer.name]));
|
||||
|
||||
const lineup = [...event.eventSlots]
|
||||
.sort((a, b) => a.start - b.start)
|
||||
.flatMap(slot => [...slot.performers]
|
||||
.sort((a, b) => a.order - b.order)
|
||||
.map(slotPerformer => performersById.get(slotPerformer.performerId)))
|
||||
.filter(Boolean);
|
||||
|
||||
const lines = [
|
||||
`**${event.name}**`,
|
||||
`<t:${event.start}:F> (<t:${event.start}:R>)`,
|
||||
];
|
||||
|
||||
if (lineup.length > 0) {
|
||||
lines.push(`DJ Lineup: ${lineup.join(', ')}`);
|
||||
}
|
||||
|
||||
if (event.urls?.vrcGroup) {
|
||||
lines.push(`Group: ${event.urls.vrcGroup}`);
|
||||
}
|
||||
|
||||
lines.push(`\nReact with ${REMINDER_EMOTE} to get pinged 1 hour before this starts.`);
|
||||
|
||||
const reply = await message.reply(lines.join('\n'));
|
||||
await reply.react(REMINDER_EMOTE);
|
||||
|
||||
const db = await message.client.localDB;
|
||||
db.prepare(`
|
||||
INSERT INTO vrctl_event_reminders (vrctl_event_id, message_id, channel_id, event_name, event_start)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
`).run(eventId, reply.id, reply.channel.id, event.name, event.start);
|
||||
} catch (error) {
|
||||
console.error(`Failed to fetch vrc.tl event ${eventId}:`, error?.response?.data ?? error);
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user