Discord Bot not working

I followed the FreeCodeCamp Code a Discord Bot with Python but the bot doesn’t seem to be working and as a beginner I didn’t understand about the API and what to do if we want to use something else like facts.

MY CODE :arrow_down:

import os
import discord
import requests
import json

client = discord.Client()

def get_quote():
  responce = requests.get ("https://zenquotes.io/api/random")
  json_data = json.load(responce.text)
  quote = json_data[0]["q"] + " -" + json_data[0]["a"]
  return(quote)

@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
   return

  if message.content.startswith('$quote'):
    quote = get_quote()
    await message.channel.send(quote)

client.run(os.getenv('TOKEN'))

And if we want the bot to respond to some other command do we make a new file for it because it doesn’t seem to work in the same file for me.
@jwilkins.oboe Can you help me out?

Looking at the final result, beau only used 2 files.
Most of the content is in the main file.

What error messages are you getting?

I get this-
image
@jwilkins.oboe

It should be json.loads not json.load.

It’s small, but you probably meant response instead of responce. You wrote that twice.

Once you fix those spelling errors it should be this.
json_data = json.loads(response.text)

I might be missing something else since it is late here :smile:
But that should help.

I am logging off for the night but if there are other errors then others on the forum will help you out too.

ok thanks ill try to do the changes and check again. gn :crescent_moon:

once you fix the errors you just need to expand this

here message.content is the content of the message, you check it for the command with an other if statement, and then you make the bot answer with

   await message.channel.send(...)

what should we name the second/third file?
and my code still seems not to work

import os
import discord
import requests
import json
import random

client = discord.Client()

greeting_words = ["Hi", "Hola", "Geia", "Kon'nichiwa", "Namaste"]

greeting2_words = ["Hi", "Hola", "Geia", "Kon'nichiwa", "Namaste"]


def get_quote():
  responce = requests.get ("https://zenquotes.io/api/random")
  json_data = json.loads(responce.text)
  quote = json_data[0]["q"] + " -" + json_data[0]["a"]
  return(quote)

@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
   return

  if msg.startswith('$quote'):
    quote = get_quote()
    await message.channel.send(quote)

  if any (word in msg for word in (greeting_words)
    await message.channel.send(random.choice(greeting2_words))

client.run(os.getenv('TOKEN'))

@jwilkins.oboe can u chek this too pls

What did beau name it in the tutorial?
What error messages are you getting?


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

image
The error

You are missing the colon at the end of your if statement.

But also, why do you have two arrays that have the exact same elements?

Are you still building out one of the arrays so they will be different somehow?

No i wat it to be same and where is the colon, i dont see it

If statements in python need colons.
https://www.programiz.com/python-programming/if-elif-else

You don’t have one here

where do i put it?
at the end of the line?

Yes.

Just like you did the other if statements earlier.

If you are new to python, it might be good to review some of the basics.
It will make building out this discord easier to understand. :grinning:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.