Skeleton for a discord.js v13 bot written in TS with Slash Command support.
This repository is a template for creating a Discord bot using Discord.js v14 with Slash Command support.
This project is for people familiar with TypeScript and Discord.js. Do NOT use if you are unfamiliar with either. No Discord.js/TypeScript support will be given!
If you plan on using github in your project, simply click the Use this template
button to get started.
Choose one of the following:
git clone https://github.com/ItzDerock/discord.js-typescript-bot-core.git .
Code
button on the top of this page and choose Download ZIP
discord.js-typescript-bot-core-main
folder into your project root.To create a slash command using this package, go into ./src/commands/
Copy and paste the example.ts
file.
super("example", "An example command.");
. The first argument is the command name, and the second is a command description.Key | Description | Default |
---|---|---|
requiredPermissions |
An array of PermissionResolvables that contains the required permissions to use this command | undefined |
build
that takes in one argument (client) and returns either SlashCommandBuilder
from @discordjs/builders or RESTPostAPIApplicationCommandsJSONBody
from discord-api-types.Example command with a boolean and string option.
import SlashCommand from "../structures/Command";
import { Client, CommandInteraction, SlashCommandBuilder } from "discord.js";
import { primaryEmbed } from "../utils/embeds";
import { RESTPostAPIApplicationCommandsJSONBody } from "discord-api-types";
export default class ExampleCommand extends SlashCommand {
constructor() {
super("example", "An example command.");
}
exec(interaction: CommandInteraction) {
const boolean = interaction.options.getBoolean('boolean');
const string = interaction.options.getString('string');
interaction.reply({
embeds: [
primaryEmbed('', `boolean: ${boolean}\nstring: ${string}`)
]
});
}
build(client: Client<boolean>, defaultCommand: SlashCommandBuilder) {
return defaultCommand
.addBooleanOption(boolean => boolean.setName('boolean').setDescription('test boolean option').setRequired(true))
.addStringOption(string => string.setName('string').setDescription('test string option').setRequired(true))
.toJSON();
}
}
When you create a slash command, the bot will not automatically create it.
To deploy all new slash commands, run npm start deploy
or node . deploy
(if built and you are in ./dist)
To deploy only certain commands, or to edit existing commands, run npm start edit [command 1 name] [command 2 name] [...]
.
To register an event,
.ts
file in ./src/events
. You can look at ready.ts
or commands.ts
for a base.Event
found in ./src/structures/Event.ts
.constructor()
run super("Event friendly name (for logging)", "eventName in client.on")
(for example, super("Slash commands", "interactionCreate")
)exec
that takes the arguments provided by the event.Example Event:
import { GuildMember } from 'discord.js';
import Event from '../structures/Event';
import { primaryEmbed } from '../utils/embeds';
export default class ReadyEvent extends Event {
constructor() { super('Member Welcomes', 'guildMemberAdd'); };
async exec(member: GuildMember) {
const welcome = member.guild.channels.resolve('a cool channel');
if(!welcome || !welcome.isText()) return;
welcome.send({
embeds: [
primaryEmbed("New Member!", `Welcome <@${member.id}> to ${member.guild.name}!`)
]
})
}
}
Client
Simply import ./src/index.ts
. Client is not the default export, so you will need to extract the client property. (ex: import { client } from "..";
).
./src/utils/logger.ts
)Exports a createLogger
function that allows you to create a logger named the first parameter.
Logs are saved in ./logs/
and each unique logger name has their own file. The logger is a combination of bunyan
and bunyan-format
.
To disable logging, set the NO_LOG_DIR
env var to something that is not null/falsy
./src/utils/embeds.ts
)Exports primaryEmbed
and errorEmbed
functions to quickly create an embed that matches the color scheme used in other embeds. Functions take in two parameters: title, description.
./src/utils/config.ts
)This bot uses a yml config type, and node.js does not support importing .yml files.
To access the config, run the default exported function from ./src/utils/config.ts
In this file you can also change the type definition for the config.
The config is a yml file that will be loaded from the current working directory.
Inside the config, you set your bot token with the token
option.
You can also change the global embed style under embeds.primary
and embeds.error
. You can set stuff like footer.text
to set a global footer.