Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Discord Node): When using OAuth2 authentication, check if user is a guild member when sending direct message #9183

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const description = updateDisplayOptions(displayOptions, properties);

export async function execute(
this: IExecuteFunctions,
guildId: string,
_guildId: string,
userGuilds: IDataObject[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const description = updateDisplayOptions(displayOptions, properties);

export async function execute(
this: IExecuteFunctions,
guildId: string,
_guildId: string,
userGuilds: IDataObject[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const description = updateDisplayOptions(displayOptions, properties);

export async function execute(
this: IExecuteFunctions,
guildId: string,
_guildId: string,
userGuilds: IDataObject[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const description = updateDisplayOptions(displayOptions, properties);

export async function execute(
this: IExecuteFunctions,
guildId: string,
_guildId: string,
userGuilds: IDataObject[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const description = updateDisplayOptions(displayOptions, properties);

export async function execute(
this: IExecuteFunctions,
guildId: string,
_guildId: string,
userGuilds: IDataObject[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const description = updateDisplayOptions(displayOptions, properties);

export async function execute(
this: IExecuteFunctions,
guildId: string,
_guildId: string,
userGuilds: IDataObject[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const description = updateDisplayOptions(displayOptions, properties);

export async function execute(
this: IExecuteFunctions,
guildId: string,
_guildId: string,
userGuilds: IDataObject[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {

import {
checkAccessToChannel,
checkIfUserGuildMember,
parseDiscordError,
prepareEmbeds,
prepareErrorData,
Expand Down Expand Up @@ -153,7 +154,7 @@ export async function execute(
};

if (embeds) {
body.embeds = prepareEmbeds.call(this, embeds, i);
body.embeds = prepareEmbeds.call(this, embeds);
}

try {
Expand All @@ -166,11 +167,23 @@ export async function execute(
extractValue: true,
}) as string;

if (isOAuth2) {
await checkIfUserGuildMember.call(this, guildId, userId, i);
}

channelId = (
(await discordApiRequest.call(this, 'POST', '/users/@me/channels', {
recipient_id: userId,
})) as IDataObject
).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') {
Expand All @@ -179,11 +192,13 @@ export async function execute(
}) as string;
}

if (!channelId) {
throw new NodeOperationError(this.getNode(), 'Channel ID is required');
if (isOAuth2 && sendTo !== 'user') {
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[] = [];

Expand Down
37 changes: 34 additions & 3 deletions packages/nodes-base/nodes/Discord/v2/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ export function prepareOptions(options: IDataObject, guildId?: string) {
return options;
}

export function prepareEmbeds(this: IExecuteFunctions, embeds: IDataObject[], i = 0) {
export function prepareEmbeds(this: IExecuteFunctions, embeds: IDataObject[]) {
return embeds
.map((embed, index) => {
.map((embed) => {
let embedReturnData: IDataObject = {};

if (embed.inputMethod === 'json') {
Expand Down Expand Up @@ -261,7 +261,7 @@ export async function checkAccessToChannel(
if (!guildId) {
throw new NodeOperationError(
this.getNode(),
`Could not fing server for channel with the id ${channelId}`,
`Could not find server for channel with the id ${channelId}`,
{
itemIndex,
},
Expand All @@ -271,6 +271,37 @@ export async function checkAccessToChannel(
checkAccessToGuild(this.getNode(), guildId, userGuilds, itemIndex);
}

export async function checkIfUserGuildMember(
this: IExecuteFunctions,
guildId: string,
userId: string,
itemIndex = 0,
) {
let lastUserId;
let members;

while (true) {
members = (await discordApiRequest.call(this, 'GET', `/guilds/${guildId}/members`, undefined, {
elsmr marked this conversation as resolved.
Show resolved Hide resolved
limit: 100,
after: lastUserId,
})) as Array<{ user: { id: string } }>;

if (!members?.length) {
throw new NodeOperationError(
this.getNode(),
`User with the id ${userId} is not a member of the selected guild`,
{
itemIndex,
},
);
} else if (members.some((member) => member.user.id === userId)) {
break;
} else {
lastUserId = members[members.length - 1].user.id;
}
}
}

export async function setupChannelGetter(this: IExecuteFunctions, userGuilds: IDataObject[]) {
const isOAuth2 = this.getNodeParameter('authentication', 0) === 'oAuth2';

Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Discord/v2/methods/listSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export async function categorySearch(this: ILoadOptionsFunctions): Promise<INode

export async function userSearch(
this: ILoadOptionsFunctions,
filter?: string,
_filter?: string,
paginationToken?: string,
): Promise<INodeListSearchResult> {
const guildId = await getGuildId.call(this);
Expand Down
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/Discord/v2/transport/discord.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function discordApiRequest(
if (remaining === 0) {
await sleep(resetAfter);
} 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 };
Expand Down Expand Up @@ -91,7 +91,7 @@ export async function discordApiMultiPartRequest(
if (remaining === 0) {
await sleep(resetAfter);
} 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);
Expand Down
Loading