闹笑话

2024-05-19 12:34:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在我的discord机器人中使用笑话功能时遇到了一些问题。它昨天运行得很好,但今天我不知道发生了什么,它只是没有响应笑话命令,我也尝试过更改字符串名称,它工作了一次,响应了一个代码中不存在的单词,这个单词是笑话,我不知道它是如何响应的,它不再响应了。我真的很困惑,不知道如何使它工作。请帮我解决这个问题,我会的感谢你

这就是错误:

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'

代码如下:

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'])

Tags: inimportclientsendjsonmessagegetif
1条回答
网友
1楼 · 发布于 2024-05-19 12:34:50

错误似乎来自joke = json_data["joke"]。这段代码正在寻找一个名为joke的json密钥,但是,您得到的集合中唯一的json密钥是error、category、type、setup、delivery、nsfw、宗教、政治、种族主义、性别歧视、explicit、safe、id和lang。您可能想要做的是获得设置,然后发送punchline。差不多

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

然后呢

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

(确保在顶部执行import asyncio)应该可以工作。还有一点建议,对于制作了discord机器人的人,使用commands.bot而不是discord.client。当您以后不可避免地需要开始使用commands.bot来实现额外功能时,这将为您节省很多痛苦

相关问题 更多 >