Skip to content

Instantly share code, notes, and snippets.

@SawshaDev
Last active August 1, 2022 22:41
Show Gist options
  • Save SawshaDev/0abbb3e066e7c48367b3f180902b99ae to your computer and use it in GitHub Desktop.
Save SawshaDev/0abbb3e066e7c48367b3f180902b99ae to your computer and use it in GitHub Desktop.
How to make a discord bot using discord.py!

Making a discord bot using discord.py Making a discord bot is no trivial task and requires basic python knowledge and requires you to know inner and outer scopes, variable, functions, asyncio, and plenty more.

if you know all of these python concepts, i will now start the tutorial!

For starters, you first need to install discord.py you could install it by running the following console command: python3 -m pip install -U discord.py which would install it.

once you have it installed, create a new file named main.py, DO NOT NAME YOUR FILE discord.py OR ELSE THE DISCORD MODULE WILL NOT WORK.

assuming you already made a bot application in the developer's dashboard, you make go on with this tutorial. if not, please do create a bot application in the dashboard.

in your main.py file, you can add the following imports

# main.py
import discord
from discord.ext import commands

the first import, imports the whole library which means you can access stuff via like discord.ext.commands but i dont recommend that as we've already import ext-commands.

the second import is just the commands extension of discord.py which allows for bot commands and plenty other things.

Next, you can define a bot constructor with either discord.Client or commands.Bot. commands.Bot is a subclass of discord.Client but with added support for the builtin commands handler i.e ext-commands and support for much more. i will only provide examples for commands.Bot

your constructor SHOULD look like this client = discord.Client() this is for if you're using discord.Client. this is just a plain client constructor with nothing, no intents, nothing.

bot = commands.Bot(command_prefix="!") this is what it should look like if you're using commands.Bot (Note: you MUST provide command_prefix, but you may change it to whatever you want instead of "!".

in all your code should look like this right now

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix=True)


bot.run("Yourtokenyougotfromthedashboard")

this example is for commands.Bot. this is what your code should look like in total. Make sure you have the last line or else it will NOT run.

if you want to define any intents such as the 3 privileged intents provided in the dashboard + all other intents, you may do this intents = discord.Intents.all() and then you could pass this variable into your bot constructor using the intents kwarg (Keyword only arg) i.e commands.Bot(command_prefix="!", intents=intents)

you may also provide specific intents such as the following https://discordpy.readthedocs.io/en/stable/api.html?highlight=discord%20intents#discord.Intents and you could add them using the following two ways

First way is make the discord.Intents a method and adding each intent individually inside the function like so

intents = discord.Intents(guilds=True, messages=True, members=True) and so on. or on the other hand you could just add them line after line like so

intents = discord.Intents()
intents.messages = True
intents.guilds = True
intents.member = True

but in the end, neither of these are needed. you could just do discord.Intents.default() which turns on the default intents.

Next, we'll cover how to make commands.

You may make a prefix command using the following decorator, @bot.command() which just defines a command. usually a bot.command decorator gets followed by an async function that always has ctx passed in which the library detects as Context or commands.Context.

right now you should have this

Note: whatever you name the function, will be the name of the command. so take this into account.

import discord
from discord.ext import commands

intents = discord.Intents.default() # Provides the default intents needed for the bot

bot = commands.Bot(command_prefix="!", intents=intents) # Your bot constructor that everything revolves around!

@bot.command()
async def ping(ctx):
    pass

bot.run("Yourtokenyougotfromthedashboard")

for now this code does not do anything and the command will not run

what you can do to make it reply to the command being invoked like !ping

in your ping function or whatever else you name it, you can add the following

await ctx.send("Pong!") which sends a message. make sure you await it because abc.Messageable.send which ctx.send inherits from is async and requires you to await it.

Your code in total should all look like this

import discord # Basic imports such for everything needed
from discord.ext import commands

intents = discord.Intents.default() # Provides the default intents needed for the bot

bot = commands.Bot(command_prefix="!", intents=intents) # Your bot constructor that everything revolves around!

@bot.command() # Defines the following function as an command and *makes* it a command
async def ping(ctx): # Just the function with the ctx paramater which is short for ``Context`` which the library handles
    await ctx.send("Pong!") # Sends "Pong" to the channel the command was invoked in

bot.run("Yourtokenyougotfromthedashboard") # Runs the bot with your bot token.

This all provides how to use discord.py, how to make a basic bot, and how to make a command! if you find any typos or issues, please make a comment about it!

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