# 8 Ball

## Preview

You will be learning to use arguments and random choices to make an 8 ball command.&#x20;

![](https://5148195-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MgIjc7Q-SbTUf-t61Rv%2F-Mj68pPlLSCS0u7Q-aoN%2F-Mj6FYwSUTZ0JrwSad2m%2Fimage.png?alt=media\&token=1124b551-3b3e-4a3c-9ace-cb7f905a63ec)

## Coding

### Imports

Before we can start coding this command, we need to import another library. Head to the top of your code, and under your last import, type `import random`. So your import list should look like the following:

```python
import os
import discord
from discord.ext import commands
import random
```

This library will allow us to pick a random item from a list, basically allowing us to pick a random response. This is crucial to making our eightball command.

### Coding - Arguments

We will be following the skeleton we used for the previous command. We just need to change a couple of things to allow us to take in arguments, or information from the sender of the command.

An example of an argument would be in bold in the command "!eightball **will i win the lottery?**". An argument can be a single word, or an entire string. For this case, we will be using an entire string as that is our question.

Now we need to modify our previous template. In `async def eightball(message):` you will need to add a couple more parameters to `eightball` after message. This will be `*` and `arg`. The `*` tells the bot that all words following the command, `!eightball` will belong to the variable called `arg`, which we define right after it.

Because we defined arg as the argument, we can now reference arg to get all of the words following the command `!eightball`. So for example, if I typed `!eightball will i win the lottery?`, if I referenced arg, it would return `will i win the lottery?`.

The modified template is as follows:

```python
@client.command(pass_context=True)
async def eightball(message, *, arg):
  await message.channel.send(response)
```

### Coding - Lists

Now we need to create a list of responses that the eightball can give.

We can create a variable called responses, as they can also store lists. Lists in python are the same as if you were making a list for things you need to buy. How you write them is different, but they do the same thing, grouping a bunch of items together.

To create a list, like stated, create a variable, in this case, lets call it responses. After the equals sign, we can tell python we are creating a list by typing \[]. Anything in between these brackets will be considered part of the list. We can separate items (strings, numbers, etc) in the list with a comma. For example

```python
name = ["Krish", "Rohan", "Shriya"]
```

would be an example of a list of strings. You can also split them up into different lines to make it easier to read. It is best practice to only do this when you have a long list, as short lists can be all contained in a single line.

```python
name = ["Krish",
        "Rohan",
        "Shriya"]
```

Practice making lists by creating a list of eightball responses. Additionally, you can throw in a couple of your own responses if you'd like.&#x20;

### Coding - Random Choice

Now we need to pick a random response and assign it to a variable. Remember how we imported the random library earlier in this section, this is why. We can get a random choice from a list by using random.choice(list).&#x20;

For example, to pick a random name you can use:

```python
name = ["Krish", "Rohan", "Shriya"]
answer = random.choice(name)
```

Now use this to pick a random response from the list of responses you created earlier.

### Coding - Message

Now we need to send a response back to the user. We can do this the same we did for our hello command using await message.channel.send(response). Create your response and add it to your code.

{% hint style="info" %}
Remember the **arg** variable can be used to refer to the question.

Remember how we added non literal text strings from the previous section. Look back if you need to.

You can use multiple `await message.channel.send(response)` in the same command.

If you want to split a message into 2 lines, use \n where you want it to split. For example "hello \nworld" would return:

"hello\
world"
{% endhint %}

{% tabs %}
{% tab title="Instructions" %}
Only select the **Final Code** tab once you have **completed** your eightball command. If you are stuck please see a mentor.&#x20;
{% endtab %}

{% tab title="Final Code" %}

```python
@client.command(pass_context=True)
async def eightball(message, *, arg):
  responses = ["Response 1",
  "Response 2",
  "Response 3"]

  answer = random.choice(responses)

  await message.channel.send(f"You asked: {arg}\nMy response: {answer}")
```

{% endtab %}
{% endtabs %}

## Code Checkup

Your code should look something like the following at this point in the sushi card:

{% tabs %}
{% tab title="Instructions" %}
Only select the **Final Code** tab once you have **completed** your eightball command. If you are stuck please see a mentor.&#x20;
{% endtab %}

{% tab title="Final Code" %}

```python
import os
import discord
from discord.ext import commands
import random

token = os.environ['token']

client = commands.Bot(command_prefix = "!", case_insensitive=True)

@client.event
async def on_ready():
    print("The Discord bot is up!")

@client.command(pass_context=True)
async def hello(message):
  await message.channel.send(f"Hello {message.author.name}")

@client.command(pass_context=True)
async def eightball(message, *, arg):
  responses = ["Response 1",
  "Response 2",
  "Response 3"]

  answer = random.choice(responses)

  await message.channel.send(f"You asked: {arg}\nMy response: {answer}")


client.run(token)
```

{% endtab %}
{% endtabs %}

If your code is not like this or is not working, please either read through the sushi card again OR come talk to one of the mentors who will guide you through the issue.
