Update examples

This commit is contained in:
Nicolás Serna 2022-07-14 14:14:18 -03:00
parent af73561279
commit 9193346f52
6 changed files with 183 additions and 180 deletions

View File

@ -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<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();
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<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

@ -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();

View File

@ -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();

View File

@ -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;
}
session.start();

View File

@ -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;
}
session.start();

View File

@ -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();