Welcome everyone to a DiscordPy tutorial. DiscordPy is a Python package aimed at helping you to make discord bots. For example, I would like to make a bot that searches PythonProgramming.net for tutorials on topics people ask about, along with providing server information, handling roles, and other general tasks I often find myself doing on Discord.
To begin, we're going to need DiscordPy!
pip install discordpy
Next, I found the documentation to be problematic, not yet fully updated it seemed. I found the best place to figure out what's available is just the API documentation here: https://discordpy.readthedocs.io/en/rewrite/api.html#
Great, once you have DiscordPy, you need a bot. To have a bot, you need an app. For an app, you'll need a server. If you have none of this, make a server first. Next, we need to head to: https://discordapp.com/developers/applications/
Log in if you arent yet, then choose to create a new application.
Pick a name, take note of your Client ID
, you will need this later.
Move down to the Bot
choice under the application settings, and choose add bot
.
Choose the settings you want. You probably at least want to be able to:
Once you've done this, you'll see a permissions integer
at the bottom, take note of this too.
Now, you're ready to invite your bot to your server. You can do this with:
https://discordapp.com/oauth2/authorize?client_id={CLIENTID}&scope=bot&permissions={PERMISSIONINT}
...replacing {CLIENTID}
with the client id you took note of earlier and {PERMISSIONINT}
with the permissions integer we just noted.
Visit that link and approve your bot.
Now, let's write the code for the bot!
import discord
#print(discord.__version__) # check to make sure at least once you're on the right version!
token = open("token.txt", "r").read() # I've opted to just save my token to a text file.
client = discord.Client() # starts the discord client.
@client.event # event decorator/wrapper. More on decorators here: https://pythonprogramming.net/decorators-intermediate-python-tutorial/
async def on_ready(): # method expected by client. This runs once when connected
print(f'We have logged in as {client.user}') # notification of login.
@client.event
async def on_message(message): # event that happens per any message.
# each message has a bunch of attributes. Here are a few.
# check out more by print(dir(message)) for example.
print(f"{message.channel}: {message.author}: {message.author.name}: {message.content}")
client.run(token) # recall my token was saved!
We have logged in as Sentdebot#2306
voice-channel-text: Wilfred#1207: Wilfred: 2/2 = 1.0 WHY
__init__: Sentdex#7777: Sentdex: He's back now
That's all there is to it! Now, you can add some logic, like having it respond to messages, like:
@client.event
async def on_message(message): # event that happens per any message.
print(f"{message.channel}: {message.author}: {message.author.name}: {message.content}")
if str(message.author) == "Sentdex#7777" and "hello" in message.content.lower():
await message.channel.send('Hi!')
Now, if you say "Hello" in some channel, your bot will reply with "Hi!"
Okay, so that's it for a basic introduction, but there's a LOT you can do with this. In the coming videos, I will cover my own creation of a bot, but this will be only a tiny slice of what's available to you, and I highly suggest you browse the documentation to see everything you can do. For example, sometimes we like to change eachother's nicknames in Discord. My first bot would change users' nicknames, and then change them again if the user tried to change it again, for example. I am a great host.