Merge pull request #1 from atridadl/dev

0.1.0
 Initial release with two commands: Wryna and Ping
🚧 Contribution docs
This commit is contained in:
Atridad Lahiji 2023-05-30 17:28:44 -06:00 committed by GitHub
commit 0fc514add7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 123 additions and 18 deletions

View file

@ -1,13 +1,27 @@
# Minimal Bot Template # Himbot
Just the minimum to get a working bot using interactions. A discord bot written in Typescript and running on the Deno runtime.
Make sure to install the latest version when you use it. ## It's dangerous to go alone! Take this!
## Setup - Install Deno [here](https://deno.com/manual@v1.34.1/getting_started/installation) (required)
- The [Deno VSCode Extension](https://marketplace.visualstudio.com/items?itemName=denoland.vscode-deno) (recommended)
Just rename `.env.example` to `.env` and fill it with your bot token. ## Structure
## Run Bot Commands and Events are all stored in named files within the src/commands and src/events directories respectively.
Usage and example ts files can be found in the examples folder.
- deno run -A mod.ts To generate a new Command or Event run `deno task new:command` or `deno task new:event` respectively.
## Running Locally
- Copy .env.example and rename to .env
- Create a Discord Bot with all gateway permissions enabled
- Generate a token for this discord bot and paste it in the .env for BOT_TOKEN
- Run `deno run --allow-all mod.ts` to run locally
## Adding the bot to a server
Use the following link (replacing BOT_TOKEN with your own Token of course...) to add your bot:
https://discord.com/oauth2/authorize?client_id=BOT_TOKEN&scope=bot&permissions=8

View file

@ -1,6 +1,8 @@
{ {
"tasks": { "tasks": {
"dev": "deno run --watch mod.ts", "dev": "deno run --watch mod.ts",
"start": "deno run --allow-env --allow-net --allow-read --allow-write mod.ts" "start": "deno run --allow-env --allow-net --allow-read --allow-write mod.ts",
"new:command": "cp ./examples/newCommand.txt ./src/commands/newCommand.ts",
"new:event": "cp ./examples/newEvent.txt ./src/events/newEvent.ts"
} }
} }

45
examples/newCommand.txt Normal file
View file

@ -0,0 +1,45 @@
import {
ApplicationCommandOptionTypes,
ApplicationCommandTypes,
InteractionResponseTypes,
} from "../../deps.ts";
import { createCommand } from "./mod.ts";
// This is the command definition. Please ensure the name matches the file name
// Also, make sure the description is meaningfull
createCommand({
name: "command",
description: "This is an example command",
type: ApplicationCommandTypes.ChatInput,
scope: "Global",
options: [
// This is an option that can be sent after the command and used for input
{
type: ApplicationCommandOptionTypes.String,
name: "input",
description: "Text you would like to send to this command.",
// This will make the option required
required: true,
},
],
execute: async (bot, interaction) => {
// In this example, we only use "input", so we just need the one item from an array of length 1.
// Options always returns an array, even if there is a single element.
const input = interaction.data?.options?.find(
(option) => option.name === "input"
);
// This is how the bot responds to interactions. In this case, it is via a reply.
await bot.helpers.sendInteractionResponse(
interaction.id,
interaction.token,
{
type: InteractionResponseTypes.ChannelMessageWithSource,
data: {
content: `${input?.value} was ${interaction.user.username}'s nickname in highschool.`,
},
}
);
},
});

15
examples/newEvent.txt Normal file
View file

@ -0,0 +1,15 @@
import { events } from "./mod.ts";
import { logger } from "../utils/logger.ts";
// Log the event name here
const log = logger({ name: "Event: messageCreate" });
/*
The properties of events are tied to specific discord events.
Please ensure that both the event type and the property (in this case message)
accurately reflect what you want to listen for.
*/
events.exampleEvent = (bot, message) => {
// In this example, the message contents are simply being logged
log.info(`${message.tag}: ${message.content}`);
};

2
mod.ts
View file

@ -22,8 +22,6 @@ await fastFileLoader(paths).catch((err) => {
Deno.exit(1); Deno.exit(1);
}); });
console.log("EVENTS!: ", events);
export const bot = enableCachePlugin( export const bot = enableCachePlugin(
createBot({ createBot({
token: Deno.env.get("BOT_TOKEN") || "", token: Deno.env.get("BOT_TOKEN") || "",

39
src/commands/wryna.ts Normal file
View file

@ -0,0 +1,39 @@
import {
ApplicationCommandOptionTypes,
ApplicationCommandTypes,
InteractionResponseTypes,
} from "../../deps.ts";
import { createCommand } from "./mod.ts";
// Thanks for the bot name idea Wryna!
createCommand({
name: "wryna",
description: "What was your nickname in highschool?",
type: ApplicationCommandTypes.ChatInput,
scope: "Global",
options: [
{
type: ApplicationCommandOptionTypes.String,
name: "input",
description: "Text you would like to send to this command.",
required: true,
},
],
execute: async (bot, interaction) => {
const input = interaction.data?.options?.find(
(option) => option.name === "input"
);
await bot.helpers.sendInteractionResponse(
interaction.id,
interaction.token,
{
type: InteractionResponseTypes.ChannelMessageWithSource,
data: {
content: `${input?.value} was ${interaction.user.username}'s nickname in highschool.`,
},
}
);
},
});

View file

@ -1,8 +0,0 @@
import { events } from "./mod.ts";
import { logger } from "../utils/logger.ts";
const log = logger({ name: "Event: messageCreate" });
events.messageCreate = (bot, message) => {
log.info(`${message.tag}: ${message.content}`);
};