Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sticker update logging #28

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bot/commands/setchannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const eventList = [
'guildMemberNickUpdate',
'guildMemberVerify',
'guildEmojisUpdate',
'guildStickersUpdate',
'guildMemberBoostUpdate'
]

Expand Down
87 changes: 87 additions & 0 deletions src/bot/events/guildStickersUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const send = require('../modules/webhooksender')
const AUDIT_ID = {
added: 90,
removed: 92,
updated: 91
}

const STICKER_FORMAT_TYPES = {
1: 'PNG',
2: 'APNG',
3: 'LOTTIE',
}

module.exports = {
name: 'guildStickersUpdate',
type: 'on',
handle: async (guild, stickers, oldStickers) => {
console.log("STICKER UPDATED")
let type
const guildStickersUpdateEvent = {
guildID: guild.id,
eventName: 'guildStickersUpdate',
embeds: [{
description: 'Guild stickers were updated.',
fields: [{
name: 'Sticker was manipulated',
value: ''
}, {
name: 'ID',
value: '```ini\nUser = Unknown\nSticker = Unknown```'
}],
color: 3553599
}]
}
let sticker
if (stickers.length > oldStickers.length) {
const newStickers = stickers.filter(e => !oldStickers.find(o => o.id === e.id))
sticker = newStickers[0]
if (!sticker) {
return
}
type = 'added'
guildStickersUpdateEvent.embeds[0].thumbnail = {
url: `https://cdn.discordapp.com/stickers/${sticker.id}.png?v=1`
}
guildStickersUpdateEvent.embeds[0].fields[0].name = 'Added sticker'
guildStickersUpdateEvent.embeds[0].fields[0].value = `Name = ${sticker.name}\nDescription = ${sticker.description}\nFormat = ${STICKER_FORMAT_TYPES[sticker.format_type]}`
} else if (oldStickers.length > stickers.length) {
const removedStickers = oldStickers.filter(e => !stickers.find(o => o.id === e.id))
sticker = removedStickers[0]
type = 'removed'
guildStickersUpdateEvent.embeds[0].fields[0].name = 'Removed sticker'
guildStickersUpdateEvent.embeds[0].fields[0].value = `Name = ${sticker.name}\nDescription = ${sticker.description}\nFormat = ${STICKER_FORMAT_TYPES[sticker.format_type]}`
} else {
type = 'updated'
sticker = stickers.find(e => oldStickers.find(o => o.id === e.id).name !== e.name)
if (!sticker) return
guildStickersUpdateEvent.embeds[0].fields[0].name = 'Updated sticker'
guildStickersUpdateEvent.embeds[0].fields[0].value = `Name = ${sticker.name}\nDescription = ${sticker.description}\nFormat = ${STICKER_FORMAT_TYPES[sticker.format_type]}`
guildStickersUpdateEvent.embeds[0].thumbnail = {
url: `https://cdn.discordapp.com/stickers/${sticker.id}.png?v=1`
}
const oldSticker = oldStickers.find(o => o.id === sticker.id)
if (sticker.name !== oldSticker.name) {
guildStickersUpdateEvent.embeds[0].fields[0].value += `\nName was = ${oldSticker.name}`
}
if (sticker.description !== oldSticker.description) {
guildStickersUpdateEvent.embeds[0].fields[0].value += `\nDescription was = ${oldSticker.description}`
}
}
await setTimeout(async () => {
const num = AUDIT_ID[type]
const logs = await guild.getAuditLog({ limit: 5, actionType: num }).catch((err) => {console.log(err)})
if (!logs) return
const log = logs.entries.find(e => e?.targetID === sticker.id && Date.now() - ((e.id / 4194304) + 1420070400000) < 3000)
if (log && log.user) { // if the audit log is less than 3 seconds off
const user = log.user
guildStickersUpdateEvent.embeds[0].author = {
name: `${user.username}#${user.discriminator}`,
icon_url: user.avatarURL
}
guildStickersUpdateEvent.embeds[0].fields[1].value = `\`\`\`ini\nUser = ${user.id}\nSticker = ${sticker.id}\`\`\``
}
await send(guildStickersUpdateEvent)
}, 1000)
}
}
1 change: 1 addition & 0 deletions src/bot/modules/eventmiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function getGuildIdByEvent (type, args) {
case 'guildBanAdd':
case 'guildBanRemove':
case 'guildEmojisUpdate':
case 'guildStickersUpdate':
case 'guildMemberAdd':
case 'guildMemberUpdate':
case 'guildRoleCreate':
Expand Down
1 change: 1 addition & 0 deletions src/bot/modules/statAggregator.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const eventStatistics = {
guildCreate: 0,
guildDelete: 0,
guildEmojisUpdate: 0,
guildStickersUpdate: 0,
guildMemberAdd: 0,
guildMemberKick: 0,
guildMemberRemove: 0,
Expand Down
4 changes: 3 additions & 1 deletion src/bot/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ exports.ALL_EVENTS = [
'voiceStateUpdate',
'voiceChannelSwitch',
'guildEmojisUpdate',
'guildStickersUpdate',
'guildMemberBoostUpdate'
]

Expand Down Expand Up @@ -67,7 +68,8 @@ exports.EVENTS_USING_AUDITLOGS = [
'guildMemberRemove',
'guildMemberUpdate',
'voiceStateUpdate',
'guildEmojisUpdate'
'guildEmojisUpdate',
'guildStickersUpdate'
]

exports.EMBED_COLORS = {
Expand Down
1 change: 1 addition & 0 deletions src/db/interfaces/postgres/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const eventLogs = {
voiceStateUpdate: '',
voiceChannelSwitch: '',
guildEmojisUpdate: '',
guildStickersUpdate: '',
guildMemberNickUpdate: '',
guildMemberBoostUpdate: ''
}
Expand Down
2 changes: 2 additions & 0 deletions src/db/interfaces/postgres/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const eventList = [
'voiceStateUpdate',
'voiceChannelSwitch',
'guildEmojisUpdate',
'guildStickersUpdate',
'guildMemberNickUpdate'
]

Expand All @@ -54,6 +55,7 @@ const eventLogs = {
voiceStateUpdate: '',
voiceChannelSwitch: '',
guildEmojisUpdate: '',
guildStickersUpdate: '',
guildMemberNickUpdate: '',
guildMemberBoostUpdate: '',
guildMemberVerify: '' // I am a moron for having an object representing
Expand Down