@@ -9,12 +9,13 @@ import { Integration } from 'apps/api/src/integrations/entities/integration'
99import { WorkflowAction } from 'apps/api/src/workflow-actions/entities/workflow-action'
1010import { WorkflowTrigger } from 'apps/api/src/workflow-triggers/entities/workflow-trigger'
1111import { OperationRunOptions } from 'apps/runner/src/services/operation-runner.service'
12- import { StaticRunner } from 'apps/runner/src/services/static-runner.service'
1312import { InteractionResponseType , InteractionType , verifyKey } from 'discord-interactions'
1413import { Request } from 'express'
1514import { OpenAPIObject } from 'openapi3-ts'
1615import { 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'
1819
1920const CHANNEL_TYPES = {
2021 GUILD_TEXT : 0 ,
@@ -43,6 +44,8 @@ export class DiscordDefinition extends SingleIntegrationDefinition {
4344 integrationVersion = '10'
4445 schemaUrl = null
4546
47+ triggers = [ new NewSlashCommandGuild ( ) ]
48+
4649 // standard oauth2 refresh token won't work for discord ()
4750 async refreshCredentials ( credentials : Record < string , any > ) : Promise < Record < string , any > > {
4851 return credentials
@@ -73,36 +76,13 @@ export class DiscordDefinition extends SingleIntegrationDefinition {
7376 return opts
7477 }
7578
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-
9979 async beforeCreateWorkflowAction (
10080 workflowAction : Partial < WorkflowAction > ,
10181 integrationAction : IntegrationAction ,
10282 accountCredential : AccountCredential | null ,
10383 ) : 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 )
10686 }
10787
10888 async beforeUpdateWorkflowAction (
@@ -111,31 +91,17 @@ export class DiscordDefinition extends SingleIntegrationDefinition {
11191 integrationAction : IntegrationAction ,
11292 accountCredential : AccountCredential | null ,
11393 ) : 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 )
11696 }
11797
11898 async beforeCreateWorkflowTrigger (
11999 workflowTrigger : Partial < WorkflowTrigger > ,
120100 integrationTrigger : IntegrationTrigger ,
121101 accountCredential : AccountCredential | null ,
122102 ) : 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 )
139105 }
140106
141107 async beforeUpdateWorkflowTrigger (
@@ -144,20 +110,8 @@ export class DiscordDefinition extends SingleIntegrationDefinition {
144110 integrationTrigger : IntegrationTrigger ,
145111 accountCredential : AccountCredential | null ,
146112 ) : 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 )
161115 }
162116
163117 async onHookReceived (
@@ -192,6 +146,8 @@ export class DiscordDefinition extends SingleIntegrationDefinition {
192146 if ( type === InteractionType . APPLICATION_COMMAND ) {
193147 const { name, guild_id } = data
194148
149+ console . log ( `Received slash command ${ name } in guild ${ guild_id } ` , data )
150+
195151 const integrationTrigger = await injects . integrationTriggerService . findOne ( { key : 'newSlashCommandGuild' } ) // TODO check integration = discord
196152 if ( ! integrationTrigger ) {
197153 throw new Error ( `Integration trigger for discord slash command not configured correctly` )
0 commit comments