mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
fix(Discord Node): When using OAuth2 authentication, check if user is a guild member when sending direct message (#9183)
This commit is contained in:
parent
d9e74949c4
commit
00dfad3279
|
@ -25,7 +25,7 @@ export const description = updateDisplayOptions(displayOptions, properties);
|
||||||
|
|
||||||
export async function execute(
|
export async function execute(
|
||||||
this: IExecuteFunctions,
|
this: IExecuteFunctions,
|
||||||
guildId: string,
|
_guildId: string,
|
||||||
userGuilds: IDataObject[],
|
userGuilds: IDataObject[],
|
||||||
): Promise<INodeExecutionData[]> {
|
): Promise<INodeExecutionData[]> {
|
||||||
const returnData: INodeExecutionData[] = [];
|
const returnData: INodeExecutionData[] = [];
|
||||||
|
|
|
@ -25,7 +25,7 @@ export const description = updateDisplayOptions(displayOptions, properties);
|
||||||
|
|
||||||
export async function execute(
|
export async function execute(
|
||||||
this: IExecuteFunctions,
|
this: IExecuteFunctions,
|
||||||
guildId: string,
|
_guildId: string,
|
||||||
userGuilds: IDataObject[],
|
userGuilds: IDataObject[],
|
||||||
): Promise<INodeExecutionData[]> {
|
): Promise<INodeExecutionData[]> {
|
||||||
const returnData: INodeExecutionData[] = [];
|
const returnData: INodeExecutionData[] = [];
|
||||||
|
|
|
@ -104,7 +104,7 @@ export const description = updateDisplayOptions(displayOptions, properties);
|
||||||
|
|
||||||
export async function execute(
|
export async function execute(
|
||||||
this: IExecuteFunctions,
|
this: IExecuteFunctions,
|
||||||
guildId: string,
|
_guildId: string,
|
||||||
userGuilds: IDataObject[],
|
userGuilds: IDataObject[],
|
||||||
): Promise<INodeExecutionData[]> {
|
): Promise<INodeExecutionData[]> {
|
||||||
const returnData: INodeExecutionData[] = [];
|
const returnData: INodeExecutionData[] = [];
|
||||||
|
|
|
@ -25,7 +25,7 @@ export const description = updateDisplayOptions(displayOptions, properties);
|
||||||
|
|
||||||
export async function execute(
|
export async function execute(
|
||||||
this: IExecuteFunctions,
|
this: IExecuteFunctions,
|
||||||
guildId: string,
|
_guildId: string,
|
||||||
userGuilds: IDataObject[],
|
userGuilds: IDataObject[],
|
||||||
): Promise<INodeExecutionData[]> {
|
): Promise<INodeExecutionData[]> {
|
||||||
const returnData: INodeExecutionData[] = [];
|
const returnData: INodeExecutionData[] = [];
|
||||||
|
|
|
@ -41,7 +41,7 @@ export const description = updateDisplayOptions(displayOptions, properties);
|
||||||
|
|
||||||
export async function execute(
|
export async function execute(
|
||||||
this: IExecuteFunctions,
|
this: IExecuteFunctions,
|
||||||
guildId: string,
|
_guildId: string,
|
||||||
userGuilds: IDataObject[],
|
userGuilds: IDataObject[],
|
||||||
): Promise<INodeExecutionData[]> {
|
): Promise<INodeExecutionData[]> {
|
||||||
const returnData: INodeExecutionData[] = [];
|
const returnData: INodeExecutionData[] = [];
|
||||||
|
|
|
@ -42,7 +42,7 @@ export const description = updateDisplayOptions(displayOptions, properties);
|
||||||
|
|
||||||
export async function execute(
|
export async function execute(
|
||||||
this: IExecuteFunctions,
|
this: IExecuteFunctions,
|
||||||
guildId: string,
|
_guildId: string,
|
||||||
userGuilds: IDataObject[],
|
userGuilds: IDataObject[],
|
||||||
): Promise<INodeExecutionData[]> {
|
): Promise<INodeExecutionData[]> {
|
||||||
const returnData: INodeExecutionData[] = [];
|
const returnData: INodeExecutionData[] = [];
|
||||||
|
|
|
@ -36,7 +36,7 @@ export const description = updateDisplayOptions(displayOptions, properties);
|
||||||
|
|
||||||
export async function execute(
|
export async function execute(
|
||||||
this: IExecuteFunctions,
|
this: IExecuteFunctions,
|
||||||
guildId: string,
|
_guildId: string,
|
||||||
userGuilds: IDataObject[],
|
userGuilds: IDataObject[],
|
||||||
): Promise<INodeExecutionData[]> {
|
): Promise<INodeExecutionData[]> {
|
||||||
const returnData: INodeExecutionData[] = [];
|
const returnData: INodeExecutionData[] = [];
|
||||||
|
|
|
@ -4,7 +4,7 @@ import type {
|
||||||
INodeExecutionData,
|
INodeExecutionData,
|
||||||
INodeProperties,
|
INodeProperties,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
import { NodeOperationError } from 'n8n-workflow';
|
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
||||||
import { updateDisplayOptions } from '../../../../../utils/utilities';
|
import { updateDisplayOptions } from '../../../../../utils/utilities';
|
||||||
import { discordApiMultiPartRequest, discordApiRequest } from '../../transport';
|
import { discordApiMultiPartRequest, discordApiRequest } from '../../transport';
|
||||||
import {
|
import {
|
||||||
|
@ -153,7 +153,7 @@ export async function execute(
|
||||||
};
|
};
|
||||||
|
|
||||||
if (embeds) {
|
if (embeds) {
|
||||||
body.embeds = prepareEmbeds.call(this, embeds, i);
|
body.embeds = prepareEmbeds.call(this, embeds);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -166,11 +166,39 @@ export async function execute(
|
||||||
extractValue: true,
|
extractValue: true,
|
||||||
}) as string;
|
}) as string;
|
||||||
|
|
||||||
|
if (isOAuth2) {
|
||||||
|
try {
|
||||||
|
await discordApiRequest.call(this, 'GET', `/guilds/${guildId}/members/${userId}`);
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof NodeApiError && error.httpCode === '404') {
|
||||||
|
throw new NodeOperationError(
|
||||||
|
this.getNode(),
|
||||||
|
`User with the id ${userId} is not a member of the selected guild`,
|
||||||
|
{
|
||||||
|
itemIndex: i,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new NodeOperationError(this.getNode(), error, {
|
||||||
|
itemIndex: i,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
channelId = (
|
channelId = (
|
||||||
(await discordApiRequest.call(this, 'POST', '/users/@me/channels', {
|
(await discordApiRequest.call(this, 'POST', '/users/@me/channels', {
|
||||||
recipient_id: userId,
|
recipient_id: userId,
|
||||||
})) as IDataObject
|
})) as IDataObject
|
||||||
).id as string;
|
).id as string;
|
||||||
|
|
||||||
|
if (!channelId) {
|
||||||
|
throw new NodeOperationError(
|
||||||
|
this.getNode(),
|
||||||
|
'Could not create a channel to send direct message to',
|
||||||
|
{ itemIndex: i },
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sendTo === 'channel') {
|
if (sendTo === 'channel') {
|
||||||
|
@ -179,11 +207,13 @@ export async function execute(
|
||||||
}) as string;
|
}) as string;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!channelId) {
|
if (isOAuth2 && sendTo !== 'user') {
|
||||||
throw new NodeOperationError(this.getNode(), 'Channel ID is required');
|
await checkAccessToChannel.call(this, channelId, userGuilds, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isOAuth2) await checkAccessToChannel.call(this, channelId, userGuilds, i);
|
if (!channelId) {
|
||||||
|
throw new NodeOperationError(this.getNode(), 'Channel ID is required', { itemIndex: i });
|
||||||
|
}
|
||||||
|
|
||||||
let response: IDataObject[] = [];
|
let response: IDataObject[] = [];
|
||||||
|
|
||||||
|
|
|
@ -114,9 +114,9 @@ export function prepareOptions(options: IDataObject, guildId?: string) {
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function prepareEmbeds(this: IExecuteFunctions, embeds: IDataObject[], i = 0) {
|
export function prepareEmbeds(this: IExecuteFunctions, embeds: IDataObject[]) {
|
||||||
return embeds
|
return embeds
|
||||||
.map((embed, index) => {
|
.map((embed) => {
|
||||||
let embedReturnData: IDataObject = {};
|
let embedReturnData: IDataObject = {};
|
||||||
|
|
||||||
if (embed.inputMethod === 'json') {
|
if (embed.inputMethod === 'json') {
|
||||||
|
@ -261,7 +261,7 @@ export async function checkAccessToChannel(
|
||||||
if (!guildId) {
|
if (!guildId) {
|
||||||
throw new NodeOperationError(
|
throw new NodeOperationError(
|
||||||
this.getNode(),
|
this.getNode(),
|
||||||
`Could not fing server for channel with the id ${channelId}`,
|
`Could not find server for channel with the id ${channelId}`,
|
||||||
{
|
{
|
||||||
itemIndex,
|
itemIndex,
|
||||||
},
|
},
|
||||||
|
|
|
@ -124,7 +124,7 @@ export async function categorySearch(this: ILoadOptionsFunctions): Promise<INode
|
||||||
|
|
||||||
export async function userSearch(
|
export async function userSearch(
|
||||||
this: ILoadOptionsFunctions,
|
this: ILoadOptionsFunctions,
|
||||||
filter?: string,
|
_filter?: string,
|
||||||
paginationToken?: string,
|
paginationToken?: string,
|
||||||
): Promise<INodeListSearchResult> {
|
): Promise<INodeListSearchResult> {
|
||||||
const guildId = await getGuildId.call(this);
|
const guildId = await getGuildId.call(this);
|
||||||
|
|
|
@ -48,7 +48,7 @@ export async function discordApiRequest(
|
||||||
if (remaining === 0) {
|
if (remaining === 0) {
|
||||||
await sleep(resetAfter);
|
await sleep(resetAfter);
|
||||||
} else {
|
} else {
|
||||||
await sleep(20); //prevent excing global rate limit of 50 requests per second
|
await sleep(20); //prevent exceeding global rate limit of 50 requests per second
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.body || { success: true };
|
return response.body || { success: true };
|
||||||
|
@ -91,7 +91,7 @@ export async function discordApiMultiPartRequest(
|
||||||
if (remaining === 0) {
|
if (remaining === 0) {
|
||||||
await sleep(resetAfter);
|
await sleep(resetAfter);
|
||||||
} else {
|
} else {
|
||||||
await sleep(20); //prevent excing global rate limit of 50 requests per second
|
await sleep(20); //prevent exceeding global rate limit of 50 requests per second
|
||||||
}
|
}
|
||||||
|
|
||||||
return jsonParse<IDataObject[]>(response.body);
|
return jsonParse<IDataObject[]>(response.body);
|
||||||
|
|
Loading…
Reference in a new issue