user16573988
user16573988

Reputation:

Discord making a joke bot

I have been having issues making the joke function work in my discord bot it was working just fine yesterday but today i don’t know what’s happening and it’s just not responding to the joke command, i have tried changing the string names as well and it worked one time and responded to a word which didn’t exist in the code and that word was jokes which i don’t know how it reponded to it and it doesn’t respond to it anymore i’m really confused and don’t know how to make it work kindly help me fix this problem i will be thankful to you.

This is the error:

Traceback (most recent call last):
ах
File "/opt/virtualenvs/python3/lib/python3.8/s
ite-packages/discord/client.py", line 343, in _r
un_event
await coro(*args, **kwargs)
File "main.py", line 48, in on_message
joke
get_joke()
File "main.py", line 33, in get_joke
joke json_data["joke"]
KeyError: 'joke'

This is the code:

import discord
import os
import requests
import json
import random
from keep_alive import keep_alive

client = discord.Client()

bye_statements =["bye","Bye", "cya" , "Cya"]

hello_statements = ["hello","Hello","wsp","sup",'Hi',"Sup"]

sad_words = ["sad", "depressed", "unhappy", "angry", "miserable", "depressing","depression"]

starter_encouragements = [
        "It's ok man relax and just watch some hentai lol im joking",
    "Cheer up man!!", "Time will pass dont worry" , "Never give up","Go and watch some anime that will cheer you up :)","Haha keep crying like a girl"
]

def get_joke():
  response = requests.get("https://v2.jokeapi.dev/joke/Programming,Miscellaneous,Dark,Pun,Spooky,Christmas?blacklistFlags=religious&type=twopart")
  json_data = json.loads(response.text)
  joke = json_data["joke"]
  return(joke)

@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

  if message.content.startswith("joke"):
    joke = get_joke()
    await message.channel.send(joke)

  if any(word in message.content for word in sad_words):
    await message.channel.send(random.choice(starter_encouragements))
  
  if any(word in message.content for word in(hello_statements)):
    await message.channel.send('Hey there,I hope you are doing well :)')

  if message.content.startswith("hi"):
    await message.channel.send("Hey there,I hope you are doing well :)")
    
  if message.content.startswith("!list"):
    await message.channel.send(starter_encouragements)
  
  if message.content.startswith('sus'):
   await message.channel.send('sussy baka')
  
  if any(word in message.content for word in bye_statements):
     await message.channel.send("Bye, see you later!")
  
  if message.content.startswith("lol"):
    await message.channel.send("lol")
  
  if message.content.startswith("stfu"):
    await message.channel.send("no")
    
  
  
keep_alive()
client.run(os.environ['TOKEN'])

Upvotes: 2

Views: 840

Answers (1)

tester2080
tester2080

Reputation: 51

The error seems to come from joke = json_data["joke"]. This code is looking for a json key named joke, however, the only json keys in the set you are getting are error, category, type, setup, delivery, nsfw, religious, political, racist, sexist, explicit, safe, id, and lang. What you probably want to do is get the setup and then send the punchline. Something like

setup = json_data["setup"]
punchline = json_data["delivery"]
return setup,punchline

and then do

setup,punchline = get_joke()
await message.channel.send(setup) 
await asyncio.sleep(5) 
await message.channel.send(punchline)

(make sure you do import asyncio at the top) should work. Also just a word of advice as someone who's made a discord bot, use commands.bot rather than discord.client. It will save you a lot of pain when you inevitably need to start using the commands.bot for the extra functionality later on.

Upvotes: 2

Related Questions