Skip to content

Commit

Permalink
Add command handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mandar1jn committed Apr 6, 2024
1 parent 9266231 commit 417fc38
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 12 deletions.
28 changes: 22 additions & 6 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { verifyKey } from "discord-interactions";
import { Env } from "env";
import { Command } from "types";
import { DiscordResponse } from "./response";
import { APIBaseInteraction, InteractionResponseType, InteractionType } from "discord-api-types/v10";
import { APIBaseInteraction, InteractionResponseType, InteractionType, APIApplicationCommandInteraction } from "discord-api-types/v10";
import { SlashCommandInteraction } from "client";

export class DiscordClient {

Expand All @@ -17,21 +18,36 @@ export class DiscordClient {
this.ctx = ctx;
}

async handle(request: Request): Promise<DiscordResponse>
async handle(request: Request): Promise<DiscordResponse | Response>
{
const {body, valid} = await this.isRequestValid(request);
if(!valid)
{
return new DiscordResponse("Invalid request signature", { status: 401 });
return new Response("Invalid request signature", { status: 401 });
}

const interaction = JSON.parse(body) as APIBaseInteraction<InteractionType, any>

switch(interaction.type)
{
case InteractionType.Ping:
{
return new DiscordResponse(InteractionResponseType.Pong);
};
case InteractionType.ApplicationCommand:
{
const slashInteraction = new SlashCommandInteraction(interaction);

const command = this.commands.find(command => command.data.name == slashInteraction.interaction.data.name);

if (interaction.type == InteractionType.Ping) {
return new DiscordResponse({ type: InteractionResponseType.Pong });
if(command)
{
return command.execute(slashInteraction);
}
}
}

return new DiscordResponse("Not Found", {status: 404});
return new Response("Not Found", {status: 404});
}

async isRequestValid(request: Request): Promise<{body: string, valid: boolean}>
Expand Down
3 changes: 2 additions & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./client"
export * from "./response"
export * from "./response"
export * from "./interaction"
23 changes: 23 additions & 0 deletions src/client/interaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { APIApplicationCommandInteraction, APIBaseInteraction, InteractionResponseType, InteractionType } from "discord-api-types/v10";
import { DiscordResponse } from "./response";

export class SlashCommandInteraction
{
interaction: APIApplicationCommandInteraction;

constructor(interaction: APIBaseInteraction<InteractionType, any>)
{
if(interaction.type != InteractionType.ApplicationCommand)
{
throw new Error("Invalid interaction type. Expected ApplicationCommand")
}

this.interaction = interaction as APIApplicationCommandInteraction;
}

reply(data: any)
{
return new DiscordResponse(InteractionResponseType.ChannelMessageWithSource, data);
}

}
6 changes: 4 additions & 2 deletions src/client/response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { InteractionResponseType } from "discord-api-types/v10";

export class DiscordResponse extends Response {
constructor(body?: any, init?: ResponseInit) {
const jsonBody = JSON.stringify(body);
constructor(type: InteractionResponseType, data?: any, init?: ResponseInit) {
const jsonBody = JSON.stringify({type, data});
init = init || {
headers: {
'content-type': 'application/json;charset=UTF-8',
Expand Down
14 changes: 12 additions & 2 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { Command } from "types";
import {SlashCommandBuilder} from "@discordjs/builders";
import {EmbedBuilder, SlashCommandBuilder} from "@discordjs/builders";
import { DiscordResponse } from "client";
import { InteractionResponseType } from "discord-api-types/v10";


const command = {
data: new SlashCommandBuilder()
.setName("help")
.setDescription("help me!")
.setDescription("help me!"),
execute(interaction) {

const embed = new EmbedBuilder()
.setTitle("L skillissue")
.setDescription("git good lmao");

return interaction.reply({embeds: [embed]});
},
} satisfies Command;

export default command;
4 changes: 3 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import { DiscordResponse, SlashCommandInteraction } from "client";

declare type Command = {
data: SlashCommandBuilder
data: SlashCommandBuilder,
execute(interaction: SlashCommandInteraction): DiscordResponse
};

0 comments on commit 417fc38

Please sign in to comment.