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
3. Try it out by messaging `Ping` on your bot dev channel
## Security:
1. Add `python-dotenv` to `requirements.txt`
2. Place your discord bot token into a `.env` file and import it into your program as an environment variable (see [KoalaBot.py](https://github.com/KoalaBotUK/KoalaBot/blob/master/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.py
to yourrequirements.txt
and use pip to add it to your packages (See task 1)@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