-
Notifications
You must be signed in to change notification settings - Fork 59
/
stickyMessageRemove.ts
70 lines (59 loc) · 2 KB
/
stickyMessageRemove.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { Message, PermissionsBitField, TextChannel } from 'discord.js'
import { StickyMessage } from '@prisma/client'
import { prisma } from '@/prisma'
import { Logger } from '@/types/Logger'
import { getCache, removeCache } from '@/utils/cache'
import { sendDm } from '@/utils/sendDm'
import { STICKY_CACHE_PREFIX } from './stickyMessages'
export async function stickyMessageRemove(
message: Message,
log: Logger = console,
) {
if (!message.content.startsWith('?stickao-remove')) {
return
}
const channel = message.channel as TextChannel
// Check if the user has the 'MANAGE_MESSAGES' permission
const authorPermissions = channel.permissionsFor(message.author)
if (!authorPermissions?.has(PermissionsBitField.Flags.ManageMessages)) {
await sendDm(message, {
content:
'You must have the "Manage Messages" permission to use this command.',
})
return
}
try {
// Retrieve the sticky message with the specified order from the database
const stickyMessageEntity = getCache(
`${STICKY_CACHE_PREFIX}-${message.channelId}`,
) as StickyMessage
// If the sticky message exists, remove it from the database
if (stickyMessageEntity) {
await prisma.stickyMessage.delete({
where: {
channelId: message.channelId,
},
})
removeCache(`${STICKY_CACHE_PREFIX}-${message.channelId}`)
const stickyMessage = await channel.messages.fetch(
stickyMessageEntity.messageId,
)
await stickyMessage.delete()
log.info(`Sticky message removed: ${stickyMessageEntity.message}`)
await sendDm(message, {
content: 'Successfully removed the sticky message.',
})
} else {
await sendDm(message, {
content: 'Not found message in this channel',
})
}
} catch (error) {
log.error(`Error removing sticky message:`, error)
await sendDm(message, {
content: 'An error occurred while removing the sticky message.',
})
} finally {
await message.delete()
}
}