You are on page 1of 12

WORKSHOP 2

LET'S BUILD A BOT


You will need

• Your botID ( for example 53 )

• You can nd your number on the server under the


section marked

• If you don't have a number contact the tutor

• Pycharm downloaded and open.

• We did tell you this last week

• Other programs are available but you will be responsible


for using them

• Be connected to the channel of the Codewrangling discord


server. Speci cally if your bot number is 53 got to bot 53.

STEP 1

Download bot.py from Week2/workshop folder


fi
fi
STEP 2

move bot.py from your downloads into a folder on your hard drive
( for example UniWork/Programming/Week2/bot.py

STEP 3

Open Pycharm and start a new project. With these settings.

You can call your project BotProject.

Put it next to your code you down loaded.

BASE INTERPRETER: MAKE SURE YOU ARE USING PYTHON 3.7

Wait while PyCharm creates the project ( can take a few mins )
STEP 4

Make sure the project runs by pressing 'the run button'.

STEP 5

IN THE FILE MENUS GO TO 'PREFERENCES OR SETTINGS'

In the new dialog click on 'Python Interpreter'


Click on preferences and click on project:bot2
Then click on interpreter
STEP 6

Click on the + button and then type discord

Click install Package

WAIT! Until you get

STEP 7
Click Open... in the le menu and select 'bot.py' from the folder
you downloaded earlier.

Step 8

Save as bot.py but in your project folder.

You should see something like this


fi
Step 9 only 20% of students are able to this rst time with out
help can you do better?

Change the bot ID to your bot number.

Step 10

Right click on the text area and Press run.

Press run

Step 11
fi
Go to your channel on discord and you should see your bot
like this.

TO make sure this is working do step 12.

STEP 12

Change the text to something unique.


EXERCISE 2

Change the message ( "you said" ) to something unique to you,


something no one else would use. Re run the program and make
sure the bot on discord outputs what you wrote.

def setup():
return "Exercise 2 "

def overheard(message, user):


return " You said " + message + " -> " + user

EXERCISE 3
Write can check your program ins running from your end by using
print( )

Try this out

def setup():
return "Exercise 3 "

def overheard(message, user):


print("I am working" + user + " said " + message )

return " You said " + message + " -> " + user

# END of method overheard


EXERCISE 4
Write this code into you program. You don't have to use "hello" you
can use what ever you like.

If the user says hello then let us respond with 'nice to meet you'
and their name. Try this code.

def setup():
return "Exercise 4 "

def overheard(message, user):


if "hello" in message:
return "Nice to meet you "+ user

return " You said " + message + " -> " + user

# END of method overheard

Run the program and check you have got this on your bot

EXERCISE 5
Let us add a second message - if the user says "where am I"
respond with anything ( your call)

def setup():
return "Exercise 2 "
EXERCISE 6

Now it's your turn add a response to respond to "meme" with "I'm 20 days meme free and
counting"

def setup():
return "Exercise 6 "
Run the program and check you have got this on your bot

EXERCISE 7

Let's respond to hello or "Hello" with "nice to meet you."

def setup():
return "Exercise 7 "

EXERCISE 8
def setup():
return "Exercise 8 "
Your turn respond to "hello" , "Hello" or "Hi" with nice to meet you.

EXERCISE 9

Let's respond to the user if they put the words help AND sh any
where in the message. Try this code.

def setup():
return "Exercise 9 "
fi
Try running the code.

EXERCISE 10
def setup():
return "Exercise 10 "
Your turn write "you know the secret word I must praise you" if the
user says "password" and "xyzzy" any where in their message.

You might also like