Skip to content

Task 3: Creating a discord.py bare bones basic bot #3

@JayDwee

Description

@JayDwee

Requirements

  1. Add discord.py to your requirements.txt and use pip to add it to your packages (See task 1)
  2. Use the following code to create a simple Ping Pong command
import discord
client = discord.Client()

@client.event
async def on_ready():
    print(fBot 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
  1. 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 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions