DEV Community

devbu9
devbu9

Posted on

Create Your First Discord Bot with Lambda

Let's set up a TypeScript package that uses the AWS CDK to deploy a Lambda function for handling Discord bot slash commands. Below is a step-by-step guide on how to structure your package and the necessary scripts.

Step by Step Guide

1. Pre-requisites

  • AWS
  • NodeJS
  • Discord

2. Initialize your CDK project

mkdir discord-bot
cd discord-bot
cdk init app --language=typescript
Enter fullscreen mode Exit fullscreen mode

3. Install necessary dependencies

npm install @types/aws-lambda dotenv discord.js discord-interactions 
npm install -D typescript jest @types/jest @types/aws-lambda ts-jest esbuild
Enter fullscreen mode Exit fullscreen mode

4. Add the following options to tsconfig.json

{
  "compilerOptions": {
    ...
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "outDir": "dist"
  },
}
Enter fullscreen mode Exit fullscreen mode

5. Create .env file with following parameters copied

DISCORD_TOKEN=<YOUR_DISCORD_TOKEN>
DISCORD_CLIENT_ID=<DISCORD_CLIENT_ID>
DISCORD_PUBLIC_KEY=<DISCORD_PUBLIC_KEY>
Enter fullscreen mode Exit fullscreen mode

6. update npm commands in package.json

"scripts": {
    "clean": "rm -rf dist && rm -rf cdk.out",
    "build": "tsc",
    "watch": "tsc -w",
    "test": "jest",
    "register": "node dist/src/commands/register-commands.js",
    "deploy": "npm run clean && npm run build && npm run register && cdk deploy"
  },
Enter fullscreen mode Exit fullscreen mode

7. Copy the files under lib and src

// Copy the entire folder
Enter fullscreen mode Exit fullscreen mode

8. Run the commands

npm run deploy
Enter fullscreen mode Exit fullscreen mode

9. Copy the CloudFormation output printed to INTERACTIONS ENDPOINT URL section and save.

10. Refresh the discord and Tadah!!

Top comments (0)