【问题标题】:Following a Python tutorial, am getting "TypeError: can only concatenate list (not "ObservedList") to list在 Python 教程之后,我得到 \"TypeError: can only concatenate list (not \"ObservedList\") to list
【发布时间】:2022-10-23 16:17:35
【问题描述】:

我逐字关注this tutorial,并在每一行添加我自己的# 注释。他解释的一切都是有意义的,直到他的代码可以工作,但我的代码却没有,尽管几乎是直接复制。

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"] #we will add those to the list of generic encouragements to create a full list of encouragements
TypeError: can only concatenate list (not "ObservedList") to list

引用的行是我试图添加一个通用的硬编码列表以及一个自定义的用户制作的事物列表来制作一个完整的列表。我正在使用 repl.it,这是他到目前为止的全部代码

import discord  #import basic discord.py crap
import os  #import replit "environment variables" module
import requests  #import "requests" module
import json  #import "json" module
import random  #import "random" module
from replit import db  #import our database

client = discord.Client(
)  #an equals sign tells us that "when I use the term client, I actually means discord.client()"

sad_words = [
    "sad", "depressed", "unhappy", "angry", "miserable", "depressing"
]  #if I put sad_words anywhere, I am actually importing this list of words.

starter_encouragements = [
    "Cheer Up!", "Hang in there.", "You are a great person / bot!"
] #if I put starter_encouragements anywhere, I am actually importing this list of sentences.


def get_quote(): #Keyword def that marks the start of the function header. We are labeling this function as "get_quote()"
    response = requests.get("https://zenquotes.io/api/random") #our responses will be whatever the API sends us
    json_data = json.loads(response.text) #we need to load the data into json_data, because the API sends the text in JSON format
    quote = json_data[0]['q'] + " -" + json_data[0]['a'] #based on the arguments provided by zenquotes.io, 'q' represents the quote, and 'a' represents the author. We use + to add extra shit to the string, so we added a dash for the quote, and we added the data regarding who the author of the quote is to the end of the message. We are clarifying that the quote = quote+dash+author
    return (quote) #return the quote with the dash with the author

def update_encouragements(encouraging_message): #will update the database of encouragements, it will accept an encouraging_message as an argument
  if "encouragements" in db.keys(): #first, check if "encouragmenets" is even a key (entry, row/column) in the database at all. This will get all the keys/list of the keys in the database.
    encouragements = db["encouragements"] #the way to get a value from the database, that is stored under a specific key, is like this. If I write "encouragements", I am actually writing db["encouragements"]. Now the bot has the whole list of existing encouragements
    encouragements.appen(encouraging_message) #this is adding something new to the list of encouragements
    db["encouragements"] = encouragements #now we save the new list back into the database. apparently this is how you do it.
  else: #if there are no encouragements already in the database then we will create it
    db["encouragements"] = [encouraging_message] #we will begin the list with whatever encouraging message was input by the user

def delete_encouragement(index): #this function will delete an encouraging message. First it will index the message to delete
  encouragements = db["encouragements"] #this will call the list of encouragements from the database, to look through
  if len(encouragements) > index: #if the length of the encouragements is more than the index, then we will delete the encouragements at the index
   del encouragements[index] #actually delete it
   db["encouragements"] = encouragements #now we will save the new list into the database


@client.event
async def on_ready(): #when the bot is fully booted and ready to begin
    print('We have logged in as {0.user}'.format(client)) #print to console that we are logged in as the bot, so that I know its operating


@client.event
async def on_message(message): #we read for new messages
    if message.author == client.user: #verify that the message is not from the bot itself
        return #if it is from the bot itself, give up this event chain

    msg = message.content #if I put msg anywhere, I am actually using message.content

    if msg.startswith('!inspire'): #if the message starts with !inspire command, then
        quote = get_quote() #if I put quote anywhere, I am actually using the get_quote() function provided earlier
        await message.channel.send(quote) #send the quote back to the same channel
    
    options = starter_encouragements
    if "encouragements" in db.keys(): #if there are custom encouragements in the database then
      options = options + db["encouragements"] #we will add those to the list of generic encouragements to create a full list of encouragements

    if any(word in msg for word in sad_words): #check the sad_words list, and if a sad word is present, then
        await message.channel.send(random.choice(options)) #send a random encouragement statement

    if msg.startswith("!new"): #if the user starts a message with !new then
      encouraging_message = msg.split("!new ",1)[1] #this sets it so the message after the !new and the space is what is being added to the database
      update_encouragements(encouraging_message) #update the database with new encouragement
      await message.channel.send("New encouraging message added.")

    if msg.startswith("!del"):
      encouragements = [] #we will create an empty list titled encouragements, because we want to return a new list of encouragements later, so we start with nothing.
      if "encouragements" in db.keys(): #check if there are any encouragements already, then
        index = int(msg.split("!del",1)[1]) #we will split the message between the command and what is input afterwards. We do not have to add the space, because we will convert the input to an integer, whether there is a space or not, the user will input a number which will be converted to an integer in python.
        delete_encouragement(index) #delete the encouragement at the index
        encouragements = db["encouragements"] #get the current list of encouragements, so we can show it to the user
      await message.channel.send(encouragements)


my_secret = os.environ['token'] #if I put my_secret anywhere, I am actually using os.environ['token'] which is just repl.it's shitty way of hiding my Discord bot API token from the public
client.run(my_secret)

【问题讨论】:

    标签: python database discord


    【解决方案1】:

    我找到了answer

    如果您想在名为鼓励的 ObserverdList 中获取列表,请使用鼓励.value

    例如:

    options = options + db["encouragements"].value
    

    【讨论】:

      猜你喜欢
      • 2021-02-02
      • 2019-05-26
      • 1970-01-01
      • 2020-09-25
      • 2021-04-26
      • 2020-08-29
      • 1970-01-01
      • 2022-11-01
      • 1970-01-01
      相关资源
      最近更新 更多