GodOfSandwiches
DIAdiscord.js - Imagine an app
•Created by GodOfSandwiches on 3/9/2024 in #djs-questions
fetching scheduled events
Sorry im a bit dumb can you show me what you mean
12 replies
DIAdiscord.js - Imagine an app
•Created by GodOfSandwiches on 3/9/2024 in #djs-questions
fetching scheduled events
code cont:
async function processEvent(event, dbClient) {
try {
const db = dbClient.db('DiscordBot');
const eventsCollection = db.collection('events');
const currentTime = new Date();
const eventTime = new Date(event.scheduledStartAt);
const eventData = {
id: event.id,
name: event.name,
startTime: event.scheduledStartAt,
endTime: event.scheduledEndAt,
server: event.guild.id,
// Add more fields as needed
};
if (currentTime >= eventTime) {
// If the event is over, delete it from the database
const result = await eventsCollection.deleteOne({ id: event.id });
console.log('Event deleted:', result.deletedCount);
} else {
// If the event is not over, insert or update it in the database
const existingEvent = await eventsCollection.findOne({ id: event.id });
if (!existingEvent) {
const result = await eventsCollection.insertOne(eventData);
console.log('Event inserted:', result.insertedId);
} else {
const result = await eventsCollection.replaceOne({ id: event.id }, eventData);
console.log('Event updated:', result.modifiedCount);
}
}
} catch (error) {
console.error('Error processing event:', error);
}
}
module.exports = { scanForEvents, processEvent, getGuildEvents };
12 replies
DIAdiscord.js - Imagine an app
•Created by GodOfSandwiches on 3/9/2024 in #djs-questions
fetching scheduled events
code:
async function getGuildEvents(guild) {
try {
const events = await GuildScheduledEvent.fetchEvents(guild); // Fetch events for the guild
return events;
} catch (error) {
console.error('Error fetching events for guild:', guild.id, error);
return null;
}
}
async function scanForEvents(client, dbClient) {
// Scan events for all guilds the bot is in
try {
for (const guild of client.guilds.cache.values()) {
let guildEvents = await getGuildEvents(guild);
// Convert guildEvents to an array if it's not already
if (!Array.isArray(guildEvents)) {
guildEvents = [guildEvents];
}
for (const event of guildEvents) {
await processEvent(event);
}
}
} catch (error) {
console.error('Error scanning guilds and database for events:', error);
}
// Scan events from the database
try {
const db = dbClient.db('DiscordBot');
const eventsCollection = db.collection('events');
const dbEvents = await eventsCollection.find({}).toArray();
console.log('Events in database:', dbEvents);
} catch (error) {
console.error('Error scanning database for events:', error);
}
}
12 replies
DIAdiscord.js - Imagine an app
•Created by GodOfSandwiches on 2/14/2024 in #djs-questions
Getting Guild Events
how do i search a specific guild using a guildid?
7 replies
DIAdiscord.js - Imagine an app
•Created by GodOfSandwiches on 2/14/2024 in #djs-questions
Getting Guild Events
Ty ❤️
7 replies
DIAdiscord.js - Imagine an app
•Created by GodOfSandwiches on 2/14/2024 in #djs-questions
Getting Guild Events
Node version is 20.11.0 discord js version listed in tags
7 replies