Skip to content

Commit

Permalink
Speed Up Discord Bot Function
Browse files Browse the repository at this point in the history
[Problem]
The Discord bot function is still a bit slow at times, resulting in
timeouts on Discord.

[Solution]
Resolve both the Discord verification and the Commands Lambda call at
the same time when needed. This saves approximately 500ms since they can
be done in parallel.

[Testing]
Deployed and manually verified that this properly resolves the issues.
  • Loading branch information
GEMISIS committed May 16, 2021
1 parent a6f3d2e commit e2aa772
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "discord-bot-cdk-construct",
"version": "0.1.7",
"version": "0.1.8",
"description": "A quick CDK Construct for creating a serverless Discord bot in AWS!",
"author": {
"name": "Gerald McAlister",
Expand Down
31 changes: 19 additions & 12 deletions src/functions/DiscordBotFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {Context, Callback} from 'aws-lambda';
import {DiscordEventRequest, DiscordEventResponse} from '../types';
import {getDiscordSecrets} from './utils/DiscordSecrets';
import {Lambda} from 'aws-sdk';
import * as nacl from 'tweetnacl';
import {commandLambdaARN} from './constants/EnvironmentProps';
import {sign} from 'tweetnacl';

const lambda = new Lambda();

Expand All @@ -18,25 +18,32 @@ export async function handler(event: DiscordEventRequest, context: Context,
callback: Callback): Promise<DiscordEventResponse> {
console.log(`Received event: ${JSON.stringify(event)}`);

if (event && await verifyEvent(event)) {
const verifyPromise = verifyEvent(event);

if (event) {
switch (event.jsonBody.type) {
case 1:
// Return pongs for pings
return {
type: 1,
};
if (await verifyPromise) {
return {
type: 1,
};
}
break;
case 2:
// Actual input request
console.log('Invoking command lambda...');
await lambda.invoke({
const lambdaPromise = lambda.invoke({
FunctionName: commandLambdaARN,
Payload: JSON.stringify(event),
InvocationType: 'Event',
}).promise();
console.log('Returning temporary response...');
return {
type: 5,
};
if (await Promise.all([verifyPromise, lambdaPromise])) {
console.log('Returning temporary response...');
return {
type: 5,
};
}
break;
}
}

Expand All @@ -53,7 +60,7 @@ export async function verifyEvent(event: DiscordEventRequest): Promise<boolean>
console.log('Getting Discord secrets...');
const discordSecrets = await getDiscordSecrets();
console.log('Verifying incoming event...');
const isVerified = nacl.sign.detached.verify(
const isVerified = sign.detached.verify(
Buffer.from(event.timestamp + JSON.stringify(event.jsonBody)),
Buffer.from(event.signature, 'hex'),
Buffer.from(discordSecrets?.publicKey ?? '', 'hex'),
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './types';
export * from './functions/utils/DiscordSecrets';
export * from './constructs/DiscordBotConstruct';
export { verifyEvent } from './functions/DiscordBotFunction';

1 comment on commit e2aa772

@timbru31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! I was suggesting increasing the RAM or making it configurable :)

Please sign in to comment.