@@ -14,83 +14,66 @@ import { batphoneChannelId, raiderRoleId, wakeupChannelId } from "../../config";
14
14
import { authorizeByMemberRoles } from "../../shared/command/util" ;
15
15
import { officerRoleId , modRoleId , knightRoleId } from "../../config" ;
16
16
import { error } from "console" ;
17
- import { redisClient } from "../../redis/client" ;
18
- import { isObject } from "lodash" ;
19
17
import { container } from "tsyringe" ;
20
18
import { WakeupService } from "../wakeup/wakeup.service" ;
21
19
import { truncate } from "lodash" ;
22
20
import { RequestBotButtonCommand } from "./request-bot-button-command" ;
23
21
import { PublicAccountsFactory } from "../../services/bot/bot-factory" ;
24
22
import { LocationService } from "../../services/location" ;
25
- import { PrismaClient } from "@prisma/client" ;
26
23
27
24
class sendBp extends Subcommand {
28
25
public async execute ( interaction : CommandInteraction < CacheType > ) {
29
- // authorize
30
- authorizeByMemberRoles (
31
- [ officerRoleId , modRoleId , knightRoleId ] ,
32
- interaction
33
- ) ;
26
+ try {
27
+ // authorize
28
+ authorizeByMemberRoles (
29
+ [ officerRoleId , modRoleId , knightRoleId ] ,
30
+ interaction
31
+ ) ;
34
32
35
- const bpChannel = await getTextChannel ( batphoneChannelId ) ;
36
- const val = this . getOption ( "message" , interaction ) ?. value as string ;
37
- // const savedMsg = await redisClient.hGet("bp", String(val));
38
- const savedBp = await prismaClient . batphone . findFirst ( {
39
- where : {
40
- key : val ,
41
- } ,
42
- } ) ;
43
- const message = savedBp ?. message || val ;
44
- if ( typeof message === "string" ) {
45
- const formattedMessage = message . replace (
46
- / \\ n / g,
47
- `
33
+ const bpChannel = await getTextChannel ( batphoneChannelId ) ;
34
+ const val = this . getOption ( "message" , interaction ) ?. value as string ;
35
+ // const savedMsg = await redisClient.hGet("bp", String(val));
36
+ const savedBp = await prismaClient . batphone . findFirst ( {
37
+ where : {
38
+ key : val ,
39
+ } ,
40
+ } ) ;
41
+ savedBp
42
+ ? console . log (
43
+ `Found saved batphone ${ savedBp . key } for ${ savedBp . location } `
44
+ )
45
+ : console . log ( `No key found for ${ val } ` ) ;
46
+ const message = savedBp ?. message || val ;
47
+ if ( typeof message === "string" ) {
48
+ const formattedMessage = message . replace (
49
+ / \\ n / g,
50
+ `
48
51
`
49
- ) ;
50
- const components : ActionRowBuilder < MessageActionRowComponentBuilder > [ ] =
51
- await this . getBotButtonComponents ( savedBp ?. location || "" ) ;
52
- await bpChannel . send ( {
53
- content :
54
- `[${ interaction . user } ] <@&${ raiderRoleId } >
52
+ ) ;
53
+ const components : ActionRowBuilder < MessageActionRowComponentBuilder > [ ] =
54
+ await getBotButtonComponents ( savedBp ?. location || "" ) ;
55
+ await bpChannel . send ( {
56
+ content :
57
+ `[${ interaction . user } ] <@&${ raiderRoleId } >
55
58
` + formattedMessage ,
56
- components : savedBp ?. location ? components : undefined ,
57
- } ) ;
59
+ components : savedBp ?. location ? components : undefined ,
60
+ } ) ;
58
61
59
- interaction . editReply ( "Batphone posted: " + message ) ;
62
+ interaction . editReply ( "Batphone posted: " + message ) ;
60
63
61
- // Wakeup
62
- if ( wakeupChannelId ) {
63
- const wakeupService = container . resolve ( WakeupService ) ;
64
- wakeupService . runWakeup (
65
- `Batphone. ${ interaction . user } sent ${ message } `
66
- ) ;
67
- }
68
- } else {
69
- interaction . editReply ( "Failed to post batphone." ) ;
70
- }
71
- }
72
-
73
- private async getBotButtonComponents ( location : string ) {
74
- const bots = await PublicAccountsFactory . getService ( ) . getBotsForBatphone (
75
- location
76
- ) ;
77
- const components : ActionRowBuilder < MessageActionRowComponentBuilder > [ ] = [ ] ;
78
- let row ;
79
- for ( let i = 0 ; i < bots . length ; i ++ ) {
80
- if ( i % 5 === 0 ) {
81
- row = new ActionRowBuilder < MessageActionRowComponentBuilder > ( {
82
- type : ComponentType . ActionRow ,
83
- components : [ ] ,
84
- } ) ;
85
- components . push ( row ) ;
64
+ // Wakeup
65
+ if ( wakeupChannelId ) {
66
+ const wakeupService = container . resolve ( WakeupService ) ;
67
+ wakeupService . runWakeup (
68
+ `Batphone. ${ interaction . user } sent ${ message } `
69
+ ) ;
70
+ }
71
+ } else {
72
+ interaction . editReply ( "Failed to post batphone." ) ;
86
73
}
87
- row ?. addComponents (
88
- new RequestBotButtonCommand (
89
- `requestbot_${ bots [ i ] . name } `
90
- ) . getButtonBuilder ( bots [ i ] )
91
- ) ;
74
+ } catch ( error : unknown ) {
75
+ console . log ( "Failed to post batphone: " + error ) ;
92
76
}
93
- return components ;
94
77
}
95
78
96
79
public async getOptionAutocomplete (
@@ -117,6 +100,30 @@ class sendBp extends Subcommand {
117
100
}
118
101
}
119
102
103
+ export const getBotButtonComponents = async ( location : string ) => {
104
+ const bots = await PublicAccountsFactory . getService ( ) . getBotsForBatphone (
105
+ location
106
+ ) ;
107
+ const components : ActionRowBuilder < MessageActionRowComponentBuilder > [ ] = [ ] ;
108
+ let row ;
109
+ for ( let i = 0 ; i < bots . length ; i ++ ) {
110
+ if ( i % 5 === 0 ) {
111
+ row = new ActionRowBuilder < MessageActionRowComponentBuilder > ( {
112
+ type : ComponentType . ActionRow ,
113
+ components : [ ] ,
114
+ } ) ;
115
+ components . push ( row ) ;
116
+ }
117
+ console . log ( `adding button for ${ bots [ i ] . name } ` ) ;
118
+ row ?. addComponents (
119
+ new RequestBotButtonCommand (
120
+ `requestbot_${ bots [ i ] . name } `
121
+ ) . getButtonBuilder ( bots [ i ] )
122
+ ) ;
123
+ }
124
+ return components ;
125
+ } ;
126
+
120
127
class setBp extends Subcommand {
121
128
public async execute ( interaction : CommandInteraction < CacheType > ) {
122
129
// authorize
@@ -279,8 +286,18 @@ ${formattedMessage}
279
286
Location: ${ savedMsg ?. location || "NO LOCATION SET" }
280
287
To change this message, use \`/bp unset ${ key } \` and then \`/bp set\` to set a new message.
281
288
` ;
289
+ savedMsg
290
+ ? console . log (
291
+ `Found saved batphone ${ savedMsg . key } for ${ savedMsg . location } `
292
+ )
293
+ : console . log ( `No key found for ${ val } ` ) ;
294
+ const components : ActionRowBuilder < MessageActionRowComponentBuilder > [ ] =
295
+ await getBotButtonComponents ( savedMsg ?. location || "" ) ;
282
296
if ( interaction . channel ) {
283
- interaction . channel . send ( replyMsg ) ; // todo: send message in channel
297
+ interaction . channel . send ( {
298
+ content : replyMsg ,
299
+ components : savedMsg ?. location ? components : undefined ,
300
+ } ) ; // todo: send message in channel
284
301
interaction . deleteReply ( ) ;
285
302
}
286
303
}
0 commit comments