GitXplorerGitXplorer
b

bot-base

public
0 stars
0 forks
1 issues

Commits

List of commits on branch master.
Unverified
56aff4f575c829a0bae43796222bb0d7fb72d307

Update README

bbraxtonhall committed 3 years ago
Unverified
f6bc13fece53bb9e4d52c714e88e890a7d089d50

Bump version number

bbraxtonhall committed 3 years ago
Unverified
193dda30d7c0ca1a4c91f1a15d0fdf3e60819eca

Hotfix wrong return value in mentionBot

bbraxtonhall committed 3 years ago
Unverified
6a7235a78e7daaa573a8ad4a40d530d95298d37d

Use correct event name in mention bot

bbraxtonhall committed 3 years ago
Unverified
70723982a2c793420a0ba6d38c1aa796ab27ac13

Add MentionBot listener

bbraxtonhall committed 3 years ago
Unverified
c39303c6b0999a0908ef6725f0f470d9016724dc

Log errors instead of crashing

bbraxtonhall committed 3 years ago

README

The README file for this repository.

bot-base

Example

App

import {startDiscord} from "@ubccpsc310/bot-base";
import {Intents} from "discord.js";

startDiscord({
	commandDirectory: `${__dirname}/commands`,
	listenerDirectory: `${__dirname}/listeners`,
	intents: [Intents.FLAGS.GUILDS],
	token: process.env.DISCORD_BOT_TOKEN,
});

Command

import {Command} from "@ubccpsc310/bot-base";
import {Client, Message} from "discord.js";

const echo: Command = {
	name: "echo",
	description: "Repeats a message",
	usage: "echo <message>?",
	procedure(client: Client, message: Message, args: string[]) {
		let reply;
		if (args.length > 0) {
			reply = args.join(" ");
		} else {
			reply = "... echo";
		}
		return message.channel.send(reply);
	},
};

export default echo;

Listener

import {Listener, Log} from "@ubccpsc310/bot-base";
import {Client} from "discord.js";

const ready: Listener<"ready"> = {
	event: "ready",
	procedure(client: Client) {
		Log.info("Bot started 👀");
	},
};

export default ready;

API

/**
 * The main function!
 * Registers commands and listeners, and logs into discord
 * @param options are defined below
 * @return Discord.js Client
 */
function startDiscord(options: Options): Promise<Client> {
	// ...
}

/**
 * Returns a db object for persisting/reading data
 */
function getDatabaseController(): DatabaseController {
	// ...
}

/**
 * Logging object used in place of `console`
 * Only prints to console if logging level permits,
 * set by process.env variable `LOG_LEVEL`
 * which should be DEBUG | INFO | WARN | ERROR | NONE
 * (defaults to NONE)
 */
const Log: {
	debug(...args: any[]): void;
	info(...args: any[]): void;
	warn(...args: any[]): void;
	error(...args: any[]): void;
} = {
	/* ... */
};

interface Options {
	// Directory where Command ts/js files live
	// All files in this directory should have
	// a default export that is a Command
	commandDirectory: string;

	// Directory where Listener ts/js files live
	// All files in this directory should have
	// a default export that is a Listener
	listenerDirectory: string;

	// See https://discord.js.org/#/docs/main/stable/typedef/IntentsResolvable
	intents: IntentsResolvable;

	// Bot token found in Discord's
	// developer portal
	token: string;
}

interface Command {
	// name that users will invoke the command with
	name: string;
	description: string;
	// example of how to use the command
	usage: string;
	// procedure that should be invoked whenever command is used
	procedure: (client: Client, message: Message, args: string[]) => void;
}

// ClientEvents comes from discord.js
interface Listener<T extends keyof ClientEvents> {
	// example "message" or "voiceStateUpdate"
	event: T;
	// procedure that should be invoked whenever event occurs
	procedure: (client: Client, ...args: ClientEvents[T]) => void;
}

interface DatabaseController {
	get<T extends Entity>(collection: string, id: string): Promise<T>;
	set<T extends Entity>(collection: string, value: T): Promise<void>;
	scan<T extends Entity>(collection: string, query: any): Promise<T[]>;
	delete(collection: string, id: string): Promise<void>;
}

interface Entity {
	id: string;
}