【问题标题】:What is the Error "but it has nothing to do with the score, but with a list in a db" in Python (data base)Python(数据库)中的错误“但它与分数无关,但与数据库中的列表无关”是什么?
【发布时间】:2021-11-28 20:58:06
【问题描述】:

我正在尝试创建一个 discord bot 命令来显示数据库中的列表,但是在观看了视频以了解如何操作并(以确保一切正常)复制一段代码后,我得到了“TypeError: can only concatenate list (not "ObservedList") to list”错误,我不明白这是什么意思...

the error

代码:

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

【问题讨论】:

    标签: python database discord.py


    【解决方案1】:

    错误在options + db["insulte"]

    options 是一个列表,db["insulte"] 是一个ObservedList。您不能只是将它们连接在一起。

    我想你想要:

    options + db["insulte"].value
    

    根据这个相关问题How does my list become ObservedList? Problem with replit db

    【讨论】:

    • 哦,谢谢,这项工作,但是现在,当我用 $cs del 删除“侮辱”时,首先它会带走学分,所以我必须找到一种方法来考虑不和谐的角色(一种可以侮辱的人的白名单),其次,出现一个错误告诉我:“index = int(msg.split("cs del",1)[1]) ValueError: invalid literal for int( ) 以 10 为基数:'nike'",nike 是一种侮辱,我刚刚用 $cs new 添加到数据库中。
    猜你喜欢
    • 1970-01-01
    • 2010-11-16
    • 2011-01-22
    • 2023-04-08
    • 1970-01-01
    • 2019-07-02
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多