Trying to build the discord bot from here Code a Discord Bot with Python - Host for Free in the Cloud - YouTube
My discord bot causes error when I try to add new encouragements $new
I’ve narrowed it down to the fact that when I run $new and it calls update rather than creating db[“encouragements”] as a list it creates it as ObservedList(value=[‘nice’])
Which behaves badly with list concatenation.
I cannot understand why this happens and why it does not happen with the similar code here https://replit.com/@BeauCarnes/Encourage-Bot
import discord
import os
import requests
import json
import random
from replit import db
client = discord.Client()
my_secret = os.environ['TOKEN']
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["encouragemments"]
encouragements.append(encouraging_message)
db["encouragements"] = encouragements
else:
db["encouragements"] = [encouraging_message]
print('initialize')
print(db["encouragements"])
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 message.content.startswith('$inspire'):
quote= get_quote()
await message.channel.send(quote)
if db["responding"]:
options = starter_encouragements
if "encouragements" in db.keys():
print(options)
print(db["encouragements"])
options = options + db["encouragements"]
if any(word in msg for word in sad_words):
await message.channel.send(random.choice(starter_encouragements))
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_encouragement(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)
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.")
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run(os.getenv('TOKEN'))