This is a template for a Discord bot using discord.js v14.15.3 and MySQL. Feel free to use and customize it for your own needs!
- π¬ Slash & message commands: Supports both slash commands and traditional message commands.
- π Simple event management: Easy setup and handling of Discord events.
- π οΈ Interactive components: Manage buttons, select menus, context menus, modals, and autocomplete effortlessly.
- π¨οΈ Message reactions & mentions: React to messages and handle bot mentions seamlessly.
- π Multilingual support: Users can select their preferred language for bot responses.
- π Auto generated help
- ποΈ MySQL integration: Connects with MySQL databases for data storage and management.
- π¨ Easy embed creation: Create and customize rich embed messages with ease.
- π Webhook management: Register, handle, and send webhooks with simplicity.
- Node.js v16 or higher
- npm (comes with Node.js)
- A Discord account and a server to add the bot to
- A MySQL server
-
Clone the repository:
git clone https://github.com/Liammmmmmmm/discord-js-template-v14.git cd discord-js-template-v14
-
Install the required dependencies:
npm install
-
Copy the
.env.example
file to.env
:cp .env.example .env
-
Open the
.env
file with your preferred text editor and fill in the required fields:DISCORD_TOKEN = YOUR_DISCORD_BOT_TOKEN DB_HOST = YOUR_DATABASE_HOST DB_USER = YOUR_DATABASE_USER DB_PASSWORD = YOUR_DATABASE_PASSWORD DB_NAME = YOUR_DATABASE_NAME
To start the bot, run the following command:
node index.js
commands/
ββ new_folder/
β ββ changeLang.js
β ββ listlangs.js
ββ utils/
β ββ help.js
β ββ ping.js
β ββ sendwebhook.js
β ββ setPrefix.js
ββ othercommandfolder/
ββ yourcommand.js
ββ yoursecondcommand.js
The folders are category for your commands (detailed in the auto generated help section) and each one contain files with you commands
By default, the help command is auto generated. Folders are category described in the lang file, and take the description of each command. You can change the display of the commands in each lang file, default is :
CATEGORY_NAME: CATEGORY_DESCRIPTION
- COMMAND_NAME: COMMAND_DESCRIPTION
When the help command is used with an arg (witch is a command or alias name), it show the advanced description of the command (writen in each language file). Aliases list is also given.
You can choose to not display a command in the help with help parameter to 0 in the command file.
Just list all of the possibles languages of the bot.
Change the bot language to one of the possible languages of the bot
Just ping the bot.
Change the bot prefix for the server. Availaible for every user with the Administrator permission
Webhook sending example. Detailed in the webhook managment section section
This bot template supports both slash commands and traditional message commands, allowing you to define and manage commands in a unified manner. You can set up commands that respond to both message-based interactions and slash commands with the same functionality.
Here's an example of how to set up a command that works with both slash commands and message commands:
const commandName = "commandname"; // Your command name here
module.exports = {
name: commandName,
aliases: ["commandnamealias"], // just a list of aliases
help: 1, // display or not in the auto generated help
message: async (client, message, args) => {
// manage here the arguments in the message command
const text = new Txt(); // Txt object, more explainaitions in language section
await text.init(message.author.id);
// check the arg amount and send error message if needed
if(validArgAmount(args, 1, text) != 1) return message.reply(validArgAmount(args, 1, text));
// when everythings good, execute your command with the args
executeCMD(client, message, {yourargname: args[0]},text);
},
slash: new SlashCommandBuilder()
.setName(commandName)
.setDescription(require("../../langs/texts/" + settings.messages.defaultLang).texts[commandName].description)
.addStringOption(option =>
option.setName('yourargname')
.setDescription(require("../../langs/texts/" + settings.messages.defaultLang).texts[commandName].arg1)
.setRequired(true)
.setMaxLength(25)
)
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator),
async execute(client, interaction) {
// manage here the arguments in the slash command
const text = new Txt();
await text.init(interaction.author.id);
await executeCMD(client, interaction, {yourargname: interaction.options.getString('yourargname')}, text);
},
}
async function executeCMD(client, message, args, text) {
// Command execution...
message.reply("example: " + args.yourargname);
}
name
: The command's unique name.aliases
: Alternative names for the command.help
: Determines if the command appears in auto-generated help (1 = yes, 0 = no).message
: Handles command execution from messages.slash
: Configures the slash command usingSlashCommandBuilder
.executeCMD
: Function where the actual command logic is processed.
Easily set up and handle Discord events with a straightforward approach.
Here's an example of how to manage a basic event, such as when the bot is ready:
module.exports = {
name: Events.ClientReady,
once: true,
execute(client) {
info(`Ready! Logged in as ${client.user.tag}`);
},
}
name
: Specifies the type of event to handle (e.g.,Events.ClientReady
).once
: Indicates whether the event should only be handled once (true
= once,false
= multiple times).execute
: Function that contains the logic to run when the event is triggered. For theClientReady
event, it logs a message indicating the bot is online.
π οΈ Interactive components: Manage buttons, select menus, context menus, modals, and autocomplete effortlessly.
Manage buttons, select menus, context menus, modals, and autocomplete with ease.
Here's how you can set up a simple button interaction handler:
module.exports = {
customId: 'example-button-id',
run: async (client, interaction) => {
await interaction.reply({
content: 'Replied from a Button interaction!',
ephemeral: true
});
}
}
customId
: A unique identifier for the button, used to distinguish different buttons in your bot.run
: The function that executes when the button is clicked. It contains the logic for handling the interaction.
File Structure:
messagesReactions/
ββ messagesReaction.js
ββ onBotMention.js
ββ randomMessages.js
Contains a single function that handles what happens when the bot is mentioned in a message.
Contains a switch case to handle different random messages. You can customize it to perform various actions based on the message content.
Combines the functionalities of onBotMention.js
and randomMessages.js
. You can also add functions from other files if you want to include more functionalities beyond just random messages.
This feature allows users to choose their preferred language for bot responses. The bot retrieves and uses language-specific texts based on each user's settings.
File Structure:
langs/
ββ texts/
β ββ en.js
β ββ fr.js
β ββ ... (additional language files)
ββ langs.js
You can find next to lang an exemple of the user table.
You can add a language just by adding a file languagename
.js
Txt
Class
The Txt
class is used to manage user language settings and retrieve localized texts. It provides methods for initializing user language settings, retrieving localized texts, changing the language, and more.
Key Methods:
-
init(userid)
: Initializes the language setting for a user. It fetches the language from the database and sets it, defaulting to a predefined language if needed. -
get(command, text, objects)
: Retrieves the localized text for a given command and text index. If the text is not found in the user's language, it falls back to the default language. Optionally, you can replace placeholders in the text with values from theobjects
. -
changeLanguage(lang)
: Changes the user's language in the database and updates the current language setting. -
getLanguage()
: Retrieves the current language of the user from the database.
Example Usage:
const { Txt } = require('./langs');
// Initialize Txt instance for a user
let text = new Txt();
await text.init('user-id-here');
// Get localized text for a command
const localizedText = text.get('commandName', 'textIndex', { VARIABLE: 'value' });
// Change user language
await text.changeLanguage('fr'); // Change to French
In this setup:
langs/texts/
contains language files for different languages.Txt
class handles language management and localization.
This feature provides an easy way to connect to and interact with a MySQL database. It allows for executing SQL queries and managing database connections.
File: SQLRequest.js
Key Components:
DatabaseConnection
Class: Manages database connections and SQL queries. By default, it uses the database configuration from the.env
file, but you can configure it to use different databases if needed.
Class Methods:
-
setDB(database, host, user, password)
: Allows you to change the default database connection settings. This method updates the configuration with the provided database name, host, user, and password. -
request(sql, params)
: Executes a SQL query with the given parameters. It returns a promise that resolves with the result of the query.
Example Usage:
1. Default Database Connection
const DatabaseConnection = require('./SQLRequest');
const db = new DatabaseConnection();
db.request("SELECT * FROM users WHERE id = ?", [userId])
.then(results => {
console.log(results);
})
.catch(err => {
console.error(err);
});
2. Custom Database Connection
const DatabaseConnection = require('./path/to/DatabaseConnection');
const db = new DatabaseConnection();
db.setDB('otherDatabase', 'host', 'user', 'password');
db.request("SELECT * FROM users WHERE id = ?", [userId])
.then(results => {
console.log(results);
})
.catch(err => {
console.error(err);
});
Class Overview:
constructor
: Sets up the connection using environment variables defined in the.env
file.setDB
: Updates the connection settings for different databases.request
: Performs SQL queries and handles results or errors, providing debugging information for successful and failed requests.
This integration simplifies database operations, making it easy to perform queries and manage data within your bot.
The DefaultEmbed
class makes it simple to create rich embed messages in Discord bots with consistent styling. It builds on the EmbedBuilder
from Discord.js, letting you set default properties like color, footer, author, and thumbnail with just a few lines of code.
File: DefaultEmbeds.js
Key Features:
DefaultEmbed
Class: ExtendsEmbedBuilder
to easily apply default settings, ensuring a uniform look for your embeds.
Main Method:
setDefault(embed, message)
: Applies default settings (color, footer, author, etc.) based on predefined configurations. It uses dynamic placeholders (e.g., server name, user avatar) that are filled in from the message context.
Example Usage:
const { DefaultEmbed } = require('./DefaultEmbeds');
const embed = new DefaultEmbed()
.setDefault('primary', message)
.setTitle('Custom Title')
.setDescription('Custom Description');
message.channel.send({ embeds: [embed] });
const embeds = {
primary: {
color: "#dbaf00",
header: {
author: "%SERVER_NAME%",
authorImageURL: "%SERVER_IMAGE%",
authorURL: "https://exemple.com",
thumbnai: "https://c.clc2l.com/t/d/i/discord-4OXyS2.png"
},
footer: {
text: "Secondary exemple",
imageURL: "",
timestamp: true,
}
}
}
The WebhookMessage
class provides a straightforward way to manage Discord webhooks, allowing for easy registration, handling, and sending of messages through webhooks. This class ensures that webhooks are efficiently reused or created as needed, with the ability to store and retrieve webhook information from a database.
File: WebhookMessage.js
Main Methods:
-
init(message)
: Initializes a webhook for the specified channel. If a webhook already exists, it is reused; otherwise, a new webhook is created and saved in the database.- Parameters:
message
- The Discord message object. - Returns:
Promise<boolean>
- Resolves totrue
if the webhook was successfully initialized.
Example Usage:
const result = await webhookMessage.init(message); if (result) { console.log('Webhook initialized successfully.'); } else { console.log('Failed to initialize webhook.'); }
- Parameters:
-
send(username, icon, content, embed)
: Sends a message through the initialized webhook.- Parameters:
username
- The name to display as the sender.icon
- The avatar URL for the sender.content
- The message content.embed
(optional) - An array of embeds to include in the message.
- Returns:
Promise<boolean>
- Resolves totrue
if the message was sent successfully, or rejects with an error.
Example Usage:
webhookMessage.send('Bot Name', 'https://example.com/avatar.png', 'Hello, world!', []) .then(() => console.log('Message sent successfully.')) .catch(err => console.error('Failed to send message:', err));
- Parameters:
Structure and commands registration inspired by - TFAGaming DiscordJS-V14-Bot-Template