CommandHandler
This commit is contained in:
parent
50e0f8d27d
commit
23cf68ac38
19 changed files with 219 additions and 6 deletions
|
@ -1,4 +1,5 @@
|
|||
{
|
||||
"tabWidth": 2,
|
||||
"useTabs": false
|
||||
"tabWidth": 4,
|
||||
"useTabs": false,
|
||||
"printWidth": 120
|
||||
}
|
||||
|
|
1
.vscode/settings.json
vendored
Normal file
1
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
{}
|
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
|
@ -11,7 +11,6 @@
|
|||
"type": "module",
|
||||
"dependencies": {
|
||||
"@discordeno/bot": "^19.0.0-next.d81b28a",
|
||||
"@discordeno/rest": "^19.0.0-next.d81b28a",
|
||||
"@discordeno/utils": "^19.0.0-next.d81b28a"
|
||||
"@discordeno/rest": "^19.0.0-next.d81b28a"
|
||||
}
|
||||
}
|
41
src/index.ts
41
src/index.ts
|
@ -1 +1,40 @@
|
|||
console.log("Hello via Bun!");
|
||||
import { createBot, Intents } from "@discordeno/bot";
|
||||
import * as interactionHandler from "./lib/handlers/InteractionHandler";
|
||||
|
||||
const client = createBot({
|
||||
token: process.env.DISCORD_TOKEN,
|
||||
intents: Intents.Guilds,
|
||||
events: {
|
||||
ready(payload, rawPayload) {
|
||||
console.log("Logged in");
|
||||
},
|
||||
interactionCreate: interactionHandler.handle,
|
||||
},
|
||||
});
|
||||
|
||||
// Load handlers step and whatnot ( hatchets )
|
||||
await interactionHandler.load(client);
|
||||
|
||||
client.start();
|
||||
|
||||
client.transformers.desiredProperties.message.content = true;
|
||||
client.transformers.desiredProperties.message.author = true;
|
||||
|
||||
client.transformers.desiredProperties.user.id = true;
|
||||
client.transformers.desiredProperties.user.username = true;
|
||||
client.transformers.desiredProperties.user.avatar = true;
|
||||
|
||||
client.transformers.desiredProperties.member.user = true;
|
||||
client.transformers.desiredProperties.member.roles = true;
|
||||
client.transformers.desiredProperties.member.permissions = true;
|
||||
|
||||
client.transformers.desiredProperties.interaction.id = true;
|
||||
client.transformers.desiredProperties.interaction.type = true;
|
||||
client.transformers.desiredProperties.interaction.data = true;
|
||||
client.transformers.desiredProperties.interaction.token = true;
|
||||
client.transformers.desiredProperties.interaction.guildId = true;
|
||||
client.transformers.desiredProperties.interaction.channelId = true;
|
||||
client.transformers.desiredProperties.interaction.member = true;
|
||||
|
||||
process.on("SIGINT", process.exit);
|
||||
process.on("SIGTERM", process.exit);
|
||||
|
|
33
src/interactions/ping.ts
Normal file
33
src/interactions/ping.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
import type { Command } from "../lib/types/command";
|
||||
import { MessageComponentTypes, TextStyles } from "@discordeno/bot";
|
||||
import SlashCommand from "../lib/classes/SlashCommand";
|
||||
import Modal from "../lib/classes/Modal";
|
||||
|
||||
export default class extends SlashCommand implements Command {
|
||||
static name = "ping";
|
||||
static description = "Show a test modal";
|
||||
static options = [];
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
async run() {
|
||||
return new Modal()
|
||||
.setTitle("dope")
|
||||
.setId("dope")
|
||||
.setComponents([
|
||||
{
|
||||
type: MessageComponentTypes.ActionRow,
|
||||
components: [
|
||||
{
|
||||
type: MessageComponentTypes.InputText,
|
||||
customId: "dope",
|
||||
style: TextStyles.Short,
|
||||
label: "dope",
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
}
|
||||
}
|
5
src/lib/classes/ApplicationCommand.ts
Normal file
5
src/lib/classes/ApplicationCommand.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { InteractionTypes } from "@discordeno/types";
|
||||
|
||||
export default class ApplicationCommand {
|
||||
static type = InteractionTypes.ApplicationCommand;
|
||||
}
|
6
src/lib/classes/MessageCommand.ts
Normal file
6
src/lib/classes/MessageCommand.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import { ApplicationCommandTypes } from "@discordeno/types";
|
||||
import ApplicationCommand from "./ApplicationCommand";
|
||||
|
||||
export default class SlashCommand extends ApplicationCommand {
|
||||
static commandType = ApplicationCommandTypes.Message;
|
||||
}
|
22
src/lib/classes/Modal.ts
Normal file
22
src/lib/classes/Modal.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import { InteractionResponseTypes, type Component } from "@discordeno/bot";
|
||||
import type { InteractionCallbackData } from "@discordeno/types";
|
||||
|
||||
export default class Modal {
|
||||
type = InteractionResponseTypes.Modal;
|
||||
data: InteractionCallbackData = {};
|
||||
|
||||
setTitle(title: string): Modal {
|
||||
this.data = Object.assign(this.data, { title });
|
||||
return this;
|
||||
}
|
||||
|
||||
setId(id: string): Modal {
|
||||
this.data = Object.assign(this.data, { custom_id: id });
|
||||
return this;
|
||||
}
|
||||
|
||||
setComponents(components: Array<Component>) {
|
||||
this.data = Object.assign(this.data, { components });
|
||||
return this;
|
||||
}
|
||||
}
|
6
src/lib/classes/SlashCommand.ts
Normal file
6
src/lib/classes/SlashCommand.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import { ApplicationCommandTypes } from "@discordeno/types";
|
||||
import ApplicationCommand from "./ApplicationCommand";
|
||||
|
||||
export default class SlashCommand extends ApplicationCommand {
|
||||
static commandType = ApplicationCommandTypes.ChatInput;
|
||||
}
|
6
src/lib/classes/UserCommand.ts
Normal file
6
src/lib/classes/UserCommand.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import { ApplicationCommandTypes } from "@discordeno/types";
|
||||
import ApplicationCommand from "./ApplicationCommand";
|
||||
|
||||
export default class SlashCommand extends ApplicationCommand {
|
||||
static commandType = ApplicationCommandTypes.User;
|
||||
}
|
0
src/lib/handlers/AutocompleteHandler.ts
Normal file
0
src/lib/handlers/AutocompleteHandler.ts
Normal file
27
src/lib/handlers/CommandHandler.ts
Normal file
27
src/lib/handlers/CommandHandler.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import type { Bot, Interaction, InteractionResponse } from "@discordeno/bot";
|
||||
import type { Command } from "../types/command";
|
||||
import REST from "./RESTHandler";
|
||||
|
||||
export const commands = new Map();
|
||||
|
||||
export function load(client: Bot, interactions: Map<string, any>) {
|
||||
for (let [key, value] of interactions) {
|
||||
commands.set(key, new value());
|
||||
}
|
||||
}
|
||||
|
||||
export function handle(interaction: Interaction) {
|
||||
if (!interaction.data) return;
|
||||
|
||||
const resolved = commands.get(interaction.data.name);
|
||||
if (!resolved) return interaction.respond("What the fuck did you do");
|
||||
resolved
|
||||
.run()
|
||||
.then((response: InteractionResponse) =>
|
||||
REST.sendInteractionResponse(interaction.id, interaction.token, response)
|
||||
)
|
||||
.catch((e: Error) => {
|
||||
console.log("CommandHandler.ts -> Exception");
|
||||
console.log(e);
|
||||
});
|
||||
}
|
0
src/lib/handlers/ComponentHandler.ts
Normal file
0
src/lib/handlers/ComponentHandler.ts
Normal file
50
src/lib/handlers/InteractionHandler.ts
Normal file
50
src/lib/handlers/InteractionHandler.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
import { InteractionTypes, type Bot, type Interaction, InteractionResponseTypes } from "@discordeno/bot";
|
||||
import { readdir } from "node:fs/promises";
|
||||
import REST from "./RESTHandler";
|
||||
|
||||
import * as CommandHandler from "./CommandHandler";
|
||||
|
||||
export const interactions = new Map();
|
||||
|
||||
export async function load(client: Bot) {
|
||||
const files = await readdir("src/interactions");
|
||||
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
let value = files[i];
|
||||
const interaction = await import(import.meta.dirname + "/../../interactions/" + value);
|
||||
interactions.set(interaction.default.name, interaction.default);
|
||||
}
|
||||
|
||||
CommandHandler.load(client, interactions);
|
||||
}
|
||||
|
||||
export function handle(interaction: Interaction) {
|
||||
const { type } = interaction;
|
||||
|
||||
if (type == InteractionTypes.ApplicationCommand) {
|
||||
CommandHandler.handle(interaction);
|
||||
} else if (type == InteractionTypes.MessageComponent) {
|
||||
REST.sendInteractionResponse(interaction.id, interaction.token, {
|
||||
type: InteractionResponseTypes.ChannelMessageWithSource,
|
||||
data: {
|
||||
content: "Not Implemented",
|
||||
flags: 64,
|
||||
},
|
||||
});
|
||||
} else if (type == InteractionTypes.ApplicationCommandAutocomplete) {
|
||||
REST.sendInteractionResponse(interaction.id, interaction.token, {
|
||||
type: InteractionResponseTypes.ApplicationCommandAutocompleteResult,
|
||||
data: {
|
||||
choices: [{ name: "not_implemented", value: "Not Implemented" }],
|
||||
},
|
||||
});
|
||||
} else if (type == InteractionTypes.ModalSubmit) {
|
||||
REST.sendInteractionResponse(interaction.id, interaction.token, {
|
||||
type: InteractionResponseTypes.ChannelMessageWithSource,
|
||||
data: {
|
||||
content: "Not Implemented",
|
||||
flags: 64,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
0
src/lib/handlers/ModalHandler.ts
Normal file
0
src/lib/handlers/ModalHandler.ts
Normal file
7
src/lib/handlers/RESTHandler.ts
Normal file
7
src/lib/handlers/RESTHandler.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { createRestManager } from "@discordeno/rest";
|
||||
|
||||
const REST = createRestManager({
|
||||
token: process.env.DISCORD_TOKEN,
|
||||
});
|
||||
|
||||
export default REST;
|
6
src/lib/types/command.d.ts
vendored
Normal file
6
src/lib/types/command.d.ts
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
import { type Interaction, type InteractionCallbackData, type InteractionResponse } from "@discordeno/bot";
|
||||
|
||||
interface Command {
|
||||
run(interaction: Interaction): Promise<InteractionResponse>;
|
||||
autocomplete?(interaction: Interaction): Promise<InteractionCallbackData>;
|
||||
}
|
5
src/lib/types/environment.d.ts
vendored
Normal file
5
src/lib/types/environment.d.ts
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
declare module "bun" {
|
||||
interface Env {
|
||||
DISCORD_TOKEN: string;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue