Skip to content

Instantly share code, notes, and snippets.

@Sengolda
Last active October 1, 2021 14:56
Show Gist options
  • Save Sengolda/a72268a3c4a6f68380dcb7dfc84f44dd to your computer and use it in GitHub Desktop.
Save Sengolda/a72268a3c4a6f68380dcb7dfc84f44dd to your computer and use it in GitHub Desktop.
A Guide for enhanced-dpy.

enhanced-discord.py guide

Guide to getting started with discord.py

things to note

  • 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.

If you do know basics, We can get to the basics of discord.py

Install

# Linux/macOS
python3 -m pip install -U enhanced-dpy

# Windows
py -3 -m pip install -U enhanced-dpy

A Minimal Bot (With Explanation of syntax)

# 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.

Solving the issues above.

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")

Events

  • 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")

Securing 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.

Guide to Securing token.

  • 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.

That is it for this guide! Hope you learned something!

of course there is much more to enhanced-dpy then what is listed here.

  • 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

Securing your bot

  • There are many things to secure apart from the token, learn about them here
@ExpertTutorials
Copy link

POGGGGGGGGGGGGGGG

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