1
1
import { CronJob } from 'cron' ;
2
- import { randomUUID } from 'crypto' ;
3
2
import type {
4
3
ChatInputCommandInteraction ,
5
4
Client ,
@@ -26,6 +25,8 @@ const frequencyDisplay = {
26
25
27
26
const inMemoryJobList : { id : string ; job : CronJob } [ ] = [ ] ;
28
27
28
+ const generateId = ( ) => Math . random ( ) . toString ( 36 ) . slice ( 2 ) ;
29
+
29
30
export type Frequency = keyof typeof cronTime ;
30
31
31
32
export const isFrequency = ( frequency : string ) : frequency is Frequency => {
@@ -34,7 +35,7 @@ export const isFrequency = (frequency: string): frequency is Frequency => {
34
35
35
36
export const hasPermission = ( interaction : ChatInputCommandInteraction ) => {
36
37
if ( ! isModo ( interaction . member ) ) {
37
- interaction . reply ( 'You are not allowed to use this command' ) . catch ( console . error ) ;
38
+ void interaction . reply ( 'You are not allowed to use this command' ) ;
38
39
return false ;
39
40
}
40
41
return true ;
@@ -63,7 +64,7 @@ export const createRecurringMessage = (
63
64
} ;
64
65
65
66
export const addRecurringMessage = async ( interaction : ChatInputCommandInteraction ) => {
66
- const jobId = randomUUID ( ) ;
67
+ const jobId = generateId ( ) ;
67
68
const channelId = interaction . channelId ;
68
69
const frequency = interaction . options . getString ( 'frequency' , true ) ;
69
70
if ( ! isFrequency ( frequency ) ) {
@@ -124,16 +125,45 @@ export const listRecurringMessages = async (interaction: ChatInputCommandInterac
124
125
return ;
125
126
}
126
127
127
- const recurringMessagesList = recurringMessages
128
- . map (
129
- ( { id, frequency, message } ) =>
130
- `id: ${ id } - frequency: ${ frequency } - ${ message . substring ( 0 , 50 ) } ${
131
- message . length > 50 ? '...' : ''
132
- } `,
133
- )
134
- . join ( '\n' ) ;
128
+ const messagesInCurrentGuild = recurringMessages . filter (
129
+ ( { channelId } ) => interaction . guild ?. channels . cache . has ( channelId ) ,
130
+ ) ;
131
+
132
+ const messagesByChannelName = messagesInCurrentGuild . reduce <
133
+ Record < string , Array < { id : string ; frequency : string ; message : string } > >
134
+ > ( ( acc , { id, frequency, message, channelId } ) => {
135
+ const channel = interaction . guild ?. channels . cache . get ( channelId ) ;
136
+ if ( channel === undefined ) throw new Error ( 'Channel not found' ) ;
137
+
138
+ const { name } = channel ;
139
+ const currentMessages = acc [ name ] ;
140
+
141
+ if ( currentMessages === undefined ) {
142
+ return { ...acc , [ name ] : [ { id, frequency, message } ] } ;
143
+ }
144
+
145
+ currentMessages . push ( { id, frequency, message } ) ;
146
+
147
+ return acc ;
148
+ } , { } ) ;
149
+
150
+ const embeds = Object . entries ( messagesByChannelName ) . map ( ( [ channelName , messages ] ) => {
151
+ const fields = messages . map ( ( { id, frequency, message } ) => ( {
152
+ name : `⏰ - ${ frequencyDisplay [ frequency as Frequency ] } (id: ${ id } )` ,
153
+ value : message . substring ( 0 , 1000 ) + ( message . length > 1000 ? '...' : '' ) ,
154
+ } ) ) ;
155
+
156
+ return {
157
+ title : `# ${ channelName } ` ,
158
+ color : 0x0099ff ,
159
+ fields,
160
+ footer : {
161
+ text : '\u2800' . repeat ( 256 ) , // hackish way have even width for all embeds
162
+ } ,
163
+ } ;
164
+ } ) ;
135
165
136
- await interaction . reply ( recurringMessagesList ) ;
166
+ await interaction . reply ( { embeds } ) ;
137
167
} ;
138
168
139
169
export const relaunchRecurringMessages = async ( client : Client < true > ) => {
0 commit comments