I've gotten the error that the name of my variable 'is parameter' and global, and thisis after an attempt to fix another error which says the same variable (comedy), is referenced before assignment. To fix this I tried to do 'global comedy', which h ad worked for the same type of error earlier, but all it did was lead to 'SyntaxError: name 'comedy' is parameter and global. For reference, here is my code. It's for a discord bot called Inspire which can pick up certain key words and respond. IN this case I'm making it respond to certain phrases with a joke, which users can add into the bot's database. This is where the issue with the code begins. For reference, here is my 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", "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):
global jokes
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 mm'):
encouragements = 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 lines where I define 'update_jokes(comedy):' is where the issue is, but I can't seem to pick it up. I'm worried that fixing this error will lead to another error which I can't spot and so I'm asking whether or not anybody can spot any underlying errors in my code, in addition to this one.