【问题标题】:Discord making a joke bot不和谐开个玩笑机器人
【发布时间】:2021-10-07 10:13:32
【问题描述】:

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

这是错误:

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

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    错误似乎来自joke = json_data["joke"]。此代码正在寻找一个名为 joke 的 json 键,但是,您得到的集合中唯一的 json 键是 error、category、type、setup、delivery、nsfw、religious、political、racist、sexist、explicit、safe、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 来获得额外功能时,它将为您省去很多麻烦。

    【讨论】:

    • 所以我必须添加您告诉我使用的第一个代码,方法是替换 joke = json_data[“joke”],然后替换第二个代码而不是 **await。 message.channel.send(joke) 我对编程有点陌生,我不太懂一些东西,很抱歉我很笨
    • @MoazHaider 是的。确保将import asyncio 与其余导入放在顶部,并删除joke = get_joke()。同时删除return(joke)
    • 我收到一个新错误@tester2080 我会以答案的形式告诉你我写的代码
    • @MoazHaider 在你的代码中你已经完成了punchline = json_data["punchline"],而它应该是punchline = json_data["delivery"]。没有带有妙语键的json数据。 (json 密钥对基本上类似于category:"Misc"delivery:"...",其中键是类别,值是“Misc”。请注意称为交付的键是您想要的键)。如果您觉得这有帮助,您可以将我的回答标记为已接受 :)
    • 它现在可以工作了,非常感谢你让它再次工作花了很长时间,但现在可以工作了,谢谢
    猜你喜欢
    • 2020-07-09
    • 2017-05-10
    • 2018-03-20
    • 2020-07-03
    • 2020-09-23
    • 2021-10-30
    • 2021-08-05
    • 2021-05-27
    • 2021-07-15
    相关资源
    最近更新 更多