Discord Bot Code Type Error (Python)

I am attempting to follow a video on creating an Encouraging Discord bot in Python but have run into an error. Whenever I type a ‘sad word’ from a list inside the script, i comes up with this error in the 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

Here is the code:

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

client = discord.Client()

sad_words = ["sad", "depressed", "unhappy", "angry", "depressing", "die", "died"]

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(encourage_message):
  if "encouragements" in db.keys():
    encouragements = db["encouragements"]
    encouragements.append(encourage_message)
    db["encouragements"] = encouragements
  else:
    db["encouragements"] = [encourage_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)

  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_encouragement(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'))

Could anyone help?

I think this is an issue with how replit manages custom dictionary and list objects under the hood using custom classes like ObservedList and ObservedDict
Maybe you can try to convert this ObservedList to a normal python List?

Here’s some docs discussing use cases of the replit database-

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