({
client,
guildId,
eventType,
data = {},
attachments = [],
content = null,
channelId: overrideChannelId = null,
})
| 196 | } |
| 197 | |
| 198 | export async function logEvent({ |
| 199 | client, |
| 200 | guildId, |
| 201 | eventType, |
| 202 | data = {}, |
| 203 | attachments = [], |
| 204 | content = null, |
| 205 | channelId: overrideChannelId = null, |
| 206 | }) { |
| 207 | try { |
| 208 | const guild = client.guilds.cache.get(guildId) || |
| 209 | await client.guilds.fetch(guildId).catch(() => null); |
| 210 | |
| 211 | if (!guild) { |
| 212 | logger.warn(`logEvent: Guild not found: ${guildId}`); |
| 213 | return null; |
| 214 | } |
| 215 | |
| 216 | const config = await getGuildConfig(client, guildId); |
| 217 | const ignore = getIgnoreList(config); |
| 218 | |
| 219 | if (data?.userId && ignore.users?.includes(data.userId)) { |
| 220 | return null; |
| 221 | } |
| 222 | if (data?.channelId && ignore.channels?.includes(data.channelId)) { |
| 223 | return null; |
| 224 | } |
| 225 | |
| 226 | if (!isEventEnabled(config, eventType)) { |
| 227 | return null; |
| 228 | } |
| 229 | |
| 230 | const logChannelId = getLogChannelForEvent(config, eventType, overrideChannelId); |
| 231 | if (!logChannelId) { |
| 232 | return null; |
| 233 | } |
| 234 | |
| 235 | const channel = guild.channels.cache.get(logChannelId) || |
| 236 | await guild.channels.fetch(logChannelId).catch(() => null); |
| 237 | |
| 238 | if (!channel || channel.type !== ChannelType.GuildText) { |
| 239 | logger.warn(`logEvent: Invalid log channel ${logChannelId} for guild ${guildId}`); |
| 240 | return null; |
| 241 | } |
| 242 | |
| 243 | const permissions = channel.permissionsFor(guild.members.me); |
| 244 | if (!permissions || !permissions.has(['SendMessages', 'EmbedLinks'])) { |
| 245 | logger.warn(`logEvent: Missing permissions in channel ${logChannelId}`); |
| 246 | return null; |
| 247 | } |
| 248 | |
| 249 | const embed = createLogEmbed(guild, eventType, data); |
| 250 | |
| 251 | const messageOptions = { embeds: [embed] }; |
| 252 | if (content) { |
| 253 | messageOptions.content = content; |
| 254 | } |
| 255 | if (attachments.length > 0) { |
no test coverage detected