How to Make a Discord Bot Using Py

by maksym_kocherev in Circuits > Robots

312 Views, 1 Favorites, 0 Comments

How to Make a Discord Bot Using Py

A927920F-2DB1-432A-8305-148B18728B9C.jpeg

Today we are going to do a bot that can respond to our massages using py

Discord Developers Portal

BAEDF5E5-818A-4F1A-87DC-D75755A9110D.jpeg

We are going to go to discord developers portal.

Creation of Application

46B9B550-2CA2-436B-A4F9-16AF2F9FAA92.jpeg

When logged in go to applications and create a new application. Name your bot with name that you picked.

Creation of a Bot

4FD7CE9F-4067-4F68-89A8-98F4780CF264.jpeg

When we created an application we can go to BOT and press a button that says Add bot.

Adding Bot to the Server

638FC623-257A-4DC3-AA99-692837ECEA49.jpeg
IMG_0963.jpeg

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

2C7F744C-DBED-42F7-87CC-93D4552D770D.jpeg
5AD481B1-C231-4F4D-A19C-96679453C969.jpeg

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

40B0715E-20E3-45B4-85C9-03CE7BBE944B.jpeg

When we opened creat repl bar we pick Node.js as our language and name it test or with your bot name.

Coding

42507D22-7AF7-46D2-8FA4-7D7473A26A59.jpeg

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

212C4A2A-C342-4309-97A9-0CC80ED481B3.jpeg
EAD0CC5C-A085-470A-AD26-1AAAF26E7CAA.jpeg

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

E2BC6135-87B8-45D8-A295-DF421DA20F4C.jpeg
EAAB6AC5-39A9-46C7-B6B7-1DAFE86D8FD4.jpeg

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

CFA4D6A5-CB4A-4AF5-96E6-7F340C24261E.jpeg

Try saying ping and your bot is going to reply with pong in 1 second.