What am i doing wrong? (Python/Replit)

Hey guys, i would really really appreciate it if anybody could take a look on “my code” (or yours) because the tutorial is from fCC (Code a Discord Bot with Python - Host for Free in the Cloud - YouTube)

I’ve changed it a bit and with the adding of new “encouraging words” it’s get nasty and i cant get the mistake in line 63… ( so says the Console, but i know it could be anywhere else too)

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


client = discord.Client()

bad_words = ["arschloch", "hurensohn", "Hurensohn", "Hure", "bastard"]

starter_encouragements = [
  "Wir wünschen die Einhaltung der **Netiquette** ansonsten wirst du gebannt!",
  "Achtung, mute in 5 Minuten",
  "Leb dein Leben du Kek"
]

# 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_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('Hello'):
    await message.channel.send('Hi {user}')

  if msg.startswith('Hilfe'):
    await message.channel.send('Wenn du Hilfe brauchst, wende dich an einen <@&686472311615914024>')

  if msg.startswith('Willkommen'):
    await message.channel.send('Welcome Buddy')

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

  if any(word in msg for word in bad_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 encourage 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)

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

I will attach the console error messagedesktop

Hey guys,

i solved the options = options + db[“encouragements”] problem with adding an .value to it. By now, and even if i do it another way with options.extend it will show up the error code i attached here. Maybe you could help me out. This is the code, error in line 29, and 70

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


client = discord.Client()

bad_words = ["arschloch", "hurensohn", "Hurensohn", "Hure", "bastard"]

starter_encouragements = [
  "Wir wünschen die Einhaltung der **Netiquette** ansonsten wirst du gebannt!",
  "Achtung, mute in 5 Minuten",
  "Leb dein Leben du Kek"
]

# 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_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('Hello'):
    await message.channel.send('Hi {user}')

  if msg.startswith('Hilfe'):
    await message.channel.send('Wenn du Hilfe brauchst, wende dich an einen <@&686472311615914024>')

  if msg.startswith('Willkommen'):
    await message.channel.send('Welcome Buddy')

  options = starter_encouragements
  if "encouragements" in db.keys():
    options = options + db["encouragements"].value

  if any(word in msg for word in bad_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 encourage 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)

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

fehler

Welcome there,

As these two posts are related to the same project, I have combined them to help keep the forum easier to navigate, and keep the conversation on one thread.

Thanks for that Sky020, yes thats because i get rid of one problem and didn’t noticed that my other Thread wasn’t released yet. I’ve edited my Entry because somehow it works better but now it says there is no definition… in the tutorial it worked this way too so the definition is given or am i missing something?

Would you mind explaining what your current problem is now? What says there is no definition?

In the above two posts, one issue was the concatenation of an ObservedList to a List, the other issue was the use of an undeclared variable.

Nevermind the first post ( got the ObservedList Problem solved) , the second one is relevant. I’ve attached the error code. There is a Problem with the definition of encouraging. In Line 29 at encouragements.append(encouraging.message) and in Line 70 with update_encouragements(encouraging_message)

1 Like

Can you see the typo here?

1 Like

Should it be

def update_encouragements(encouraging_message):  x
  if "encouragements" in db.keys():
    encouragements = db["encouragements"]
    encouragements.append(encouraging_message)  x

WORKED. But then new Problem appears, now the next step would be the $del command. Now it says: Invalid literal for int() with base 10 in line 76 sooo it’s here:

if msg.startswith("$del"):                                73
    encouragements = []                                      74
    if "encouragements" in db.keys():           75
      index = int(msg.split("$del",1)[1])          76
      delete_encouragement(index)                77
      encouragements = db["encouragements"]              78
    await message.channel.send(encouragements)     79

I encourage you to do some debugging. The error messages are very informative, albeit scary to look at at first.

Add a few print statements here and there, and see if you are getting the values you expect.

Let us know how you get on.

:joy: Nice pun. I look for them, but they are not really revealing for me.
I’ll add them here i don’t know where to go otherwise.
BTW: The Debugger says everything if fine

Here

here2

Clarification: If you are meaning there is no error syntax highlighting, that is not the usual debugger - that is the linter.

Debuggers come in many forms, and icons, and sizes, and they usually require some set up before they are of much use.

However, in your case, you do not have to use one. Just do some debugging: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/#debugging

Start by reading the error message to get an idea of the scope of the problem:

File “main.py”, line 76…
…invalid literal for int() …

Those are the most useful bits.

Now, debug:

if "encouragements" in db.keys():
    msg_list = msg.split("$del", 1)
    my_int = msg_list[1]
    print("msg_list:", msg_list, "my_int:", my_int)
    index = int(my_int)
    ....

Now, you might get more information from that. Or, you might get another error about the length of msg.split("$del", 1)

Hope this helps