Package Exports
- discord-bot-cdk-construct
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (discord-bot-cdk-construct) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
discord-bot-cdk-construct
A CDK Construct for creating a serverless Discord bot. All you need to do is supply your code to handle the commands!
Architecture Overview
This is the architecture for how this project is laid out server-side. The tools used to create these diagrams are:
The bot has a fairly straightforward setup:

The biggest confusion likely stems from the use of two Lambda functions instead of one. This is to ensure that the initial request can respond within Discord's 3 second time limit and return a proper response to the user.
Sample Commands Lambda Function
For handling commands, you just need to provide a Lambda function for sending response to Discord's Web APIs. As an example of how this can be done:
import axios from 'axios';
import {Context, Callback} from 'aws-lambda';
import { DiscordEventRequest, DiscordResponseData, getDiscordSecrets} from 'discord-bot-cdk-construct';
export async function handler(event: DiscordEventRequest, context: Context,
callback: Callback): Promise<string> {
const response = {
tts: false,
content: 'Hello world!',
embeds: [],
allowed_mentions: [],
};
if (event.jsonBody.token && await sendResponse(response, event.jsonBody.token)) {
console.log('Responded successfully!');
} else {
console.log('Failed to send response!');
}
return '200';
}
async function sendResponse(response: DiscordResponseData,
interactionToken: string): Promise<boolean> {
const discordSecret = await getDiscordSecrets();
const authConfig = {
headers: {
'Authorization': `Bot ${discordSecret?.authToken}`
}
};
try {
let url = `https://discord.com/api/v8/webhooks/${discordSecret?.clientId}/${interactionToken}`;
return (await axios.post(url, response, authConfig)).status == 200;
} catch (exception) {
console.log(`There was an error posting a response: ${exception}`);
return false;
}
}A full example project utilzing this construct can be found here. Specifically, the start-api-stack.ts file uses the construct, with DiscordCommands.ts being the commands file (like shown above).
Useful commands
npm run buildcompile typescript to jsnpm run watchwatch for changes and compilenpm run testperform the jest unit testsnpm run lintperform a lint check across the codenpm run fix-lintfix any lint issues automatically where possiblecdk deploydeploy this stack to your default AWS account/regioncdk diffcompare deployed stack with current statecdk synthemits the synthesized CloudFormation template