Refactored into simple Discord node so it can be extended later

This commit is contained in:
neomaking 2019-10-20 23:26:58 +02:00
parent f2af8d706d
commit f033e4c886
4 changed files with 33 additions and 84 deletions

View file

@ -1,18 +0,0 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class DiscordApi implements ICredentialType {
name = 'discordApi';
displayName = 'Discord API';
properties = [
{
displayName: 'Webhook URI',
name: 'webhookUri',
type: 'string' as NodePropertyTypes,
default: '',
},
];
}

View file

@ -6,27 +6,21 @@ import {
INodeType, INodeType,
} from 'n8n-workflow'; } from 'n8n-workflow';
export class DiscordWebhook implements INodeType { export class Discord implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'Discord Webhook', displayName: 'Discord',
name: 'discordwebhook', name: 'discord',
icon: 'file:discord.png', icon: 'file:discord.png',
group: ['output'], group: ['output'],
version: 1, version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', subtitle: '={{$parameter["resource"]}}',
description: 'Sends data to Discord', description: 'Sends data to Discord',
defaults: { defaults: {
name: 'Discord Webhook', name: 'Discord',
color: '#7289da', color: '#7289da',
}, },
inputs: ['main'], inputs: ['main'],
outputs: ['main'], outputs: ['main'],
credentials: [
{
name: 'discordApi',
required: true,
}
],
properties: [ properties: [
{ {
displayName: 'Resource', displayName: 'Resource',
@ -34,41 +28,14 @@ export class DiscordWebhook implements INodeType {
type: 'options', type: 'options',
options: [ options: [
{ {
name: 'Message', name: 'Webhook',
value: 'message', value: 'webhook',
}, },
], ],
default: 'message', default: 'webhook',
description: 'The resource to operate on.', description: 'The resource to operate on.',
}, },
// ----------------------------------
// operations
// ----------------------------------
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'message',
],
},
},
options: [
{
name: 'Post',
value: 'post',
description: 'Post a message into a channel',
},
],
default: 'post',
description: 'The operation to perform.',
},
// ---------------------------------- // ----------------------------------
// message // message
// ---------------------------------- // ----------------------------------
@ -76,6 +43,23 @@ export class DiscordWebhook implements INodeType {
// ---------------------------------- // ----------------------------------
// message:post // message:post
// ---------------------------------- // ----------------------------------
{
displayName: 'Webhook URL',
name: 'webhookUri',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
displayOptions: {
show: {
resource: [
'webhook',
],
},
},
description: 'The webhook url',
},
{ {
displayName: 'Text', displayName: 'Text',
name: 'text', name: 'text',
@ -86,11 +70,8 @@ export class DiscordWebhook implements INodeType {
default: '', default: '',
displayOptions: { displayOptions: {
show: { show: {
operation: [
'post'
],
resource: [ resource: [
'message', 'webhook',
], ],
}, },
}, },
@ -106,13 +87,6 @@ export class DiscordWebhook implements INodeType {
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
let responseData; let responseData;
const credentials = this.getCredentials('discordApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
let operation: string;
let resource: string; let resource: string;
let requestMethod = 'POST'; let requestMethod = 'POST';
@ -120,20 +94,14 @@ export class DiscordWebhook implements INodeType {
let body: IDataObject; let body: IDataObject;
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
const webhookUri = this.getNodeParameter('webhookUri', i) as string;
body = {}; body = {};
resource = this.getNodeParameter('resource', i) as string; resource = this.getNodeParameter('resource', i) as string;
operation = this.getNodeParameter('operation', i) as string;
if (resource === 'message') { if (resource === 'webhook') {
if (operation === 'post') { requestMethod = 'POST';
// ---------------------------------- body.content = this.getNodeParameter('text', i) as string;
// message:post
// ----------------------------------
requestMethod = 'POST';
body.content = this.getNodeParameter('text', i) as string;
}
} else { } else {
throw new Error(`The resource "${resource}" is not known!`); throw new Error(`The resource "${resource}" is not known!`);
} }
@ -141,7 +109,7 @@ export class DiscordWebhook implements INodeType {
const options = { const options = {
method: requestMethod, method: requestMethod,
body, body,
uri: `${credentials.webhookUri}`, uri: `${webhookUri}`,
headers: { headers: {
'content-type': 'application/json; charset=utf-8' 'content-type': 'application/json; charset=utf-8'
}, },

View file

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

View file

@ -51,8 +51,7 @@
"dist/credentials/TelegramApi.credentials.js", "dist/credentials/TelegramApi.credentials.js",
"dist/credentials/TrelloApi.credentials.js", "dist/credentials/TrelloApi.credentials.js",
"dist/credentials/TwilioApi.credentials.js", "dist/credentials/TwilioApi.credentials.js",
"dist/credentials/TypeformApi.credentials.js", "dist/credentials/TypeformApi.credentials.js"
"dist/credentials/DiscordApi.credentials.js"
], ],
"nodes": [ "nodes": [
"dist/nodes/ActiveCampaign/ActiveCampaign.node.js", "dist/nodes/ActiveCampaign/ActiveCampaign.node.js",
@ -65,7 +64,7 @@
"dist/nodes/Chargebee/ChargebeeTrigger.node.js", "dist/nodes/Chargebee/ChargebeeTrigger.node.js",
"dist/nodes/Cron.node.js", "dist/nodes/Cron.node.js",
"dist/nodes/Dropbox/Dropbox.node.js", "dist/nodes/Dropbox/Dropbox.node.js",
"dist/nodes/DiscordWebhook/DiscordWebhook.node.js", "dist/nodes/Discord/Discord.node.js",
"dist/nodes/EditImage.node.js", "dist/nodes/EditImage.node.js",
"dist/nodes/EmailReadImap.node.js", "dist/nodes/EmailReadImap.node.js",
"dist/nodes/EmailSend.node.js", "dist/nodes/EmailSend.node.js",