Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sachinl0har/discord.js-v12-bot-tutorial

An Discord bot build in Discord.js V12. Features :- Anime, Chatbot, Fun, Info, Moderation, Music, Developer commands and many more coming soon.
https://github.com/sachinl0har/discord.js-v12-bot-tutorial

discord discord-api discord-js discordapp nodejs

Last synced: 3 months ago
JSON representation

An Discord bot build in Discord.js V12. Features :- Anime, Chatbot, Fun, Info, Moderation, Music, Developer commands and many more coming soon.

Awesome Lists containing this project

README

        

![Discord.js-V12-Bot-Tutorial](https://socialify.git.ci/sachinl0har/Discord.js-V12-Bot-Tutorial/image?description=1&font=Source%20Code%20Pro&forks=1&issues=1&language=1&owner=1&pattern=Plus&pulls=1&stargazers=1&theme=Dark)
# Discord.js-Tutorial

Here is a Discord.js Bot Tutorial.
![forthebadge](https://forthebadge.com/images/badges/made-with-javascript.svg)

## Getting Started

For Getting Started you should atleast have a basic knowledge of JavaScript. Basic Knowledge means you should know the basic functions and Syntax's.

### Prerequisites and Installing Softwares

First you need to download the latest verison of [Node.js](https://nodejs.org/en/) after installing, if you want to check if node.js is installed in your computer or not. go to the search bar and type `CMD` open it and type `node --version` in the terminal it should show you the latest verison which you installed.
Second you need a code editor i recommend you [Visual Studio Code](https://code.visualstudio.com/download), Then after installation open Visual Studio Code and go to Extensions and download the extension of JavaScript.

### Create Discord Application

Go to [Discord Developer Portal](https://discord.com/developers/applications) And click "New Application" And then name your application then click "Create" .Now , Head over to Bot. Click Add Bot, then you customize the name and avatar(If youd like). Now lets get coding.

### Coding

Make a folder where you want to store all your bot files. Open CMD with that Folder location and type `npm init` This will create a `package.json` file in your directory.Then open Visual Studio Code with that Directory.then make a file with your main file name, in my case its `alpha.js` then type this code
```js
// IMPORT MAIN FILES AND PACKAGES WHICH IS REQUIRED.
const Discord = require('discord.js');
const config = require('./configs/config.json'); // AS YOU SEE YOU HAVE TO MAKE A FOLDER NAMED `configs` AND IN THAT FOLDER MAKE `config.json` FILE. CODE WILL BE GIVEN ABOVE.
const fs = require('fs');
const db = require('quick.db');
const path = require("path");
const client = new Discord.Client();
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
const { Player } = require('discord-player');
const player = new Player(client);
client.player = player;
client.emotes = require('./configs/emotes.json')
client.filters = require('./configs/filters.json');
["aliases", "commands"].forEach(cmd => client[cmd] = new Discord.Collection());
["console", "command", "event"].forEach(events => require(`./handlers/${events}`)(client));
client.categories = fs.readdirSync('./commands');
// EVENTS
client.on('ready', async() => {
console.log('Alpha Started!');
});
client.login(config.token)
```

Config File
```js
{
"prefix": "your prefix here",
"dev": "736977089307345004", // DEVELOEPRS ID HERE
"token": "bot token here"
}
```

### Command Handler
command.js // READ FILES INSIDE COMMANDS FOLDER
```js
const fs = require('fs');
module.exports = (client) => {
const load = dirs => {
const commands = fs.readdirSync(`./commands/${dirs}/`).filter(cmd => cmd.endsWith('.js'));
for (let cmd of commands) {
let pull = require(`../commands/${dirs}/${cmd}`);
client.commands.set(pull.config.name, pull);
if (pull.config.aliases) pull.config.aliases.forEach(cmd => client.aliases.set(cmd, pull.config.name));
};
};
["Folder Names Inside Commands folder"].forEach(cmd => load(cmd));
};
```

### Event Handler
event.js // READ EVENT FILES
```js
const fs = require('fs');
module.exports = (client) => {
const load = dirs => {
const events = fs.readdirSync(`./events/${dirs}`).filter(events => events.endsWith('.js'));
for (let file of events) {
const events = require(`../events/${dirs}/${file}`);
let eventName = file.split('.')[0];
client.on(eventName, events.bind(null,client));
};
};
['client', 'guild'].forEach(events => load(events));
};
```

### Bot Status and Activity
```js
let totalUsers = client.guilds.cache.reduce((users , value) => users + value.memberCount, 0);
let totalGuilds = client.guilds.cache.size
let totalChannels = client.channels.cache.size
client.user.setPresence({ activity: { name: `${totalUsers} Users | ${totalGuilds} Servers | ${totalChannels} Channels`, type: "PLAYING" }, status: "online" });
```

### Command Structure
```js
const Discord = require('discord.js');
const config = require('../../configs/config.json');
module.exports = {
config: {
name: '',
description: '',
aliases: [""],
usage: '',
accessableby: "",
},
run: async (client, message, args) => {
// PROCESSING CODE HERE
}
}
```

### ChatBot Feature For a Specific Channel
```
var chatbot = new alexa("chat bot api"); //YOUR CHATBOT API HERE
```

```js
client.on("message", async message => {
let sChannel = db.fetch(`chatbot_${message.guild.id}`);
if (sChannel === null) {
return;
}
if (message.author.bot && message.author.discriminator !== '0000') return;
if(message.channel.id === sChannel){
let content = message.content;
if(!content) return;
chatbot.getReply(content).then(r => client.channels.cache.get(sChannel).send(r));
}
});
```

## More Advanced commands will be added soon stay tunned and follow. And if this was helpfull i will be glad if you give it a Star.