feat(Discord Node): Add additional options (#2918)

* 🔖 Discord Node v2.0

* Updated image from png to svg

* Added correct versioning

* Added old for versioning purposes

* Added other parameter for the url

* Fixed subtitle added multipart option for payload

* Removed unused imports

* Changed data type for binary file

* Removed console.log

* Moved the additional fields to an option field + fixed some bugs

* Refactored node into one version

* Removed any type

* Fixed some broken behaviour

* Minor fixes for discord node

*  Fix parameter name

Co-authored-by: Timeraa <me@timeraa.dev>
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
agobrech 2022-04-15 10:04:27 +02:00 committed by GitHub
parent 18dee373d5
commit 310bffe713
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 777 additions and 58510 deletions

59013
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -2,9 +2,7 @@
"node": "n8n-nodes-base.discord", "node": "n8n-nodes-base.discord",
"nodeVersion": "1.0", "nodeVersion": "1.0",
"codexVersion": "1.0", "codexVersion": "1.0",
"categories": [ "categories": ["Communication"],
"Communication"
],
"resources": { "resources": {
"credentialDocumentation": [ "credentialDocumentation": [
{ {

View file

@ -1,4 +1,3 @@
import { get } from 'lodash';
import { IExecuteFunctions } from 'n8n-core'; import { IExecuteFunctions } from 'n8n-core';
import { import {
IDataObject, IDataObject,
@ -10,6 +9,8 @@ import {
NodeOperationError, NodeOperationError,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { DiscordAttachment, DiscordWebhook } from './Interfaces';
export class Discord implements INodeType { export class Discord implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'Discord', displayName: 'Discord',
@ -23,7 +24,7 @@ export class Discord implements INodeType {
}, },
inputs: ['main'], inputs: ['main'],
outputs: ['main'], outputs: ['main'],
properties: [ properties:[
{ {
displayName: 'Webhook URL', displayName: 'Webhook URL',
name: 'webhookUri', name: 'webhookUri',
@ -31,18 +32,95 @@ export class Discord implements INodeType {
typeOptions: { typeOptions: {
alwaysOpenEditWindow: true, alwaysOpenEditWindow: true,
}, },
required: true,
default: '', default: '',
description: 'The webhook url.', placeholder: 'https://discord.com/api/webhooks/ID/TOKEN',
}, },
{ {
displayName: 'Text', displayName: 'Content',
name: 'text', name: 'text',
type: 'string', type: 'string',
typeOptions: { typeOptions: {
maxValue: 2000,
alwaysOpenEditWindow: true, alwaysOpenEditWindow: true,
}, },
default: '', default: '',
description: 'The text to send.', required: false,
placeholder: 'Hello World!',
},
{
displayName: 'Additional Fields',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Allowed Mentions',
name: 'allowedMentions',
type: 'json',
typeOptions: { alwaysOpenEditWindow: true, editor: 'code' },
default: '',
},
{
displayName: 'Attachments',
name: 'attachments',
type: 'json',
typeOptions: { alwaysOpenEditWindow: true, editor: 'code' },
default: '',
},
{
displayName: 'Avatar URL',
name: 'avatarUrl',
type: 'string',
default: '',
required: false,
},
{
displayName: 'Components',
name: 'components',
type: 'json',
typeOptions: { alwaysOpenEditWindow: true, editor: 'code' },
default: '',
},
{
displayName: 'Embeds',
name: 'embeds',
type: 'json',
typeOptions: { alwaysOpenEditWindow: true, editor: 'code' },
default: '',
required: false,
},
{
displayName: 'Flags',
name: 'flags',
type: 'number',
default: '',
},
{
displayName: 'JSON Payload',
name: 'payloadJson',
type: 'json',
typeOptions: { alwaysOpenEditWindow: true, editor: 'code' },
default: '',
},
{
displayName: 'Username',
name: 'username',
type: 'string',
default: '',
required: false,
placeholder: 'User',
},
{
displayName: 'TTS',
name: 'tts',
type: 'boolean',
default: false,
required: false,
description: 'Should this message be sent as a Text To Speech message?',
},
],
}, },
], ],
}; };
@ -50,57 +128,136 @@ export class Discord implements INodeType {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
const requestMethod = 'POST'; const webhookUri = this.getNodeParameter('webhookUri', 0, '') as string;
// For Post if (!webhookUri) throw Error('Webhook uri is required.');
let body: IDataObject;
const items = this.getInputData();
const length = items.length as number
for (let i = 0; i < length; i++) {
const body: DiscordWebhook = {};
for (let i = 0; i < items.length; i++) {
const webhookUri = this.getNodeParameter('webhookUri', i) as string; const webhookUri = this.getNodeParameter('webhookUri', i) as string;
body = {};
body.content = this.getNodeParameter('text', i) as string; body.content = this.getNodeParameter('text', i) as string;
const options = this.getNodeParameter('options', i) as IDataObject;
const options = { if (!body.content && !options.embeds) {
method: requestMethod, throw new Error('Either content or embeds must be set.');
}
if (options.embeds) {
try {
//@ts-expect-error
body.embeds = JSON.parse(options.embeds);
if (!Array.isArray(body.embeds)) {
throw new Error('Embeds must be an array of embeds.');
}
} catch (e) {
throw new Error('Embeds must be valid JSON.');
}
}
if (options.username) {
body.username = options.username as string;
}
if (options.components) {
try {
//@ts-expect-error
body.components = JSON.parse(options.components);
} catch (e) {
throw new Error('Components must be valid JSON.');
}
}
if (options.allowed_mentions) {
//@ts-expect-error
body.allowed_mentions = JSON.parse(options.allowed_mentions);
}
if (options.avatarUrl) {
body.avatar_url = options.avatarUrl as string;
}
if (options.flags) {
body.flags = options.flags as number;
}
if (options.tts) {
body.tts = options.tts as boolean;
}
if (options.payloadJson) {
//@ts-expect-error
body.payload_json = JSON.parse(options.payloadJson);
}
if (options.attachments) {
//@ts-expect-error
body.attachments = JSON.parse(options.attachments as DiscordAttachment[]);
}
//* Not used props, delete them from the payload as Discord won't need them :^
if (!body.content) delete body.content;
if (!body.username) delete body.username;
if (!body.avatar_url) delete body.avatar_url;
if (!body.embeds) delete body.embeds;
if (!body.allowed_mentions) delete body.allowed_mentions;
if (!body.flags) delete body.flags;
if (!body.components) delete body.components;
if (!body.payload_json) delete body.payload_json;
if (!body.attachments) delete body.attachments;
let requestOptions;
if(!body.payload_json){
requestOptions = {
method: 'POST',
body, body,
uri: `${webhookUri}`, uri: webhookUri,
headers: { headers: {
'content-type': 'application/json; charset=utf-8', 'content-type': 'application/json; charset=utf-8',
}, },
json: true, json: true,
}; };
}else {
requestOptions = {
method: 'POST',
body,
uri: webhookUri,
headers: {
'content-type': 'multipart/form-data; charset=utf-8',
},
};
}
let maxTries = 5; let maxTries = 5;
do { do {
try { try {
await this.helpers.request(options); await this.helpers.request(requestOptions);
break; break;
} catch (error) { } catch (error) {
if (error.statusCode === 429) { if (error.statusCode === 429) {
// Waiting rating limit //* Await ratelimit to be over
await new Promise((resolve) => { await new Promise<void>((resolve) =>
setTimeout(async () => { setTimeout(resolve, error.response.body.retry_after || 150),
// @ts-ignore );
resolve();
}, get(error, 'response.body.retry_after', 150)); continue;
});
} else {
throw new NodeApiError(this.getNode(), error);
}
} }
//* Different Discord error, throw it
throw error;
}
} while (--maxTries); } while (--maxTries);
if (maxTries <= 0) { if (maxTries <= 0) {
throw new NodeApiError(this.getNode(), { request: options } as JsonObject, { message: 'Could not send message. Max. amount of rate-limit retries got reached.' }); throw new Error(
'Could not send Webhook message. Max. amount of rate-limit retries reached.',
);
}
returnData.push({ success: true });
} }
returnData.push({success: true});
}
return [this.helpers.returnJsonArray(returnData)]; return [this.helpers.returnJsonArray(returnData)];
} }

View file

@ -0,0 +1,33 @@
// tslint:disable: no-any
export interface DiscordWebhook {
content?: string;
username?: string;
avatar_url?: string;
tts?: boolean;
file?: Buffer;
embeds?: any[];
allowed_mentions?: {
parse: Array<'roles' | 'users' | 'everyone'>;
roles: string[];
users: string[];
replied_user: boolean;
};
flags?: number;
attachments?: DiscordAttachment[];
components?: any[];
payload_json?: any;
}
export interface DiscordAttachment {
id?: string;
filename?: string;
size?: number;
description?: string;
content_type?: string;
url?: string;
proxy_url?: string;
height?: number;
width?: number;
ephemeral?: boolean;
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="256px" height="199px" viewBox="0 0 256 199" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">
<g>
<path d="M216.856339,16.5966031 C200.285002,8.84328665 182.566144,3.2084988 164.041564,0 C161.766523,4.11318106 159.108624,9.64549908 157.276099,14.0464379 C137.583995,11.0849896 118.072967,11.0849896 98.7430163,14.0464379 C96.9108417,9.64549908 94.1925838,4.11318106 91.8971895,0 C73.3526068,3.2084988 55.6133949,8.86399117 39.0420583,16.6376612 C5.61752293,67.146514 -3.4433191,116.400813 1.08711069,164.955721 C23.2560196,181.510915 44.7403634,191.567697 65.8621325,198.148576 C71.0772151,190.971126 75.7283628,183.341335 79.7352139,175.300261 C72.104019,172.400575 64.7949724,168.822202 57.8887866,164.667963 C59.7209612,163.310589 61.5131304,161.891452 63.2445898,160.431257 C105.36741,180.133187 151.134928,180.133187 192.754523,160.431257 C194.506336,161.891452 196.298154,163.310589 198.110326,164.667963 C191.183787,168.842556 183.854737,172.420929 176.223542,175.320965 C180.230393,183.341335 184.861538,190.991831 190.096624,198.16893 C211.238746,191.588051 232.743023,181.531619 254.911949,164.955721 C260.227747,108.668201 245.831087,59.8662432 216.856339,16.5966031 Z M85.4738752,135.09489 C72.8290281,135.09489 62.4592217,123.290155 62.4592217,108.914901 C62.4592217,94.5396472 72.607595,82.7145587 85.4738752,82.7145587 C98.3405064,82.7145587 108.709962,94.5189427 108.488529,108.914901 C108.508531,123.290155 98.3405064,135.09489 85.4738752,135.09489 Z M170.525237,135.09489 C157.88039,135.09489 147.510584,123.290155 147.510584,108.914901 C147.510584,94.5396472 157.658606,82.7145587 170.525237,82.7145587 C183.391518,82.7145587 193.761324,94.5189427 193.539891,108.914901 C193.539891,123.290155 183.391518,135.09489 170.525237,135.09489 Z" fill="#5865F2" fill-rule="nonzero"></path>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.9 KiB