How to Make a Discord Bot Using Py
by maksym_kocherev in Circuits > Robots
328 Views, 2 Favorites, 0 Comments
How to Make a Discord Bot Using Py
Today we are going to do a bot that can respond to our massages using py
Discord Developers Portal
We are going to go to discord developers portal.
Creation of Application
When logged in go to applications and create a new application. Name your bot with name that you picked.
Creation of a Bot
When we created an application we can go to BOT and press a button that says Add bot.
Adding Bot to the Server
We are going to go to OAuth2 from all words listed we are going to click bot. Then scroll down and pick send massages. You are going to get custom link to add a bot to your server. Click it and authorize then pick a server you want to add it in.
Creating Replit.com Account
Go to replit.com and create a new account. When you created a new account click creat new repl in top right corner.
Creating Repl
When we opened creat repl bar we pick Node.js as our language and name it test or with your bot name.
Coding
We are going to start coding and with importing discord.js and saying that discord client = const bot. Then we are going to write that if bot goes online on a log tab we are going to see that it goes online. Code below
require('dotenv').config();
const Discord = require('discord.js'); const bot = new Discord.Client();
bot.on('ready', () => { console.info(`Logged in as ${bot.user.tag}!`); })
Finishing the Code
We are going to write that if from new Discord client we receive massage “ping” it responds with “pong” And we are adding .env file that nobody sees in this file we have bot token (don’t show your token to anyone) to copy this token go to discord developers portal to bot tab than copy your token and go to replit.com and on left you can see lock picture press it and it opens a tab with your .env things add a new env called “TOKEN” (don’t rename it or script won’t work) and paste you token to token equal to (token) Then scroll down and press insert button. Code below
require('dotenv').config();
const Discord = require('discord.js'); const bot = new Discord.Client();
const TOKEN = process.env.TOKEN;
bot.on('ready', () => { console.info(`Logged in as ${bot.user.tag}!`); })
bot.on('message', msg => { if (msg.content === 'ping') { msg.reply('pong'); msg.channel.send('pong'); } })
bot.login(TOKEN);
Running the Bot
Press run button and you can see in log that our bot working and now it’s online. If you have done something wrong it’s going to show every error in logs go back and read steps one more time.
Trying the Command
Try saying ping and your bot is going to reply with pong in 1 second.