I'm a python beginner and recently I've been trying to develop a bot for discord. The problem is that I've been getting the following error when I type the command "!new"
:
await coro(*args, **kwargs)
File "main.py", line 71, in on_message
update_encouragements(encouraging_message)
File "main.py", line 32, in update_encouragements
encouragements.append(encouraging_message)
AttributeError: 'bool' object has no attribute 'append'
I already tried to rewrite the update_encouragement function and the encouragement.append but I can't get out of this error. I'm out of ideas on how to solve this.
This is my code
import discord
import os
import requests
import json
import random
from replit import db
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_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('!inspire'):
quote = get_quote()
await message.channel.send(quote)
if db["responding"]:
options = starter_encouragements
if "encouragements" in db.keys():
(options) += str(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_encouragement(index)
encouragements = db["encouragements"]
await message.channel.send(encouragements)
if msg.startswith("!lista"):
encouragements = []
if "encouragements" in db.keys():
encouragements = db["encouragements"]
await message.channel.send(encouragements)
if msg.startswith("!responding"):
value = msg.split("!responding ",1)[0]
if value.lower() == "true":
db["encouragements"] = True
await message.channel.send("Responding is on.")
else:
db["encouragements"] = False
await message.channel.send("Responding is off.")
You are missing the .content
method on line 32. They encouragements
keywords is a boolean value that returns a true
or false
statement. So you can fix it by changing line 32 to:
encouragements.append(encouraging_message.content)
The error shows that the encouragements
variable is bool.
check out dir(encouragements)
to check which methods you can use on this object to get a list object
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.