@@ -9,12 +9,13 @@ import { Integration } from 'apps/api/src/integrations/entities/integration'
9
9
import { WorkflowAction } from 'apps/api/src/workflow-actions/entities/workflow-action'
10
10
import { WorkflowTrigger } from 'apps/api/src/workflow-triggers/entities/workflow-trigger'
11
11
import { OperationRunOptions } from 'apps/runner/src/services/operation-runner.service'
12
- import { StaticRunner } from 'apps/runner/src/services/static-runner.service'
13
12
import { InteractionResponseType , InteractionType , verifyKey } from 'discord-interactions'
14
13
import { Request } from 'express'
15
14
import { OpenAPIObject } from 'openapi3-ts'
16
15
import { OptionsWithUrl } from 'request'
17
- import { GetAsyncSchemasProps , IntegrationHookInjects , RequestInterceptorOptions , StepInputs } from '../definition'
16
+ import { GetAsyncSchemasProps , IntegrationHookInjects , RequestInterceptorOptions } from '../../definition'
17
+ import { DiscordLib } from './discord.lib'
18
+ import { NewSlashCommandGuild } from './triggers/new-slash-command-guild.trigger'
18
19
19
20
const CHANNEL_TYPES = {
20
21
GUILD_TEXT : 0 ,
@@ -43,6 +44,8 @@ export class DiscordDefinition extends SingleIntegrationDefinition {
43
44
integrationVersion = '10'
44
45
schemaUrl = null
45
46
47
+ triggers = [ new NewSlashCommandGuild ( ) ]
48
+
46
49
// standard oauth2 refresh token won't work for discord ()
47
50
async refreshCredentials ( credentials : Record < string , any > ) : Promise < Record < string , any > > {
48
51
return credentials
@@ -73,36 +76,13 @@ export class DiscordDefinition extends SingleIntegrationDefinition {
73
76
return opts
74
77
}
75
78
76
- // We need to ensure the user has access to the guild id and the channel id belongs to the user
77
- // This must be run before create and update triggers and actions
78
- private async ensurePermissions ( inputs : StepInputs , accountCredential ?: AccountCredential | null ) {
79
- const credentials = accountCredential ?. credentials ?? { }
80
- inputs . guildId = credentials . guild_id // enforce guild id
81
- if ( inputs . channelId ) {
82
- const { outputs : channels } = await StaticRunner . run ( {
83
- definition : this ,
84
- actionKey : 'getGuildChannels' ,
85
- inputs : {
86
- guildId : credentials . guild_id ,
87
- } ,
88
- accountCredential,
89
- } )
90
- const hasChannel = ( channels as unknown as any [ ] ) . some (
91
- ( channel ) => channel . id . toString ( ) === inputs . channelId . toString ( ) ,
92
- )
93
- if ( ! hasChannel ) {
94
- throw new Error ( `Invalid permissions to access channel ${ inputs . channelId } ` )
95
- }
96
- }
97
- }
98
-
99
79
async beforeCreateWorkflowAction (
100
80
workflowAction : Partial < WorkflowAction > ,
101
81
integrationAction : IntegrationAction ,
102
82
accountCredential : AccountCredential | null ,
103
83
) : Promise < Partial < WorkflowAction > > {
104
- await this . ensurePermissions ( workflowAction . inputs ?? { } , accountCredential )
105
- return workflowAction
84
+ await DiscordLib . ensurePermissions ( workflowAction . inputs ?? { } , accountCredential ! . credentials )
85
+ return super . beforeCreateWorkflowAction ( workflowAction , integrationAction , accountCredential )
106
86
}
107
87
108
88
async beforeUpdateWorkflowAction (
@@ -111,31 +91,17 @@ export class DiscordDefinition extends SingleIntegrationDefinition {
111
91
integrationAction : IntegrationAction ,
112
92
accountCredential : AccountCredential | null ,
113
93
) : Promise < Partial < WorkflowAction > > {
114
- await this . ensurePermissions ( update . inputs ?? { } , accountCredential )
115
- return update
94
+ await DiscordLib . ensurePermissions ( update . inputs ?? { } , accountCredential ! . credentials )
95
+ return super . beforeUpdateWorkflowAction ( update , prevWorkflowAction , integrationAction , accountCredential )
116
96
}
117
97
118
98
async beforeCreateWorkflowTrigger (
119
99
workflowTrigger : Partial < WorkflowTrigger > ,
120
100
integrationTrigger : IntegrationTrigger ,
121
101
accountCredential : AccountCredential | null ,
122
102
) : Promise < Partial < WorkflowTrigger > > {
123
- await this . ensurePermissions ( workflowTrigger . inputs ?? { } , accountCredential )
124
- switch ( integrationTrigger . key ) {
125
- case 'newSlashCommandGuild' :
126
- await StaticRunner . run ( {
127
- definition : this ,
128
- actionKey : 'createGuildCommand' ,
129
- inputs : {
130
- applicationId : process . env . DISCORD_CLIENT_ID ,
131
- guildId : accountCredential ?. credentials . guild_id ,
132
- name : workflowTrigger . inputs ?. name ,
133
- description : workflowTrigger . inputs ?. description ,
134
- } ,
135
- accountCredential,
136
- } )
137
- }
138
- return workflowTrigger
103
+ await DiscordLib . ensurePermissions ( workflowTrigger . inputs ?? { } , accountCredential ! . credentials )
104
+ return super . beforeCreateWorkflowTrigger ( workflowTrigger , integrationTrigger , accountCredential )
139
105
}
140
106
141
107
async beforeUpdateWorkflowTrigger (
@@ -144,20 +110,8 @@ export class DiscordDefinition extends SingleIntegrationDefinition {
144
110
integrationTrigger : IntegrationTrigger ,
145
111
accountCredential : AccountCredential | null ,
146
112
) : Promise < Partial < WorkflowTrigger > > {
147
- await this . ensurePermissions ( update . inputs ?? { } , accountCredential )
148
- return update
149
- }
150
-
151
- async beforeDeleteWorkflowTrigger (
152
- workflowTrigger : Partial < WorkflowTrigger > ,
153
- integrationTrigger : IntegrationTrigger ,
154
- accountCredential : AccountCredential | null ,
155
- ) : Promise < void > {
156
- switch ( integrationTrigger . key ) {
157
- case 'newSlashCommandGuild' :
158
- // TODO delete command
159
- break
160
- }
113
+ await DiscordLib . ensurePermissions ( update . inputs ?? { } , accountCredential ! . credentials )
114
+ return super . beforeUpdateWorkflowTrigger ( update , prevWorkflowTrigger , integrationTrigger , accountCredential )
161
115
}
162
116
163
117
async onHookReceived (
@@ -192,6 +146,8 @@ export class DiscordDefinition extends SingleIntegrationDefinition {
192
146
if ( type === InteractionType . APPLICATION_COMMAND ) {
193
147
const { name, guild_id } = data
194
148
149
+ console . log ( `Received slash command ${ name } in guild ${ guild_id } ` , data )
150
+
195
151
const integrationTrigger = await injects . integrationTriggerService . findOne ( { key : 'newSlashCommandGuild' } ) // TODO check integration = discord
196
152
if ( ! integrationTrigger ) {
197
153
throw new Error ( `Integration trigger for discord slash command not configured correctly` )
0 commit comments