diff --git a/packages/backend/src/apps/discord/actions/index.ts b/packages/backend/src/apps/discord/actions/index.ts
new file mode 100644
index 00000000..d6d1738d
--- /dev/null
+++ b/packages/backend/src/apps/discord/actions/index.ts
@@ -0,0 +1 @@
+export default [];
diff --git a/packages/backend/src/apps/discord/assets/favicon.svg b/packages/backend/src/apps/discord/assets/favicon.svg
new file mode 100644
index 00000000..0483a9d3
--- /dev/null
+++ b/packages/backend/src/apps/discord/assets/favicon.svg
@@ -0,0 +1,4 @@
+
\ No newline at end of file
diff --git a/packages/backend/src/apps/discord/auth/create-auth-data.ts b/packages/backend/src/apps/discord/auth/create-auth-data.ts
new file mode 100644
index 00000000..79ae7fbd
--- /dev/null
+++ b/packages/backend/src/apps/discord/auth/create-auth-data.ts
@@ -0,0 +1,27 @@
+import { IField, IGlobalVariable } from '@automatisch/types';
+import { URLSearchParams } from 'url';
+import scopes from '../common/scopes';
+
+export default async function createAuthData($: IGlobalVariable) {
+ try {
+ const oauthRedirectUrlField = $.app.auth.fields.find(
+ (field: IField) => field.key == 'oAuthRedirectUrl'
+ );
+ const callbackUrl = oauthRedirectUrlField.value as string;
+
+ const searchParams = new URLSearchParams({
+ client_id: $.auth.data.consumerKey as string,
+ redirect_uri: callbackUrl,
+ response_type: 'code',
+ scope: scopes.join(' '),
+ });
+
+ const url = `${$.app.apiBaseUrl}/oauth2/authorize?${searchParams.toString()}`;
+
+ await $.auth.set({ url });
+ } catch (error) {
+ throw new Error(
+ `Error occured while verifying credentials: ${error}`
+ );
+ }
+}
diff --git a/packages/backend/src/apps/discord/auth/index.ts b/packages/backend/src/apps/discord/auth/index.ts
new file mode 100644
index 00000000..f247e86f
--- /dev/null
+++ b/packages/backend/src/apps/discord/auth/index.ts
@@ -0,0 +1,221 @@
+import createAuthData from './create-auth-data';
+import verifyCredentials from './verify-credentials';
+import isStillVerified from './is-still-verified';
+
+export default {
+ fields: [
+ {
+ key: 'oAuthRedirectUrl',
+ label: 'OAuth Redirect URL',
+ type: 'string' as const,
+ required: true,
+ readOnly: true,
+ value: '{WEB_APP_URL}/app/discord/connections/add',
+ placeholder: null,
+ description: 'When asked to input an OAuth callback or redirect URL in Discord OAuth, enter the URL above.',
+ docUrl: 'https://automatisch.io/docs/discord#oauth-redirect-url',
+ clickToCopy: true
+ },
+ {
+ key: 'consumerKey',
+ label: 'Consumer Key',
+ type: 'string' as const,
+ required: true,
+ readOnly: false,
+ value: null,
+ placeholder: null,
+ description: null,
+ docUrl: 'https://automatisch.io/docs/discord#consumer-key',
+ clickToCopy: false
+ },
+ {
+ key: 'consumerSecret',
+ label: 'Consumer Secret',
+ type: 'string' as const,
+ required: true,
+ readOnly: false,
+ value: null,
+ placeholder: null,
+ description: null,
+ docUrl: 'https://automatisch.io/docs/discord#consumer-secret',
+ clickToCopy: false
+ }
+ ],
+ authenticationSteps: [
+ {
+ step: 1,
+ type: 'mutation' as const,
+ name: 'createConnection',
+ arguments: [
+ {
+ name: 'key',
+ value: '{key}'
+ },
+ {
+ name: 'formattedData',
+ value: null,
+ properties: [
+ {
+ name: 'consumerKey',
+ value: '{fields.consumerKey}'
+ },
+ {
+ name: 'consumerSecret',
+ value: '{fields.consumerSecret}'
+ }
+ ]
+ }
+ ]
+ },
+ {
+ step: 2,
+ type: 'mutation' as const,
+ name: 'createAuthData',
+ arguments: [
+ {
+ name: 'id',
+ value: '{createConnection.id}'
+ }
+ ]
+ },
+ {
+ step: 3,
+ type: 'openWithPopup' as const,
+ name: 'openAuthPopup',
+ arguments: [
+ {
+ name: 'url',
+ value: '{createAuthData.url}'
+ }
+ ]
+ },
+ {
+ step: 4,
+ type: 'mutation' as const,
+ name: 'updateConnection',
+ arguments: [
+ {
+ name: 'id',
+ value: '{createConnection.id}'
+ },
+ {
+ name: 'formattedData',
+ value: null,
+ properties: [
+ {
+ name: 'oauthVerifier',
+ value: '{openAuthPopup.code}'
+ }
+ ]
+ }
+ ]
+ },
+ {
+ step: 5,
+ type: 'mutation' as const,
+ name: 'verifyConnection',
+ arguments: [
+ {
+ name: 'id',
+ value: '{createConnection.id}'
+ }
+ ]
+ }
+ ],
+ reconnectionSteps: [
+ {
+ step: 1,
+ type: 'mutation' as const,
+ name: 'resetConnection',
+ arguments: [
+ {
+ name: 'id',
+ value: '{connection.id}'
+ }
+ ]
+ },
+ {
+ step: 2,
+ type: 'mutation' as const,
+ name: 'updateConnection',
+ arguments: [
+ {
+ name: 'id',
+ value: '{connection.id}'
+ },
+ {
+ name: 'formattedData',
+ value: null,
+ properties: [
+ {
+ name: 'consumerKey',
+ value: '{fields.consumerKey}'
+ },
+ {
+ name: 'consumerSecret',
+ value: '{fields.consumerSecret}'
+ }
+ ]
+ }
+ ]
+ },
+ {
+ step: 3,
+ type: 'mutation' as const,
+ name: 'createAuthData',
+ arguments: [
+ {
+ name: 'id',
+ value: '{connection.id}'
+ }
+ ]
+ },
+ {
+ step: 4,
+ type: 'openWithPopup' as const,
+ name: 'openAuthPopup',
+ arguments: [
+ {
+ name: 'url',
+ value: '{createAuthData.url}'
+ }
+ ]
+ },
+ {
+ step: 5,
+ type: 'mutation' as const,
+ name: 'updateConnection',
+ arguments: [
+ {
+ name: 'id',
+ value: '{connection.id}'
+ },
+ {
+ name: 'formattedData',
+ value: null,
+ properties: [
+ {
+ name: 'oauthVerifier',
+ value: '{openAuthPopup.code}'
+ }
+ ]
+ }
+ ]
+ },
+ {
+ step: 6,
+ type: 'mutation' as const,
+ name: 'verifyConnection',
+ arguments: [
+ {
+ name: 'id',
+ value: '{connection.id}'
+ }
+ ]
+ }
+ ],
+
+ createAuthData,
+ verifyCredentials,
+ isStillVerified,
+};
diff --git a/packages/backend/src/apps/discord/auth/is-still-verified.ts b/packages/backend/src/apps/discord/auth/is-still-verified.ts
new file mode 100644
index 00000000..b34d1517
--- /dev/null
+++ b/packages/backend/src/apps/discord/auth/is-still-verified.ts
@@ -0,0 +1,14 @@
+import { IGlobalVariable } from '@automatisch/types';
+import getCurrentUser from '../common/get-current-user';
+
+const isStillVerified = async ($: IGlobalVariable) => {
+ try {
+ await getCurrentUser($);
+
+ return true;
+ } catch (error) {
+ return false;
+ }
+};
+
+export default isStillVerified;
diff --git a/packages/backend/src/apps/discord/auth/verify-credentials.ts b/packages/backend/src/apps/discord/auth/verify-credentials.ts
new file mode 100644
index 00000000..b7c5c3ad
--- /dev/null
+++ b/packages/backend/src/apps/discord/auth/verify-credentials.ts
@@ -0,0 +1,50 @@
+import { IGlobalVariable, IField } from '@automatisch/types';
+import { URLSearchParams } from 'url';
+import scopes from '../common/scopes';
+import getCurrentUser from '../common/get-current-user';
+
+const verifyCredentials = async ($: IGlobalVariable) => {
+ const oauthRedirectUrlField = $.app.auth.fields.find(
+ (field: IField) => field.key == 'oAuthRedirectUrl'
+ );
+ const callbackUrl = oauthRedirectUrlField.value as string;
+ const params = new URLSearchParams({
+ client_id: $.auth.data.consumerKey as string,
+ redirect_uri: callbackUrl,
+ response_type: 'code',
+ scope: scopes.join(' '),
+ client_secret: $.auth.data.consumerSecret as string,
+ code: $.auth.data.oauthVerifier as string,
+ grant_type: 'authorization_code',
+ });
+ const { data: verifiedCredentials } = await $.http.post(
+ '/oauth2/token',
+ params.toString()
+ );
+
+ const {
+ access_token: accessToken,
+ refresh_token: refreshToken,
+ expires_in: expiresIn,
+ scope: scope,
+ token_type: tokenType,
+ } = verifiedCredentials;
+
+ await $.auth.set({
+ accessToken,
+ refreshToken,
+ expiresIn,
+ scope,
+ tokenType,
+ })
+
+ const user = await getCurrentUser($);
+
+ await $.auth.set({
+ userId: user.id,
+ screenName: user.username,
+ email: user.email,
+ });
+};
+
+export default verifyCredentials;
diff --git a/packages/backend/src/apps/discord/common/add-auth-header.ts b/packages/backend/src/apps/discord/common/add-auth-header.ts
new file mode 100644
index 00000000..6f879476
--- /dev/null
+++ b/packages/backend/src/apps/discord/common/add-auth-header.ts
@@ -0,0 +1,12 @@
+import { TBeforeRequest } from '@automatisch/types';
+
+const addAuthHeader: TBeforeRequest = ($, requestConfig) => {
+ const { tokenType, accessToken } = $.auth.data;
+ if (tokenType && accessToken) {
+ requestConfig.headers.Authorization = `${tokenType} ${accessToken}`;
+ }
+
+ return requestConfig;
+};
+
+export default addAuthHeader;
diff --git a/packages/backend/src/apps/discord/common/get-current-user.ts b/packages/backend/src/apps/discord/common/get-current-user.ts
new file mode 100644
index 00000000..8b79e0c5
--- /dev/null
+++ b/packages/backend/src/apps/discord/common/get-current-user.ts
@@ -0,0 +1,10 @@
+import { IGlobalVariable, IJSONObject } from '@automatisch/types';
+
+const getCurrentUser = async ($: IGlobalVariable): Promise => {
+ const response = await $.http.get('/users/@me');
+ const currentUser = response.data;
+
+ return currentUser;
+};
+
+export default getCurrentUser;
diff --git a/packages/backend/src/apps/discord/common/scopes.ts b/packages/backend/src/apps/discord/common/scopes.ts
new file mode 100644
index 00000000..f0e5bba5
--- /dev/null
+++ b/packages/backend/src/apps/discord/common/scopes.ts
@@ -0,0 +1,3 @@
+const scopes = ['identify', 'email'];
+
+export default scopes;
diff --git a/packages/backend/src/apps/discord/index.d.ts b/packages/backend/src/apps/discord/index.d.ts
new file mode 100644
index 00000000..e69de29b
diff --git a/packages/backend/src/apps/discord/index.ts b/packages/backend/src/apps/discord/index.ts
new file mode 100644
index 00000000..9734fd34
--- /dev/null
+++ b/packages/backend/src/apps/discord/index.ts
@@ -0,0 +1,20 @@
+import defineApp from '../../helpers/define-app';
+import addAuthHeader from './common/add-auth-header';
+import auth from './auth';
+import actions from './actions';
+import triggers from './triggers';
+
+export default defineApp({
+ name: 'Discord',
+ key: 'discord',
+ iconUrl: '{BASE_URL}/apps/discord/assets/favicon.svg',
+ authDocUrl: 'https://automatisch.io/docs/connections/discord',
+ supportsConnections: true,
+ baseUrl: 'https://discord.com',
+ apiBaseUrl: 'https://discord.com/api',
+ primaryColor: '5865f2',
+ beforeRequest: [addAuthHeader],
+ auth,
+ triggers,
+ actions,
+});
diff --git a/packages/backend/src/apps/discord/triggers/index.ts b/packages/backend/src/apps/discord/triggers/index.ts
new file mode 100644
index 00000000..d6d1738d
--- /dev/null
+++ b/packages/backend/src/apps/discord/triggers/index.ts
@@ -0,0 +1 @@
+export default [];
diff --git a/packages/backend/src/helpers/global-variable.ts b/packages/backend/src/helpers/global-variable.ts
index bd7bdfe9..3f38316c 100644
--- a/packages/backend/src/helpers/global-variable.ts
+++ b/packages/backend/src/helpers/global-variable.ts
@@ -39,6 +39,8 @@ const globalVariable = async (
...args,
},
});
+
+ $.auth.data = connection.formattedData;
}
return null;