diff --git a/examples/builderComponent.ts b/examples/components/mod.ts similarity index 72% rename from examples/builderComponent.ts rename to examples/components/mod.ts index 94a1aa1..fb52bc6 100644 --- a/examples/builderComponent.ts +++ b/examples/components/mod.ts @@ -1,57 +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().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(); +import 'https://deno.land/std@0.146.0/dotenv/load.ts'; +import { + ActionRowBuilder, + ButtonBuilder, + ButtonStyles, + ComponentInteraction, + GatewayIntents, + InteractionResponseTypes, + Session, +} from 'https://x.nest.land/biscuit@0.1.0/mod.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().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(); diff --git a/examples/bun.js b/examples/hello-bun/index.js similarity index 89% rename from examples/bun.js rename to examples/hello-bun/index.js index 0dea8f8..f575a3a 100644 --- a/examples/bun.js +++ b/examples/hello-bun/index.js @@ -1,34 +1,30 @@ -/** - * Bun example - * this example should work on most systems, but if it doesn't just clone the library and import everything from mod.ts - */ - -import { GatewayIntents, Session } from "@oasisjs/biscuit"; -// if it didn't worked use: -// const { GatewayIntents, Session } = require("@oasisjs/biscuit"); - -const token = process.env.TOKEN; -const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages; -const session = new Session({ token, intents }); - -// uncomment to debug stuff -// session.on("debug", (any) => { -// console.debug(any); -// }) - -session.on("ready", (payload) => { - console.log("Logged in as:", payload.user.username); -}); - -session.on("messageCreate", async (message) => { - if (message.content.startsWith("whatever")) { - const whatever = await message.fetch().then(console.log); - console.log(whatever); - } - - if (message.content.startsWith("ping")) { - message.reply({ content: "pong!" }).catch((err) => console.error(err)); - } -}); - -await session.start(); +/** + * Bun example + * this example should work on most systems, but if it doesn't just clone the library and import everything from mod.ts +*/ + +import { GatewayIntents, Session } from "@oasisjs/biscuit"; +// if it didn't worked use: +// const { GatewayIntents, Session } = require("@oasisjs/biscuit"); + +const token = process.env.TOKEN; +const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages; +const session = new Session({ token, intents }); + +session.on("ready", (payload) => { + console.log("Logged in as:", payload.user.username); +}); + +session.on("messageCreate", async (message) => { + if (message.content.startsWith("whatever")) { + const whatever = await message.fetch().then(console.log); + console.log(whatever); + } + + if (message.content.startsWith("ping")) { + message.reply({ content: "pong!" }).catch((err) => console.error(err)); + } +}); + +await session.start(); + \ No newline at end of file diff --git a/examples/deno.ts b/examples/hello-deno/mod.ts similarity index 89% rename from examples/deno.ts rename to examples/hello-deno/mod.ts index 7275dc7..e247c3f 100644 --- a/examples/deno.ts +++ b/examples/hello-deno/mod.ts @@ -1,36 +1,39 @@ -/** - * Deno example - */ - -import "https://deno.land/std@0.146.0/dotenv/load.ts"; -import { GatewayIntents, Session } from "https://x.nest.land/biscuit@0.1.0/mod.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 }); - -session.on("ready", (payload) => { - console.log("Logged in as:", payload.user.username); -}); - -const PREFIX = ">"; - -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(); - - if (name === "ping") { - message.reply({ content: "pong!" }); - } -}); - -await session.start(); +/** + * Deno example +*/ + +import "https://deno.land/std@0.146.0/dotenv/load.ts"; + +// Session for create a new bot and intents +import { GatewayIntents, Session } from "https://x.nest.land/biscuit@0.1.0/mod.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 }); + +session.on("ready", (payload) => { + console.log("Logged in as:", payload.user.username); +}); + +const PREFIX = ">"; + +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(); + + if (name === "ping") { + message.reply({ content: "pong!" }); + } +}); + +await session.start(); + \ No newline at end of file diff --git a/examples/nodemodules.mjs b/examples/hello-node-mjs/index.mjs similarity index 85% rename from examples/nodemodules.mjs rename to examples/hello-node-mjs/index.mjs index f566931..f6a04c7 100644 --- a/examples/nodemodules.mjs +++ b/examples/hello-node-mjs/index.mjs @@ -2,10 +2,14 @@ * Biscuit node example */ +// process for get the token /** @type {NodeJS.Process} process */ import process from 'node:process'; + +// Session for create a new bot and intents import { Session, GatewayIntents } from '@oasisjs/biscuit'; +// Discord bot token /** @type {string} token */ const token = process.env.TOKEN || "YOUR_TOKEN_HERE"; @@ -15,6 +19,8 @@ if (token === "") { const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages; const session = new Session({ token, intents }); + +// Command prefix const PREFIX = ">"; session.on("ready", (data) => { @@ -35,8 +41,4 @@ session.on("messageCreate", (message) => { } }); -try { - session.start(); -} catch(err){ - throw err; -} \ No newline at end of file +session.start(); \ No newline at end of file diff --git a/examples/node.js b/examples/hello-node/index.js similarity index 85% rename from examples/node.js rename to examples/hello-node/index.js index aedac40..b71827a 100644 --- a/examples/node.js +++ b/examples/hello-node/index.js @@ -2,10 +2,14 @@ * Biscuit node example */ +// process for get the token /** @type {NodeJS.Process} process */ const process = require("node:process"); + +// Session for create a new bot and intents const { Session, GatewayIntents } = require("@oasisjs/biscuit"); +// Discord bot token /** @type {string} token */ const token = process.env.TOKEN || "YOUR_TOKEN_HERE"; @@ -15,6 +19,8 @@ if (token === "") { const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages; const session = new Session({ token, intents }); + +// Command prefix const PREFIX = ">"; session.on("ready", (data) => { @@ -35,8 +41,4 @@ session.on("messageCreate", (message) => { } }); -try { - session.start(); -} catch(err){ - throw err; -} \ No newline at end of file +session.start(); \ No newline at end of file diff --git a/examples/slashCommand.ts b/examples/slash-commands/mod.ts similarity index 96% rename from examples/slashCommand.ts rename to examples/slash-commands/mod.ts index dbf9148..8f52ab0 100644 --- a/examples/slashCommand.ts +++ b/examples/slash-commands/mod.ts @@ -1,43 +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(); +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();