首页 > 解决方案 > 不和谐开个玩笑机器人

问题描述

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

这是错误:

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

标签: pythondiscorddiscord.py

解决方案


错误似乎来自joke = json_data["joke"]. 此代码正在寻找一个名为 joke 的 json 键,但是,您得到的集合中唯一的 json 键是错误、类别、类型、设置、交付、nsfw、宗教、政治、种族主义、性别歧视、显式、安全、id、和朗。您可能想要做的是进行设置,然后发送妙语。就像是

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在顶部做)应该工作。作为制作不和谐机器人的人,也只是一个建议,使用commands.bot而不是discord.client。当您以后不可避免地需要开始使用 commands.bot 来获得额外功能时,它将为您节省很多痛苦。


推荐阅读