This commit is contained in:
Yuzu 2022-07-14 12:58:09 -05:00
parent a2dd9ddf5d
commit 4dd48d9bb4
10 changed files with 274 additions and 272 deletions

View File

@ -7,7 +7,6 @@
[![downloads](https://img.shields.io/npm/dw/@oasisjs/biscuit?color=green&logo=npm&style=flat)](https://www.npmjs.com/package/@oasisjs/biscuit) [![downloads](https://img.shields.io/npm/dw/@oasisjs/biscuit?color=green&logo=npm&style=flat)](https://www.npmjs.com/package/@oasisjs/biscuit)
[![deno.land](https://img.shields.io/badge/deno-%5E1.23.3-informational?color=blue&logo=deno&style=flat)](https://deno.land/x/biscuit) [![deno.land](https://img.shields.io/badge/deno-%5E1.23.3-informational?color=blue&logo=deno&style=flat)](https://deno.land/x/biscuit)
<img align="right" src="https://raw.githubusercontent.com/oasisjs/biscuit/main/assets/biscuit.svg" alt="biscuit"/> <img align="right" src="https://raw.githubusercontent.com/oasisjs/biscuit/main/assets/biscuit.svg" alt="biscuit"/>
### Install (for [node18](https://nodejs.org/en/download/)) ### Install (for [node18](https://nodejs.org/en/download/))

View File

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

View File

@ -1,33 +1,32 @@
/** /**
* Bun example * Bun example
* this example should work on most systems, but if it doesn't just clone the library and import everything from mod.ts * this example should work on most systems, but if it doesn't just clone the library and import everything from mod.ts
*/ */
const { GatewayIntents, Session } = require("@oasisjs/biscuit"); const { GatewayIntents, Session } = require("@oasisjs/biscuit");
// if it didn't worked use: // if it didn't worked use:
// const { GatewayIntents, Session } = require("@oasisjs/biscuit"); // const { GatewayIntents, Session } = require("@oasisjs/biscuit");
const token = process.env.TOKEN; const token = process.env.TOKEN;
const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages; const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages;
const session = new Session({ token, intents }); const session = new Session({ token, intents });
session.on("ready", (payload) => { session.on("ready", (payload) => {
console.log("Logged in as:", payload.user.username); console.log("Logged in as:", payload.user.username);
}); });
session.on("messageCreate", async (message) => { session.on("messageCreate", async (message) => {
// GET // GET
if (message.content.startsWith("whatever")) { if (message.content.startsWith("whatever")) {
const whatever = await message.fetch(); const whatever = await message.fetch();
console.log(whatever); console.log(whatever);
} }
// POST // POST
if (message.content.startsWith("ping")) { if (message.content.startsWith("ping")) {
message.reply({ content: "pong!" }).catch((err) => console.error(err)); message.reply({ content: "pong!" }).catch((err) => console.error(err));
} }
}); });
await session.start(); await session.start();

View File

@ -1,39 +1,38 @@
/** /**
* Deno example * Deno example
*/ */
import "https://deno.land/std@0.146.0/dotenv/load.ts"; import "https://deno.land/std@0.146.0/dotenv/load.ts";
// Session to create a new bot (and intents) // Session to create a new bot (and intents)
import { GatewayIntents, Session } from "https://x.nest.land/biscuit/mod.ts"; import { GatewayIntents, Session } from "https://x.nest.land/biscuit/mod.ts";
const token = Deno.env.get("TOKEN") ?? Deno.args[0]; const token = Deno.env.get("TOKEN") ?? Deno.args[0];
if (!token) { if (!token) {
throw new Error("Please provide a token"); throw new Error("Please provide a token");
} }
const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages; const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages;
const session = new Session({ token, intents }); const session = new Session({ token, intents });
session.on("ready", (payload) => { session.on("ready", (payload) => {
console.log("Logged in as:", payload.user.username); console.log("Logged in as:", payload.user.username);
}); });
const PREFIX = ">"; const PREFIX = ">";
session.on("messageCreate", (message) => { session.on("messageCreate", (message) => {
if (message.author?.bot || !message.content.startsWith(PREFIX)) { if (message.author?.bot || !message.content.startsWith(PREFIX)) {
return; return;
} }
const args = message.content.substring(PREFIX.length).trim().split(/\s+/gm); const args = message.content.substring(PREFIX.length).trim().split(/\s+/gm);
const name = args.shift()?.toLowerCase(); const name = args.shift()?.toLowerCase();
if (name === "ping") { if (name === "ping") {
message.reply({ content: "pong!" }); message.reply({ content: "pong!" });
} }
}); });
session.start(); session.start();

View File

@ -1,44 +1,44 @@
/** /**
* Biscuit node example * Biscuit node example
*/ */
// process for get the token // process for get the token
/** @type {NodeJS.Process} process */ /** @type {NodeJS.Process} process */
import process from 'node:process'; import process from "node:process";
// Session for create a new bot and intents // Session for create a new bot and intents
import { Session, GatewayIntents } from '@oasisjs/biscuit'; import { GatewayIntents, Session } from "@oasisjs/biscuit";
// Discord bot token // Discord bot token
/** @type {string} token */ /** @type {string} token */
const token = process.env.TOKEN || "YOUR_TOKEN_HERE"; const token = process.env.TOKEN || "YOUR_TOKEN_HERE";
if (token === "") { if (token === "") {
console.log(new Error("Please set the TOKEN environment variable")); console.log(new Error("Please set the TOKEN environment variable"));
} }
const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages; const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages;
const session = new Session({ token, intents }); const session = new Session({ token, intents });
// Command prefix // Command prefix
const PREFIX = ">"; const PREFIX = ">";
session.on("ready", (data) => { session.on("ready", (data) => {
console.log("Ready! Let's start chatting!"); console.log("Ready! Let's start chatting!");
console.log("Connected as: " + data.user.username); console.log("Connected as: " + data.user.username);
}) });
session.on("messageCreate", (message) => { session.on("messageCreate", (message) => {
if (message.author?.bot || !message.content.startsWith(PREFIX)) { if (message.author?.bot || !message.content.startsWith(PREFIX)) {
return; return;
} }
const args = message.content.substring(PREFIX.length).trim().split(/\s+/gm); const args = message.content.substring(PREFIX.length).trim().split(/\s+/gm);
const name = args.shift()?.toLowerCase(); const name = args.shift()?.toLowerCase();
if (name === "ping") { if (name === "ping") {
message.reply({ content: "pong!" }); message.reply({ content: "pong!" });
} }
}); });
session.start(); session.start();

View File

@ -1,44 +1,44 @@
/** /**
* Biscuit node example * Biscuit node example
*/ */
// process for get the token // process for get the token
/** @type {NodeJS.Process} process */ /** @type {NodeJS.Process} process */
const process = require("node:process"); const process = require("node:process");
// Session for create a new bot and intents // Session for create a new bot and intents
const { Session, GatewayIntents } = require("@oasisjs/biscuit"); const { Session, GatewayIntents } = require("@oasisjs/biscuit");
// Discord bot token // Discord bot token
/** @type {string} token */ /** @type {string} token */
const token = process.env.TOKEN || "YOUR_TOKEN_HERE"; const token = process.env.TOKEN || "YOUR_TOKEN_HERE";
if (token === "") { if (token === "") {
return new Error("Please set the TOKEN environment variable"); return new Error("Please set the TOKEN environment variable");
} }
const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages; const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages;
const session = new Session({ token, intents }); const session = new Session({ token, intents });
// Command prefix // Command prefix
const PREFIX = ">"; const PREFIX = ">";
session.on("ready", (data) => { session.on("ready", (data) => {
console.log("Ready! Let's start chatting!"); console.log("Ready! Let's start chatting!");
console.log("Connected as: " + data.user.username); console.log("Connected as: " + data.user.username);
}) });
session.on("messageCreate", (message) => { session.on("messageCreate", (message) => {
if (message.author?.bot || !message.content.startsWith(PREFIX)) { if (message.author?.bot || !message.content.startsWith(PREFIX)) {
return; return;
} }
const args = message.content.substring(PREFIX.length).trim().split(/\s+/gm); const args = message.content.substring(PREFIX.length).trim().split(/\s+/gm);
const name = args.shift()?.toLowerCase(); const name = args.shift()?.toLowerCase();
if (name === "ping") { if (name === "ping") {
message.reply({ content: "pong!" }); message.reply({ content: "pong!" });
} }
}); });
session.start(); session.start();

View File

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

View File

@ -1,14 +1,14 @@
import type { Localization, PermissionStrings } from '../../../../discordeno/mod.ts'; import type { Localization, PermissionStrings } from "../../../../discordeno/mod.ts";
import { ApplicationCommandTypes } from '../../../../discordeno/mod.ts'; import { ApplicationCommandTypes } from "../../../../discordeno/mod.ts";
import { OptionBased } from './ApplicationCommandOption.ts'; import { OptionBased } from "./ApplicationCommandOption.ts";
import { CreateApplicationCommand } from "../../../Session.ts" import { CreateApplicationCommand } from "../../../Session.ts";
export abstract class ApplicationCommandBuilder implements CreateApplicationCommand { export abstract class ApplicationCommandBuilder implements CreateApplicationCommand {
protected constructor( protected constructor(
// required // required
public type: ApplicationCommandTypes = ApplicationCommandTypes.ChatInput, public type: ApplicationCommandTypes = ApplicationCommandTypes.ChatInput,
public name = '', public name = "",
public description = '', public description = "",
// non-required // non-required
public defaultMemberPermissions?: PermissionStrings[], public defaultMemberPermissions?: PermissionStrings[],
// etc // etc
@ -69,7 +69,7 @@ export class MessageApplicationCommandBuilder {
} }
public toJSON(): { name: string; type: ApplicationCommandTypes.Message } { public toJSON(): { name: string; type: ApplicationCommandTypes.Message } {
if (!this.name) throw new TypeError('Propety \'name\' is required'); if (!this.name) throw new TypeError("Propety 'name' is required");
return { return {
type: ApplicationCommandTypes.Message, type: ApplicationCommandTypes.Message,
@ -82,10 +82,10 @@ export class ChatInputApplicationCommandBuilder extends ApplicationCommandBuilde
public type: ApplicationCommandTypes.ChatInput = ApplicationCommandTypes.ChatInput; public type: ApplicationCommandTypes.ChatInput = ApplicationCommandTypes.ChatInput;
public toJSON(): CreateApplicationCommand { public toJSON(): CreateApplicationCommand {
if (!this.type) throw new TypeError('Propety \'type\' is required'); if (!this.type) throw new TypeError("Propety 'type' is required");
if (!this.name) throw new TypeError('Propety \'name\' is required'); if (!this.name) throw new TypeError("Propety 'name' is required");
if (!this.description) { if (!this.description) {
throw new TypeError('Propety \'description\' is required'); throw new TypeError("Propety 'description' is required");
} }
return { return {

View File

@ -1,5 +1,5 @@
import { ApplicationCommandOptionTypes, type ChannelTypes, type Localization } from "../../../../discordeno/mod.ts"; import { ApplicationCommandOptionTypes, type ChannelTypes, type Localization } from "../../../../discordeno/mod.ts";
import { ApplicationCommandOptionChoice } from "../../interactions/CommandInteraction.ts" import { ApplicationCommandOptionChoice } from "../../interactions/CommandInteraction.ts";
export class ChoiceBuilder { export class ChoiceBuilder {
public name?: string; public name?: string;

View File

@ -38,7 +38,7 @@ export interface InteractionApplicationCommandCallbackData
// components?: MessageComponents; // components?: MessageComponents;
flags?: MessageFlags; flags?: MessageFlags;
choices?: ApplicationCommandOptionChoice[]; choices?: ApplicationCommandOptionChoice[];
} }
/** /**
* @link https://discord.com/developers/docs/interactions/slash-commands#applicationcommandoptionchoice * @link https://discord.com/developers/docs/interactions/slash-commands#applicationcommandoptionchoice