Skip to content

Commit

Permalink
Version 3
Browse files Browse the repository at this point in the history
  • Loading branch information
TFAGaming committed Jun 6, 2024
1 parent 6d5c31b commit 7690837
Show file tree
Hide file tree
Showing 76 changed files with 1,634 additions and 1,855 deletions.
6 changes: 2 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
CLIENT_TOKEN = "Your bot token"
CLIENT_ID = "Your bot ID"
GUILD_ID = "Your guild ID"
MONGODB_URI = "Your mongodb URI string"
# Discord bot configuration
CLIENT_TOKEN="Your bot token"
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ package-lock.json

# DO NOT REMOVE THESE FOR SECURITY
src/config.js
.env
.env
database.yml
terminal.log
236 changes: 96 additions & 140 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,197 +1,153 @@
# <samp>DiscordJS-V14-Bot-Template</samp> v2
# <samp>DiscordJS-V14-Bot-Template</samp> v3

A simplified and popular Discord bot commands & events handler built with discord.js version 14 and written in JavaScript. This handler can load up to 4 different types of commands: Prefix, Slash, User context, and Message context. It can also handle components, including Buttons, Modals, Select menus (any type), and autocomplete.
A Discord bot commands, components and events handler based on **discord.js v14** and fully written in JavaScript.

Did you like my project? Click on the star button (⭐️) right above your screen, thank you!
Did you like the project? Click on the star button (⭐️) right above your screen, thank you!

## Features
- Using latest [discord.js v14.x version](https://github.com/discordjs/discord.js/releases).
- Updated to the latest version of [discord.js v14.x](https://github.com/discordjs/discord.js/releases).
- Supports all possible type of commands.
- Message commands (AKA: Prefix commands).
- Message commands.
- Application commands:
- Chat Input
- User context
- Message context
- Components Handler.
- Handles components.
- Buttons
- Select menus
- Modals
- Autocomplete
- Easy and simple to use.
- Advanced command options ([click here](#command-options)).
- Updated to latest discord.js version.
- Included a database: MongoDB.
- Advanced command options.
- Simple Database included (YAML).

## Commands, events, and components structure
## Commands, Components, and Events structure:
### Message commands:

This Discord bot template uses CommonJS modules. You cannot use `import`, `export`, or any related keywords from the ES6 modules.
[`Partial`](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype).<br>
`Awaitable` means the function might be **async**.


### Prefix commands:
```ts
module.exports = {
structure: {
name: string,
description: string,
aliases: string[],
permissions?: PermissionResolvable,
cooldown?: number,
globalCooldown?: boolean,
ownerOnly?: boolean,
developers?: boolean,
nsfw?: boolean
new MessageCommand({
command: {
name: string, // The command name
description?: string, // The command description (optional)
aliases?: string[], // The command aliases (optional)
permissions?: PermissionResolvable[], // The command permissions (optional)
},
run: (client, message, args) => void
};
options?: Partial<{
cooldown: number, // The command cooldown, in milliseconds
botOwner: boolean, // Bot owner can only run it? (true = yes, false = no)
guildOwner: boolean, // Guild owner can only run it? (true = yes, false = no)
botDevelopers: boolean, // Bot developers can only run it? (true = yes, false = no)
nsfw: boolean // The command contains NSFW content? (true = yes, false = no)
}>,
run: Awaitable<(client: DiscordBot, message: Message, args: string[]) => void> // The main function to execute the command
});
```

### Slash/User/Message context commands:
```ts
module.exports = {
structure: SlashCommandBuilder | ContextMenuCommandBuilder,
options?: {
cooldown?: number,
globalCooldown?: boolean,
ownerOnly?: boolean,
developers?: boolean,
nsfw?: boolean
},
run: (client, interaction) => void
};
```
### Application commands (Chat input, User context, Message context):

### Event listener:

The `...args` is a spread operator of arguments from the event chosen. For an example, if the event is **'messageCreate'**, the first argument is going to be type of **Message**.
[`APIApplicationCommand`](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-structure), [`Partial`](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype).<br>
`Awaitable` means the function might be **async**.

```ts
module.exports = {
event: string,
once?: boolean,
run: (client, ...args) => void
};
new ApplicationCommand({
command: APIApplicationCommand,
options?: Partial<{
cooldown: number, // The command cooldown, in milliseconds
botOwner: boolean, // Bot owner can only run it? (true = yes, false = no)
guildOwner: boolean, // Guild owner can only run it? (true = yes, false = no)
botDevelopers: boolean, // Bot developers can only run it? (true = yes, false = no)
}>,
run: Awaitable<(client: DiscordBot, interaction: Interaction) => void> // The main function to execute the command
});
```

### Component:
### Components:
#### Autocomplete:

The `interaction` is the interaction of the component. For an example, if the custom ID is from a button, the interaction is type of ButtonInteraction.
`Awaitable` means the function might be **async**.

```ts
module.exports = {
customId: string,
options?: {
public?: boolean
},
run: (client, interaction) => void
};
new AutocompleteComponent({
commandName: string,
run: Awaitable<(client: DiscordBot, interaction: AutocompleteInteraction) => void> // The main function to execute the command
});
```

## Requirements
### Packages:
- **chalk** v2.4.2
- **discord.js** v^14.13.0
- **dotenv** v^latest
- **mongoose** v^latest

> [!WARNING]
> Installing any version from the package `chalk` that is over **v2.4.2** will throw an error that you must enable ES6 modules, while this handler uses CommonJS modules.
### Platforms:
- **Node.js** v16.9.0 or newer

## Setup
1. Install Visual Studio Code.
2. Download this project as a **.zip** file: [Click here](https://github.com/TFAGaming/DiscordJS-V14-Bot-Template/archive/refs/heads/main.zip)
3. Extract the .zip file into a normal folder.
4. Open VSCode, click on **Open Folder**, and select the new created folder.
5. Go to `src/` and rename `example.config.js` to `config.js` and fill all the required values. You can use ENV instead of `config.js` to keep your bot token and ID, and your MongoDB URI in a secured place by renaming the file `.env.example` to `.env` and filling all required values.

> [!CAUTION]
> Sharing your Discord bot's token with anyone is a very risky move since you'll allow them to use your bot. This is also a dangerous move for the MongoDB database; we don't recommend using any public URIs or sharing your database connection URL.
#### Buttons, Select Menus, and Modals:

6. Initialize a new npm package:
[`Partial`](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype).<br>
`Awaitable` means the function might be **async**.

```ts
new Component({
customId: string,
type: 'modal' | 'select' | 'button',
options?: Partial<{
public: boolean // Other users can use the main interaction author button/select? (true = yes, false = no)
}>
run: Awaitable<(client: DiscordBot, interaction: Interaction) => void> // The main function to execute the command
});
```
npm init -y
```

7. Install all [required packages](#packages):

```
npm install chalk@2.4.2 discord.js@latest dotenv mongoose
```

8. To start your bot, run `node .` or `npm run start`.
9. Enjoy. =)

<!--
## Hosting (<img src="https://media.discordapp.net/attachments/1111644651036876822/1124045180484472882/discloud_white_horizon-e96efbfa.png?width=960&height=163" width=100>)
Use [Discloud](https://discloudbot.com/)! A trust-worthy Discord bot hosting service.
### Events:

1. Login using your Discord/GitHub account, and then don't close the Dashboard tab (because we need it later).
2. Go to your project on VSCode, create a new file `discloud.config`. This file is a configuration file for Discloud apps servers. The `discloud.config` file content must be like the codeblock below, and remember to change **YOUR_BOT_ID** to your actual bot ID.
`Awaitable` means the function might be **async**.<br>
`K` is a type parameter, extends `keyof ClientEvents`.

```apache
ID = YOUR_BOT_ID
TYPE = bot
MAIN = src/index.js
RAM = 100
AUTORESTART = true
VERSION = latest
API = tools
```ts
new Event({
event: K,
once?: boolean, // The event can only happen once? (true = yes, false = no)
run: Awaitable<(client: DiscordBot, ...args: ClientEvents[K]) => void>
});
```

3. Open explorer and navigate using the path of your project. Hold the **CTRL** key, and click on `src/`, `package.json`, and `discloud.config`. Now release the key, and right click on the selected files/folders and click on **Convert into .zip file**.
4. Go back to the Discloud dashboard, click on **Add App**, and then upload the .zip file in the upload box. Make sure to accept the ToS of the service and then click on **Upload**.
5. Wait for at least 20 seconds (depends on your Internet speed), and then your bot should be on.
## Dependencies
- **colors** → latest
- **discord.js** → 14.13.0 or newer
- **dotenv** → latest
- **quick-yaml.db** → latest

> **Warning**
> Discloud has recently made every Free plan servers into **15 days** hosting only. Use the command `.rw` from their bot in the commands channel (on their Discord server) to reset the timer. Join the server: [Click here!](https://discord.gg/discloud-584490943034425391)
> [!NOTE]
> **Node.js v16.11.0** or newer is required to run **discord.js**.
-->

## Command options
The command options, each property is optional, which means it's allowed to provide an `undefined` value to one of these properties below.
## Setup
1. Install a code editor ([Visual Studio Code](https://code.visualstudio.com/Download) for an example).
2. Download this project as a **.zip** file: [Download](https://github.com/TFAGaming/DiscordJS-V14-Bot-Template/archive/refs/heads/main.zip)
3. Extract the **.zip** file into a normal folder.
4. Open your code editor, click on **Open Folder**, and select the new created folder.
5. Rename the following files:

- `permissions` (**PermissionFlagsBits** | **string**): The required permissions for the command, available to message commands only.
- `cooldown` (**number**): The cooldown of the command, in milliseconds.
- `globalCooldown` (**boolean**): Determines whether the cooldown is global or not.
- `developers` (**boolean**): Determines whether the command is executable only to the developers of the bot.
- `nsfw` (**boolean**): Determines whether this command is executable only in NSFW channels.
- `src/example.config.js``src/config.js`: Used for handler configuration.
- `.env.example``.env`: Used for secrets, like the Discord bot token.
- `example.database.yml``database.yml`: Used as a main file for the database.
- `example.terminal.log``terminal.log`: Used as a clone of terminal (to save previous terminal messages).

## Component options
The component options, each property is optional which means it's allowed to provide an `undefined` value to one of these properties below.
6. Fill all the required values in **config.js** and **.env**.

- `public` (**boolean**): If set to true, the component will be available to everyone (default), if set to false, the component will be available to the component owner (original interaction user) only.
> [!CAUTION]
> Please remember not to share your Discord bot token! This will give access to attackers to do anything they want with your bot, so please keep the token in a safe place, which is the **.env** file.
## FAQs
### 1. I'm getting this error: "Unable to load application commands to Discord API"
- The bot token and/or bot ID are invalid.
- The bot token and bot ID are not from the same Discord bot.
- Too many application commands.
- 100 Global Chat input (AKA: Slash) commands.
- 5 Global User context commands.
- 5 Global Message context commands.
- Invalid application command structure.
- Missing description, type, or any required properties for a command.
- The command cannot be modified.
- The Discord API has an issue ([Verify Discord status](https://discordstatus.com/)).
7. Initialize a new project: `npm init` (To skip every step, do `npm init -y`).
8. Install all [required dependencies](#dependencies): `npm install colors discord.js dotenv quick-yaml.db`

[Learn more...](https://discord.com/developers/docs/interactions/application-commands#registering-a-command)
9. Run the command `node .` or `npm run start` to start the bot.
10. Enjoy! The bot should be online.

### 2. I'm unable to view any application commands, no errors in console?
This is a common problem for developers, to fix this issue, restart the Discord app or go in a different text channel.
## Contributing
Feel free to fork the repository and submit a new pull request if you wish to contribute to this project.

### 3. Is MongoDB required?
No, MongoDB is not required. There is an option to disable it in `config.js` so you will avoid errors from the commands that requires the database, such as `?prefix`. If you want to use the database, visit [MongoDB website](https://www.mongodb.com/).
Before you submit a pull request, ensure you tested it and have no issues. Also, keep the same coding style, which means don't use many unnecessary spaces or tabs.

## Contributors
Thank you to all the people who contributed to **DiscordJS-V14-Bot-Template**!

<img src="https://contrib.rocks/image?repo=TFAGaming/DiscordJS-V14-Bot-Template">

## Support
Join our Discord server if you need any help!
Join our Discord server if you have any questions to ask, or if you have a problem with this project, you can go to the [issues section](https://github.com/TFAGaming/DiscordJS-V14-Bot-Template/issues) and submit a new issue.

<a href="https://discord.gg/E6VFACWu5V">
<img src="https://discord.com/api/guilds/918611797194465280/widget.png?style=banner3">
Expand Down
75 changes: 0 additions & 75 deletions command.example.txt

This file was deleted.

Loading

0 comments on commit 7690837

Please sign in to comment.