Code a Discord Bot with Python Course Help

I was following the Discord Bot course and I came up with this error

and here is my whole code

import os
import discord
import requests
import json
import random
from replit import db


client = discord.Client()

sad_words = ["sad", "depressed", "unhappy", "angry", "miserable", "depressing"]

starter_encouragements = [
  "Cheer Up!",
  "Hang in there",
  "You are a great person!"
]

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

def update_encouragements(encouraging_message):
  if "encouragements" in db.keys():
    encouragements = db["encouragements"]
    encouragements.append(encouraging_message)
    db["encouragements"] = encouragements
  else:
    db["encouragements"] = [encouraging_message]

def delete_encouragement(index):
  encouragements = db["encouragements"]
  if len (encouragements) > index:
    del encouragements[index]
    db["encouragements"] = encouragements

@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

    msg = message.content

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

    options = starter_encouragements
    if "encouragements" in db.keys():
      options = options + db["encouragements"]

    if any(word in msg for word in sad_words):
      await message.channel.send(random.choice(options))

    if msg.startswith("$add"):
      encouraging_message = msg.split("$add ",1)[1]
      update_encouragements(encouraging_message)
      await message.channel.send("New encouraging message added! :white_check_mark:")

    if msg.startswith("$del"):
      encouragements = []
      if "encouragements" in db.keys():
        index = int(msg.split("$del",1)[1])
        delete_encouragement(index)
        encouragements = db["encouragements"]
      await message.channel.send(encouragements)

my_secret = os.environ['TOKEN']
client.run(my_secret)

if it doesn’t work with + you will need to use a different way to concatenate lists

for example like you used here

1 Like

am kinda new to this, so how exactly will the code look like if I use the method you suggested right now?

It should look something like this

1 Like

Thank You, That worked!

1 Like

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