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!
- Create a project
- Creating Slash Commands
- Deploying/Registering Slash Commands
- Creating Events
- Access the Client object
- Utils
- Config File
If you plan on using github in your project, simply click the Use this template
button to get started.
Choose one of the following:
- Open a terminal in the folder you want to create the project in and run
git clone https://github.com/ItzDerock/discord.js-typescript-bot-core.git .
- Click the
Code ▼
button on the top of this page and chooseDownload ZIP
- Open the zip archive using whatever you want (Windows File Explorer, Winrar, 7z, etc).
- Drag and drop all the files in
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.
- Change
super("example", "An example command.");
. The first argument is the command name, and the second is a command description. - A third optional paramemeter can be supplied with the following options:
Key | Description | Default |
---|---|---|
requiredPermissions |
An array of PermissionResolvables that contains the required permissions to use this command | undefined |
- Add a function to the class called
build
that takes in one argument (client) and returns eitherSlashCommandBuilder
from @discordjs/builders orRESTPostAPIApplicationCommandsJSONBody
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,
- Create an
.ts
file in./src/events
. You can look atready.ts
orcommands.ts
for a base. - Export a class that extends
Event
found in./src/structures/Event.ts
. - In the
constructor()
runsuper("Event friendly name (for logging)", "eventName in client.on")
(for example,super("Slash commands", "interactionCreate")
) - Create a function
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}!`)
]
})
}
}
Simply import ./src/index.ts
. Client is not the default export, so you will need to extract the client property. (ex: import { client } from "..";
).
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
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.
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.