Discord is a Python bot

Hello everyone, today we will write a Discord bot in Python and discord.py + as a bonus, let’s look at examples of bots. Let’s get started 🙂

Before work

Before you start, you need:

  1. Python 3;
  2. discord.py;
  3. Discord-account and your server.

To install discord.py use the package manager:

pip3 install discord.py

Creating our bot

Go to the Developer Portal and click on New application.

You have created your application, on the application page, go to Bot >> Add Bot and create your own Discord bot.

Save the bot token! Then we will need it!

If everything went well, congratulations, half the job is done 😀

Adding a bot to the server

Now you can add a bot to the server.

Go to OAuth2 >> URL Generator, in Scopes select Bot and below — bot rights, copy the generated URL. We insert it into the browser, and add it to our server.

Echo Bot

Let’s write a traditional echo bot, and analyze each line of code.

Code:

import discord
from discord.ext import commands

config = {
    'token': 'your-token',
    'prefix': 'prefix',
}

bot = commands.Bot(command_prefix=config['prefix'])

@bot.event
async def on_message(ctx):
    if ctx.author != bot.user:
        await ctx.reply(ctx.content)

bot.run(config['token'])

Example of work:

Analysis:

import discord
from discord.ext import commands

The imports we need.

config = {
    'token': 'your-token',
    'prefix': 'prefix',
}

The config auxiliary dictionary in which we store the token and the command prefix (I’ll tell you later why the command prefix is needed).

bot = commands.Bot(command_prefix=config['prefix'])

Creating our bot, we pass the prefix in the argument.

@bot.event

A decorator designed to handle events, read more here.

async def on_message(ctx):

Creating an asynchronous function, with the ctx parameter, is a message.

if ctx.author != bot.user:

Checking whether the author of the message is our Discord bot. The fact is that if the bot sends a message, it will be a new event, and then a cycle is obtained.

await ctx.reply(ctx.content)

We reply to the message (ctx.reply), we pass the message (ctx.content) to the arguments.

bot.run(config['token'])

We launch our bot, we pass the bot token to the arguments.

I hope you have figured out the code, and we can move on.

Command processing

Before processing commands, we will need our prefix.

Consider the code:

import random
import discord
from discord.ext import commands

config = {
    'token': 'your-token',
    'prefix': '$',
}

bot = commands.Bot(command_prefix=config['prefix'])

@bot.command()
async def rand(ctx, *arg):
    await ctx.reply(random.randint(0, 100))

bot.run(config['token'])

The result of the work:

Analysis:

@bot.command()

Command Processing Decorator

async def rand(ctx, *arg):

Asynchronous rand function

await ctx.reply(random.randint(0, 100))

We respond to the message, we pass a random number from 0 to 100 to the arguments

Bonus

Role verification:

import random
import discord
from discord.ext import commands

config = {
    'token': 'your-token',
    'prefix': '$',
}

bot = commands.Bot(command_prefix=config['prefix'])

@bot.command()
@commands.has_role("Хозяин")
async def rand(ctx, *arg):
    await ctx.reply(random.randint(0, 100))

bot.run(config['token'])

Expel the user

import discord
from discord.ext import commands

config = {
    'token': 'your-token',
    'prefix': '$',
}

bot = commands.Bot(command_prefix=config['prefix'])

@bot.command()
async def kick(ctx, user : discord.User(), *arg, reason='Причина не указана'):
    await bot.kick(user)
    await ctx.send('Пользователь {user.name} был изгнан по причине "{reason}"')

bot.run(config['token'])

Outsourcing Software Development Services | Dedicated Solutions Outsourcing

Ready to see us in action:

More To Explore

IWanta.tech
Logo
Enable registration in settings - general
Have any project in mind?

Contact us:

small_c_popup.png