Requirements
- Add
discord.py to your requirements.txt and use pip to add it to your packages (See task 1)
- Use the following code to create a simple Ping Pong command
import discord
client = discord.Client()
@client.event
async def on_ready():
print(f”Bot user {client.user} is ready.”)
@client.event
async def on_message(msg):
if msg.author == client.user:
return # Don’t respond to itself
if msg.content == “Ping”: # Check that the message content matches
await msg.channel.send(“Pong!”) # If it does, reply
client.run(TOKEN) #insert token here
- Try it out by messaging
Ping on your bot dev channel
Security:
- Add
python-dotenv to requirements.txt
- Place your discord bot token into a
.env file and import it into your program as an environment variable (see KoalaBot.py for an example of how to do it
Extension: virtual environment
- Instead of installing pip packages to your global python install, create a virtual environment in a
venv folder, activate it, and then install all pip requirements within your new virtual environment
- This allows any issues with one virtual environment not to affect any others
Requirements
discord.pyto yourrequirements.txtand use pip to add it to your packages (See task 1)Pingon your bot dev channelSecurity:
python-dotenvtorequirements.txt.envfile and import it into your program as an environment variable (see KoalaBot.py for an example of how to do itExtension: virtual environment
venvfolder, activate it, and then install all pip requirements within your new virtual environment