Coding Error code

Hi everyone!
I was following the tutorial of BeauCarnes on youtube called “Code a Discord Bot with Python - Host for Free in the Cloud”.
the first issue I had was TypeError: can only concatenate list (not “observedlist”) to list.
So I looked it up and was able to change my input from options = options + db[“encouragements”] to options = options.extend(db[“encouragements”]).

But now I have an error saying: File “main.py”, line 63, in on_message
await message.channel.send (random.choice (options) )

& TypeError: object of type ‘NoneType’ has no len()

HERE IS THE CODE:

if db["responding"]:
  options = starter_encouragements
  if "encouragements" in db.keys():
    options = options.extend(db["encouragements"])

  if any(word in msg for word in sad_words):
    await message.channel.send(random.choice(options))

Please help me!!

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

1 Like

I’m not seeing 63 lines. Do you have more code?

If options is a list, using list.extend extends the list in place (so it appends the items of the argument to the original list), but it does not return anything.

>>> foo = [1,2,3]
>>> bar = [2,3,4]
>>> baz = foo.extend(bar)
>>> baz is None
True
>>> foo
[1, 2, 3, 2, 3, 4]

Here is the full code

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

client = discord.Client()

sad_words = ["sad", "depressed", "unhappy", "angry", "miserable", "depressing"]

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

def delete_encouragment(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.extend(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_encouragment(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.")

keep_alive()
client.run (os.environ['TOKEN'])

So what would you suggest I do?

Only use options.extend() without the assignment.

Then I get this error:

TypeError: extend() takes exactly one argument (0 given)

Yeah, because you left away the argument as well - I didn’t write the actual code…
This is your code: options = options.extend(db[“encouragements”])
This part is the assignment: options =
This is the part your are supposed to keep: options.extend(db[“encouragements”])

1 Like

It worked. Thank you so much Jagaya.

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