【发布时间】: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