Skip to content

Instantly share code, notes, and snippets.

@Yuma-Tsushima07
Last active April 19, 2024 07:18
Show Gist options
  • Save Yuma-Tsushima07/ab520088be145f0e32dae9bf8b104363 to your computer and use it in GitHub Desktop.
Save Yuma-Tsushima07/ab520088be145f0e32dae9bf8b104363 to your computer and use it in GitHub Desktop.
How to Start making a discord bot?

How to start making a discord bot?

Seeking people who made custom discord bots? Liking their really cool commands and have no idea how they did it? Well no need to fear! This simple tutorial will show you how to make a discord bot in no time!

Intro

  • You will need a Code Editor. I use VSC, some people prefer Sublime Text .
  • I will be using Node.js LTS VERSION to run the bot and install the dependencies.
  • Understanding of Javascript. If you don't have that then click --> HERE
  • Understanding of JSON. If you don't have that then click --> HERE
  • A Discord account. If you don't have that then click --> HERE
  • An organised discord server. yes I mean organised because then you will know where to put things Click --> HERE if you need help!

My Source Codes are for reference use ONLY, so you can understand the layout!

Requirements

Go to to discord developers, then create a new application. Then go on oauth and make a link. Go to bot and click on reveal token (don't show anyone this, its like a password). Then paste it in your config.json

Step 1 : Package.json

Package.jsons is a json file where all the dependencies are kept, so the package name, version, folders.

  • Create a package.json file using npm init in your terminal or manually making one.
{
    "name": "Bot",
    "version": "1.0.0",
    "description": "My First Bot Made With Discord.js!",
    "main": "index.js",
    "dependencies": {
        "discord.js": "^11.3.0"
    },
    "author": "Your Nickname Here"
}

Step 2 : Config.json

Config.jsons are used to keep valuable information like tokens, and passwords, ids etc. IMPORTANT TO KEEP THIS SAFE

  • Create a config.json file manually.
{
  "token": "BOT-TOKEN-HERE",
  "prefix": "PREFIX",
  "ownerid": "YOUR-ID-HERE"
}

Step 3 : Index.js

Index.js is the main frame to your bot! You can edit it so it can collect your commands from the commands folder and make it fetch the js only file.

const Discord = require('discord.js'); // Require discord.js for your bot to communicate with the Discord API
const config = require('./config.json'); // Our config file
const client = new Discord.Client();

client.on('ready', () => { // When bot is online and ready
    console.log(`I'm ready!`);
});

client.on("message", message => { // When the bot spots a message
    
    if(message.author.bot) return; // Doesn't respond to bots

    if(message.content.indexOf(config.prefix) !== 0) return; // Only responding to existing commands that start with "!"

    const args = message.content.slice(config.prefix.length).trim().split(/ +/g); // Configure arguments for the commands
    const command = args.shift().toLowerCase(); // Detect existing commands

    //Commands
    if(command === "ping") { // "!ping"
        message.channel.send('Pong!'); // Send a message saying "Pong!"
    }
});

client.login(config.token); // Log in with the bot token from our config file.

Step 4 : Installing Dependencies

Open terminal in VSC or type cmd in search.

VSC Terminal

  • npm init
  • npm i
  • npm i discord.js

CMD

  • Using cd (change directory) navigate to your folder Eg : cd Programming/Bot
  • Then use ls
  • You will see all the files
  • npm init
  • npm i
  • npm i discord.js

Step 5 : Testing

  • Go to your discord server
  • Invite the bot using your invite link that you made
  • Then go back to Cmd or VSC
  • Type node index.js The bot should turn on!
  • Now run PREFIX ping

You have successfully made your own discord bot. From now you can start adding more commands and understanding! Happy Coding!

Author: Yuma-Tsushima

{
"token": "BOT-TOKEN-HERE",
"prefix": "PREFIX",
"ownerid": "YOUR-ID-HERE"
}
const Discord = require('discord.js'); // Require discord.js for your bot to communicate with the Discord API
const config = require('./config.json'); // Our config file
const client = new Discord.Client();
client.on('ready', () => { // When bot is online and ready
console.log(`I'm ready!`);
});
client.on("message", message => { // When the bot spots a message
if(message.author.bot) return; // Doesn't respond to bots
if(message.content.indexOf(config.prefix) !== 0) return; // Only responding to existing commands that start with "!"
const args = message.content.slice(config.prefix.length).trim().split(/ +/g); // Configure arguments for the commands
const command = args.shift().toLowerCase(); // Detect existing commands
//Commands
if(command === "ping") { // "!ping"
message.channel.send('Pong!'); // Send a message saying "Pong!"
}
});
client.login(config.token); // Log in with the bot token from our config file.
{
"name": "Bot",
"version": "1.0.0",
"description": "My First Bot Made With Discord.js!",
"main": "index.js",
"dependencies": {
"discord.js": "^11.3.0"
},
"author": "Your Nickname Here"
}
@ItzDenkiRepo
Copy link

well

@YaBoiEternal
Copy link

what

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment