Discord Bot code Type Error

I keep on getting a TypeError when I try to add in a new encouraging message. I was following this video. Code a Discord Bot with Python - Host for Free in the Cloud - YouTube

Console
Ignoring exception in on_message
Traceback (most recent call last):
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py”, line 343, in _run_event
await coro(*args, **kwargs)
File “main.py”, line 59, in on_message
options = options + db[“encouragements”]
TypeError: can only concatenate list (not “ObservedList”) to list
Ignoring exception in on_message
Traceback (most recent call last):
File “/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py”, line 343, in _run_event
await coro(*args, **kwargs)
File “main.py”, line 59, in on_message
options = options + db[“encouragements”]
TypeError: can only concatenate list (not “ObservedList”) to list

Code so far:

import discord
import os
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 / bot!"
]

if "responding" not in db.keys():
  db["responding"] = True

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_encouragment(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)

  if db["responding"]:
    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("$new"):
    encouraging_message = msg.split("$new ",1)[1]
    update_encouragements(encouraging_message)
    await message.channel.send("New encouraging message added.")

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

  if msg.startswith("$list"):
    encouragements = []
    if "encouragements" in db.keys():
      encouragements = db["encouragements"]
    await message.channel.send(encouragements)

  if msg.startswith("$responding"):
    value = msg.split("$responding ",1)[1]

    if value.lower() == "true":
      db["responding"] = True
      await message.channel.send("Responding is on.")
    else:
      db["responding"] = False
      await message.channel.send("Responding is off.")

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

Hello. I tried to replicate the code from the “Code a Discord Bot with Python” video but when I run it and try to add a $new encouraging word in I keep on getting a TypeError on line 59 of the code “options = options + db[“encouragements”] saying that it can only concatenate list (not"ObservedList”) to list. How can I solve this?

Hey, I was watching the same video today and experienced the same error. I’ll let you know if I manage to fix it.

Try using one of the other methods of list concatenation. The following worked for me -

      options.extend(db["encouragements"])

6 Likes

encouragements.appen(encouraging_message) is now giving an error. can you kindly help?

This Really Helped. Thanks

Where exactly do we add that in? When I tried that, it still seemed to hate the line

options = options + db[“encouragements”]

Guys, the following line helped me

base = base + list(db['YOUR KEY FROM DB'])
2 Likes
invalid literal for int() with base 10: ' hi'   

I keep getting this error after running $del hi in discord

Hi, there. I have also been watching the tutorial video on how to create a bot. I have received a similar error. I have copied the code exactly from the video but somehow it always shows this as an error:
update_encouragements(encouraging_message)

At first I thought it was because I had misspelled ‘encouragement’ a couple of times but after fixing that, the problem was still there.

Could anyone help me fix it?

thanks it worked…but soon later it gave this error UnboundLocalError: local variable ‘encouragements’ referenced before assignment and later the $del function didnt work.

Guys if you are getting the same error use this below code.
options = options + list(db[‘encouragements’])
it worked for me.

What it is printing–>
ObservedList(value=[‘omg’])

this helped me, thank you!

thankyou sir u helped me alot by this comment

@pranabsaha1130 Its Not Appen Its Append, I made this Mistake Too. :wink:

I found The Solution You can Use

options = options.extend(db[“encouragements”])