Guide to getting started with discord.py
- to learn about enhanced-discord.py, you must know the basics of python, this includes if statements, dicts, tuples, lists, loops, and all the other basics.
# Linux/macOS
python3 -m pip install -U enhanced-dpy
# Windows
py -3 -m pip install -U enhanced-dpy
# Importing discord
import discord
# Importing commands.
from discord.ext import commands
# Making the bot instance.
bot = commands.Bot(command_prefix="$")
# Making a command.
@bot.command() # Use the name keyword to give it a name, this will default to coro.__name__ (Your function's name)
async def ping(ctx):
await ctx.send("pong")
# Run the bot using it's token.
bot.run("token")
- So yes this is a bot, but with many issues,
- the ping command won't actually tell the propery latency.
- There is nothing printed so we do not know when the bot starts.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="$")
@bot.event
async def on_ready():
print("I'm alive!")
@bot.command()
async def ping(ctx):
await ctx.send(f"pong: {round(bot.latency * 1000, 2)} ms")
bot.run("token")
- What if you wanted to respond to a message without the prefix? Well that is possible!
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="$")
@bot.event
async def on_ready():
print("I'm alive!")
@bot.event
async def on_message(message):
if message.content == "hello bot":
await message.channel.send("Hey I'm am a bot that is alive!")
await bot.process_commands(message) # This method is required or you commands will not work!
# Read more about it: https://enhanced-dpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Bot.process_commands
@bot.command()
async def ping(ctx):
await ctx.send(f"pong: {round(bot.latency * 1000, 2)} ms")
bot.run("token")
- Do not leave your token in the middle of your main file, put it in a place that you know you will not have to show.
- Simple way is by using a .txt file, lets get started, make a file called
token.txt
and put your token in it. - Now before your bot.run put this
bot_token = open("token.txt", "r").read()
- In your bot.run do
bot.run(bot_token)
- Boom, You have secured your token.
- You can learn about all of these in the offcial documentaion by clicking here
- More examples about other things you can do in enhanced-dpy
- There are many things to secure apart from the token, learn about them here
POGGGGGGGGGGGGGGG