47 lines
1.6 KiB
JavaScript
47 lines
1.6 KiB
JavaScript
const { Guild, GuildMember, Events } = require('discord.js')
|
|
const db = require('../../db')
|
|
|
|
const { userStreamingEmbed } = require('../../embeds/userStreaming')
|
|
|
|
|
|
module.exports = {
|
|
name: Events.PresenceUpdate,
|
|
async execute(oldPresence, newPresence) {
|
|
|
|
//console.log('Presence Update:', newPresence);
|
|
|
|
|
|
|
|
// Check if the user is streaming
|
|
if (!newPresence.activities) return;
|
|
const streamingActivity = newPresence.activities.find(activity => activity.type === 1);
|
|
if (!streamingActivity) return;
|
|
|
|
const user = newPresence.user;
|
|
|
|
if (streamingActivity)
|
|
{
|
|
try {
|
|
for (const [guildId, guild] of newPresence.client.guilds.cache) {
|
|
const member = await guild.members.fetch(user.id).catch(() => null);
|
|
if (!member) continue;
|
|
const result = await db.query("SELECT channel_id FROM bot_channel WHERE guild_id = $1", [guildId]);
|
|
if (result.rows.length === 0) continue;
|
|
const channelId = result.rows[0].channel_id;
|
|
const channel = guild.channels.cache.get(channelId);
|
|
|
|
if (channel && channel.isTextBased()) {
|
|
const { embeds, components } = userStreamingEmbed(user, streamingActivity);
|
|
await channel.send({
|
|
embeds,
|
|
components
|
|
});
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching user:', error);
|
|
return;
|
|
}
|
|
}
|
|
},
|
|
}; |