TypeError but I did everything like in the Tutorial :D

Im Talking about this Vid: Code a Discord Bot with Python - Host for Free in the Cloud - YouTube
And yeah…after trying to add a second encouragement this appears in the Console constantly. Even if i write “sad” so that the Bot should trigger.

The Error:
Ignoring exception in on_message
Traceback (most recent call last):
File “…”, line 34 3, in _run_event
await coro(*args, **kwargs)
File “main.py”, line 56, in _on_message
options = options + db[“encouragements”]
TypeError: can only concatenate list (not “ObservedList”) to list

tbh I didn´t get why the > thing is in line 35, maybe someone can help me explaining it.
Thanks for help :smiley:

Please provide a link to your code, so we actually know what you wrote in line 35.

1 Like
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!"

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']

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

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

async def on_ready():
  print('Wir sind eingeloggt als {0.user}'.format(client))

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

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

  if msg.startswith("!new"):
    encouraging_message = msg.split("!new ",1) [1]
    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])
      encouragements = db["encouragements"]
    await message.channel.send(encouragements)


I didn’t include it in the message because I thought I did everything as in the video. Sorry.

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 (’).

I’m sorry, thanks for your help!

The thing is, nobody here has memorized the video or the code used there.

Also the problem is NOT in line 35, but in line 56.
Because you use imported libraries, the error message will usually point to several points where the error is happening because it’s happening in the code within the imported library. So it points there, but also to where the error actually occured and with several connected libraries the message can end up pointing to half a dozen different lines → so you need to look at entire message focusing on File “main.py” or however you main file is called.

Now with that being said, the error also points to options = options + db[“encouragements”].
So that’s the code where the error is happening and this might be line 56. This can be very confusing when starting out but it’s actually super helpful as it gives insight in where the error is happening in different files/libraries. So if you would have happened to write those yourself or in a team, you can track down all the connected files with the error-message directly.

Now given db is an imported object, it’s values might be a special kind of list, hence addition doesn’t work. You might be able to .append() the list instead of using “+” or might need to turn db["…"] into a list.
Maybe print the value of db and check the type for further info.

I used the .append keyword with options but it didn’t work.
Adding .value to the same statement of options = options + db[“encouragements”] worked for me.

Modify the line to be
options = options + db[“encouragements”].value

To understand why .value, print both ‘options’ and ‘db[“encouragements”]’ in the for loop. Through this you will be able to see that db[“encouragements”] is like a dictionary which has value=["//Your message"]

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