Skip to content

Commit

Permalink
⚡ use get member endpoint instead of iterationg over guild members
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency committed Apr 24, 2024
1 parent e037c15 commit 82092b5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
INodeExecutionData,
INodeProperties,
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
import { updateDisplayOptions } from '../../../../../utils/utilities';
import { discordApiMultiPartRequest, discordApiRequest } from '../../transport';
import {
Expand All @@ -16,7 +16,6 @@ import {

import {
checkAccessToChannel,
checkIfUserGuildMember,
parseDiscordError,
prepareEmbeds,
prepareErrorData,
Expand Down Expand Up @@ -168,7 +167,23 @@ export async function execute(
}) as string;

if (isOAuth2) {
await checkIfUserGuildMember.call(this, guildId, userId, i);
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 = (
Expand Down
31 changes: 0 additions & 31 deletions packages/nodes-base/nodes/Discord/v2/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,37 +271,6 @@ 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, {
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

0 comments on commit 82092b5

Please sign in to comment.