This commit is contained in:
Nicolás Serna 2022-07-13 21:29:32 -03:00
commit e366462114
9 changed files with 117 additions and 11 deletions

1
examples/.env.example Normal file
View File

@ -0,0 +1 @@
TOKEN=YOUR_BOT_TOKEN

View File

@ -0,0 +1,57 @@
import "https://deno.land/std@0.146.0/dotenv/load.ts";
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyles,
ComponentInteraction,
GatewayIntents,
InteractionResponseTypes,
Session,
} from "./deps.ts";
const token = Deno.env.get("TOKEN") ?? Deno.args[0];
if (!token) {
throw new Error("Please provide a token");
}
const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages;
const session = new Session({ token, intents });
const PREFIX = ">";
const components = new ButtonBuilder().setCustomId("ping").setLabel("Hello!").setStyle(ButtonStyles.Success);
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(components).toJSON();
session.on("ready", (payload) => {
console.log("Logged in as:", payload.user.username);
});
session.on("messageCreate", (message) => {
if (message.author?.bot || !message.content.startsWith(PREFIX)) {
return;
}
const args = message.content.substring(PREFIX.length).trim().split(/\s+/gm);
const name = args.shift()?.toLowerCase();
console.log(args, name);
if (name === "ping") {
message.reply({ components: [row] })
.then(() => {})
.catch(e => console.error(e))
}
});
// Follow interaction event
session.on("interactionCreate", (interaction) => {
if (!interaction.isComponent()) return;
const component = interaction as ComponentInteraction;
if (component.customId == "ping") {
component.respond({
type: InteractionResponseTypes.ChannelMessageWithSource,
data: { content: "pong!" },
});
}
});
await session.start();

View File

@ -21,11 +21,11 @@ session.on("ready", (payload) => {
const PREFIX = ">";
session.on("messageCreate", (message) => {
if (message.author.bot || message.content.startsWith(PREFIX)) {
if (message.author?.bot || !message.content.startsWith(PREFIX)) {
return;
}
const args = message.content.slice(PREFIX.length).trim().split(/\s+/gm);
const args = message.content.substring(PREFIX.length).trim().split(/\s+/gm);
const name = args.shift()?.toLowerCase();
if (name === "ping") {

43
examples/slashCommand.ts Normal file
View File

@ -0,0 +1,43 @@
import "https://deno.land/std@0.146.0/dotenv/load.ts";
import { CreateApplicationCommand, GatewayIntents, InteractionResponseTypes, Session } from "./deps.ts";
const token = Deno.env.get("TOKEN") ?? Deno.args[0];
if (!token) {
throw new Error("Please provide a token");
}
const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages;
const session = new Session({ token, intents });
const command: CreateApplicationCommand = {
name: "ping",
description: "Replies with pong!",
};
const guildId = '';
session.on("ready", async (payload) => {
console.log("Logged in as:", payload.user.username);
console.log("Creating the application commands...");
// create command
try {
await session.createApplicationCommand(command, guildId);
console.log("Done!");
} catch (err) {
console.error(err);
}
});
// Follow interaction event
session.on("interactionCreate", (interaction) => {
if (!interaction.isCommand()) return;
if (interaction.commandName === "ping") {
interaction.respond({
type: InteractionResponseTypes.ChannelMessageWithSource,
data: { content: "pong!" },
});
}
});
await session.start()

View File

@ -8,6 +8,7 @@ import type {
FileContent,
MessageActivityTypes,
MessageTypes,
DiscordMessageComponents
} from "../../discordeno/mod.ts";
import type { Component } from "./components/Component.ts";
import type { GetReactions } from "../Routes.ts";
@ -20,7 +21,6 @@ import Member from "./Member.ts";
import Attachment from "./Attachment.ts";
import ComponentFactory from "./components/ComponentFactory.ts";
import MessageReaction from "./MessageReaction.ts";
import Sticker from "./Sticker.ts";
import * as Routes from "../Routes.ts";
import { StickerItem } from "./Sticker.ts";
@ -51,6 +51,7 @@ export interface CreateMessage {
files?: FileContent[];
messageReference?: CreateMessageReference;
tts?: boolean;
components?: DiscordMessageComponents;
}
/**
@ -290,6 +291,7 @@ export class Message implements Model {
: undefined,
embeds: options.embeds,
tts: options.tts,
components: options.components,
},
);

View File

@ -43,7 +43,7 @@ export class InputTextBuilder {
this.#data.required = required;
return this;
}
toJSON() {
return { ...this.#data };
toJSON(): DiscordInputTextComponent {
return { ...this.#data, type: this.type };
}
}

View File

@ -1,4 +1,4 @@
import type { MessageComponentTypes } from "../../../discordeno/mod.ts";
import type { DiscordActionRow, MessageComponentTypes } from "../../../discordeno/mod.ts";
import type { ComponentBuilder } from "../../Util.ts";
export class ActionRowBuilder<T extends ComponentBuilder> {
@ -23,7 +23,10 @@ export class ActionRowBuilder<T extends ComponentBuilder> {
return this;
}
toJSON() {
return { type: this.type, components: this.components.map((c) => c.toJSON()) };
toJSON(): DiscordActionRow {
return {
type: this.type,
components: this.components.map((c) => c.toJSON()) as DiscordActionRow["components"],
};
}
}

View File

@ -39,6 +39,6 @@ export class ButtonBuilder {
}
toJSON(): DiscordButtonComponent {
return { ...this.#data };
return { ...this.#data, type: this.type };
}
}

View File

@ -47,7 +47,7 @@ export class SelectMenuBuilder {
);
}
toJSON() {
return { ...this.#data, options: this.options.map((option) => option.toJSON()) };
toJSON(): DiscordSelectMenuComponent {
return { ...this.#data, type: this.type, options: this.options.map((option) => option.toJSON()) };
}
}