Discord 制作一个笑话机器人

时间:2021-08-01 23:36:38

标签: python discord discord.py

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

这是错误:

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

1 个答案:

答案 0 :(得分:1)

错误似乎来自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 来获取额外功能时,它会为您省去很多麻烦。