0

I'm trying to create a discord bot command to display a list from a data base, but after watching a video to learn how to do it and (to make sure everything works) copying a piece of code, I get a "TypeError: can only concatenate list (not "ObservedList") to list" error, and I don't understand what that means...

the error

the code :

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

client = discord.Client()



insulte_ = [
  "fdp",
   "pd", 
   "con", 
   "chier"
  ]


def update_insulte(nouvelle_insulte):
  if "insulte" in db.keys():
    insulte = db["insulte"]
    insulte.append(nouvelle_insulte)
    db["insulte"] = insulte
  else:
    db["insulte"] = [nouvelle_insulte]


def delete_insulte(index):
  insulte = db["insulte"]
  if len(insulte) > index:
    del insulte[index]
    db["insulte"] = insulte


@client.event
async def on_ready():
  print('Je me suis lancé en {0.user}'.format(client))

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

  msg = message.content

  """if msg.startswith('$cs hello'):
    await message.channel.send('hello!')


  if msg.startswith('$cs cs'):
    await message.channel.send('vous avez ' + str(score))"""

  options = insulte_
  if "insulte" in db.keys():
    options = options + db["insulte"]


  if any(word in msg for word in options):
    await message.channel.send('vous avez perdu 10 crédits.')
    #score = score - 10


  if msg.startswith("$cs new"):
    nouvelle_insulte = msg.split("$cs new ",1)[1]
    update_insulte(nouvelle_insulte)
    await message.channel.send("nouvelle insulte ajoutée")


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


  if msg.startswith("$cs insultes"):
    await message.channel.send(str(options))

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

1 Answers1

0

The error is in options + db["insulte"]

options is a list, db["insulte"] is an ObservedList. You cannot just concatenate them together.

I think you want:

options + db["insulte"].value

As per this related question How does my list become ObservedList? Problem with replit db

JeffUK
  • 4,107
  • 2
  • 20
  • 34
  • oh thx, this work, but now, when I delete an "insulte" with $cs del, firstly it takes away credits, so I'll have to find a way for it to take into account discord roles (a kind of whitelist of people who can insult), and secondly, an error appears telling me : "index = int(msg.split("cs del",1)[1]) ValueError: invalid literal for int() with base 10: ' nike' ", nike being an insult I just added to the db with $cs new. – ENDERastronaute Nov 28 '21 at 13:27