-
Notifications
You must be signed in to change notification settings - Fork 59
/
stickyMessageHandler.ts
50 lines (41 loc) · 1.21 KB
/
stickyMessageHandler.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
import { Message } from 'discord.js'
import { StickyMessage } from '@prisma/client'
import { Logger } from '@/types/Logger'
import { getCache } from '@/utils/cache'
import { isChannelLock } from './channelLock'
import { incCounter } from './messageCounter'
import {
STICKY_CACHE_PREFIX,
isNeedToUpdateMessage,
pushMessageToBottom,
} from './stickyMessages'
export async function stickyMessageHandler(
message: Message,
log: Logger = console,
) {
// TODO: fix this to handle the command dynamically
if (
message.content.startsWith('?stickao-set') ||
message.content.startsWith('?stickao-remove')
) {
return
}
const stickyMessage = getCache(
`${STICKY_CACHE_PREFIX}-${message.channelId}`,
) as StickyMessage
// if channel not has any sticky message -> do nothing
if (!stickyMessage) {
return
}
// if message doesn't need to update -> increase message counter
if (!isNeedToUpdateMessage(message.channelId)) {
incCounter(message.channelId)
return
}
// if message is updating -> do nothing
if (isChannelLock(message.channelId)) {
return
}
// if message need to update -> push sticky message to bottom
await pushMessageToBottom(message, stickyMessage, log)
}