Skip to content

Commit

Permalink
Merge pull request #1 from Ry0xi/feature/middleware-discord-handle-pi…
Browse files Browse the repository at this point in the history
…ng-message

feat: add discord-handle-ping-message-middleware
  • Loading branch information
Ry0xi authored Dec 17, 2023
2 parents 08df78f + 0e321f8 commit 481ca3e
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Node Dependencies
run: npm install --frozen-lockfile
- name: Check Code format & Linting
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*.js
!jest.config.js
!.eslintrc.js
!prettierrc.js
!.prettierrc.js
*.d.ts
node_modules
.env
Expand Down
12 changes: 12 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
singleQuote: true,
trailingComma: 'all',
overrides: [
{
files: '*.md',
options: {
tabWidth: 2,
},
},
],
};
21 changes: 20 additions & 1 deletion handlers/discord-bot-handler/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import middy from '@middy/core';
import httpErrorHandlerMiddleware from '@middy/http-error-handler';
import httpHeaderNormalizerMiddleware from '@middy/http-header-normalizer';
import httpJsonBodyParserMiddleware from '@middy/http-json-body-parser';
import inputOutputLoggerMiddleware from '@middy/input-output-logger';
import type {
APIGatewayProxyEventV2,
APIGatewayProxyResultV2,
} from 'aws-lambda';

import discordHandlePingMessageMiddleware from '@/handlers/middlewares/discord-handle-ping-message';
import { getEnv } from '@/handlers/utils';

const handleInteraction = async (
Expand All @@ -19,4 +24,18 @@ const handleInteraction = async (
};
};

export const handler = middy().handler(handleInteraction);
export const handler = middy()
// input and output logging
// https://middy.js.org/docs/middlewares/input-output-logger/
.use(inputOutputLoggerMiddleware())
// normalize HTTP headers to lowercase
// https://middy.js.org/docs/middlewares/http-header-normalizer
.use(httpHeaderNormalizerMiddleware())
// parse HTTP request body and convert it into an object
// https://middy.js.org/docs/middlewares/http-json-body-parser
.use(httpJsonBodyParserMiddleware())
.use(discordHandlePingMessageMiddleware())
// handle uncaught errors that contain the properties statusCode and message and creates a proper HTTP response for them
// https://middy.js.org/docs/middlewares/http-error-handler
.use(httpErrorHandlerMiddleware())
.handler(handleInteraction);
38 changes: 38 additions & 0 deletions handlers/interaction-event-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { APIGatewayProxyEventV2 } from 'aws-lambda';
import type { InteractionType } from 'discord-interactions';

export interface DiscordInteractionEvent
extends Omit<APIGatewayProxyEventV2, 'body'> {
body: InteractionBodyType;
}

export interface InteractionBodyType {
id: string;
application_id: string;
type: InteractionType;
token: string;
// type: 1の時以外は存在する
// https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-data
data?: ApplicationCommandInteractionData;
}

export interface ApplicationCommandInteractionData {
id: string;
type: ApplicationCommandType;
name: string;
// Discordのパラメータ上ではオプショナルだが、コマンドの情報のために必須
options: Array<ApplicationCommandInteractionDataOption>;
}

export interface ApplicationCommandInteractionDataOption {
name: string;
value: string | number | boolean;
options?: Array<ApplicationCommandInteractionDataOption>;
}

// https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-types
export enum ApplicationCommandType {
CHAT_INPUT = 1,
USER = 2,
MESSAGE = 3,
}
40 changes: 40 additions & 0 deletions handlers/middlewares/discord-handle-ping-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type middy from '@middy/core';
import type { APIGatewayProxyResultV2 } from 'aws-lambda';
import { InteractionType } from 'discord-interactions';

import type { DiscordInteractionEvent } from '@/handlers/interaction-event-schema';

const discordHandlePingMessageMiddleware = (): middy.MiddlewareObj<
DiscordInteractionEvent,
APIGatewayProxyResultV2
> => {
/**
* PING - PONG
*
* @see https://discord.com/developers/docs/interactions/receiving-and-responding#receiving-an-interaction
*/
const discordHandlePingMessageMiddlewareBefore: middy.MiddlewareFn<
DiscordInteractionEvent,
APIGatewayProxyResultV2
> = async (request): Promise<APIGatewayProxyResultV2 | void> => {
const interactionType = request.event.body.type;

if (interactionType === InteractionType.PING) {
return {
statusCode: 200,
body: JSON.stringify({
type: InteractionType.PING,
}),
headers: {
'Content-Type': 'application/json',
},
};
}
};

return {
before: discordHandlePingMessageMiddlewareBefore,
};
};

export default discordHandlePingMessageMiddleware;
89 changes: 88 additions & 1 deletion handlers/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion handlers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
"private": true,
"description": "",
"dependencies": {
"@middy/core": "^5.1.0"
"@middy/core": "^5.1.0",
"@middy/http-error-handler": "^5.1.0",
"@middy/http-header-normalizer": "^5.1.0",
"@middy/http-json-body-parser": "^5.1.0",
"@middy/input-output-logger": "^5.1.0",
"discord-interactions": "^3.4.0"
},
"devDependencies": {
"@types/aws-lambda": "^8.10.130"
Expand Down

0 comments on commit 481ca3e

Please sign in to comment.