I'm receiving an error in my code that my variable (jokes) is being referenced before assignment. For reference, here is my code (all of it) :
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", "Sad" "depressed", "unhappy", "angry", "miserable", "depressing", "rip", "Rip", "annoyed", "suicide", "bash", "beat", "cut", "kill", "murder", "murdering", "killing"]
happy_words = ["better", "happy", "Happy" "thanks", "Thanks", "Thanks!", "thanks!", "amazing", "overjoyed", "grateful", "joyous", "helped", "great"]
jokes = ["I could use a joke right now", "I want to laugh", "I want to see something funny", "I could use a joke"]
starter_encouragements = [
"Cheer up!",
"Hang in there.",
"You are a great person!"
]
if "responding" not in db.keys():
db["responding"] = True
responses = [
"You're welcome!",
"I'm glad I could help.",
"Have a great rest of your day.",
"Don't hesitate to ask for my help in the future!",
"Oh, and in case I don't see you, good afternoon, good evening, and good night!",
]
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 update_jokes(comedy):
if "jokes" in db.keys():
jokes = db["jokes"]
jokes.append(comedy)
db["jokes"] = jokes
else:
db["jokes"] = [comedy]
def delete_encouragment(index):
encouragements = db["encouragements"]
if len(encouragements) > index:
del encouragements[index]
db["encouragements"] = encouragements
def delete_jokes(index):
jokes = db["jokes"]
if len(jokes) > index:
del jokes[index]
db["jokes"] = jokes
@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 msg.startswith('+add'):
comedy = msg.split("+add mm ",1)[1]
update_encouragements(starter_encouragements)
await message.channel.send("New encouraging message added :D")
if msg.startswith('+add joke'):
comedy = msg.split("+add joke ",1)[1]
update_jokes(comedy)
await message.channel.send("New joke added ;D")
if db["responding"]:
options = starter_encouragements
if "encouragements" in db.keys():
options = options + db["encouragements"].value
if any(word in msg for word in sad_words):
await message.channel.send(random.choice(options))
if any(word in msg for word in happy_words):
await message.channel.send(random.choice(responses))
**if any(word in msg for word in jokes):**
await message.channel.send(random.choice(comedy))
if msg.startswith("+help"):
await message.channel.send("Here is a Google Doc of all my commands! https://docs.google.com/document/d/1WKAG5rs0ZLQ7imIiQc9VxY1AkYmGmqM6SaXvAr5XSxI/edit?usp=sharing")
if msg.startswith("+del mm"):
encouragements = []
if "encouragements" in db.keys():
index = int(msg.split("+del mm",1)[1])
delete_encouragment(index)
encouragements = db["encouragements"]
await message.channel.send(encouragements)
if msg.startswith("+del joke"):
jokes = []
if "jokes" in db.keys():
index = int(msg.split("+del joke",1)[1])
delete_jokes(index)
jokes = db[jokes]
await message.channel.send(comedy)
if msg.startswith("+list mm"):
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.getenv('token'))
The line in bold is the line that I'm having an issue with. I suspect that the issue may be somewhere earlier in my code, but I'm unable to pinpoint where. I also think that there may be some other errors in my code contributing to this one. If anybody could help me fix these, that would be great.