AttributeError:' nonepy'对象没有属性'键'
import discord
import requests
import json
import random
from replit import db
client = discord.Client()
sad_words = ["sad", "depressed" , "unhappy", "angry" , "miserable" , "depressing"]
starter_encouragements = [ "Cheer up!", "Hang in there." , "You are a great person /
bot!"]
def get_quote():
response = requests.get("https://zenquotes.io/api/random")
json_data = json.loads(response.text)
quote = json_data[0]['q'] + "-" + json_data[0]['a']
return(quote)
def update_encouragements(encouraging_message):
if "encouragements" in db.keys():
encouragements = db["encouragements"]
encouragements.appen(encouraging_message)
db["encouragements"] = encouragements
else:
db["encouragements"] = [encouraging_message]
def delete_encouragement(index):
encouragements = db["encouragements"]
if len(encouragements) > index:
del encouragements[index]
db["encouragements"] = encouragements
TOKEN = XXX
@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
msg = message.content
if msg.startswith('inspire'):
quote = get_quote()
await message.channel.send(quote)
options = starter_encouragements
if "encouragements" in db.keys():
options = options + db["encouragements"]
if any(word in msg for word in sad_words) :
await message.channel.send(random.choice(options))
if msg.startswith("$new"):
encouraging_message = msg.split("$new " ,1)[1]
update_encouragements(encouraging_message)
await message.channel.send("New encouraging message added.")
if msg.startswith("del"):
encouragements = []
if "encouragements" in db.keys():
index = msg.split("$del" , 1)[1]
delete_encouragement(index)
encouragements = db["encouragements"]
await message.channel.send(encouragements)
client.run(TOKEN)
'''
这是一个DISCORD机器人,我遇到的错误是“非电视”对象没有属性“键”。这是一个属性错误。我希望我能得到一些帮助。 (关于我,只是一个初学者尝试新事物并喜欢它)请放下一些技巧,以制作更有效的机器人 '''
错误在下面写下
--------->错误:'如果在db.keys()中“鼓励”:'
'attributeError:'nontype'对象没有 属性“钥匙”
import discord
import requests
import json
import random
from replit import db
client = discord.Client()
sad_words = ["sad", "depressed" , "unhappy", "angry" , "miserable" , "depressing"]
starter_encouragements = [ "Cheer up!", "Hang in there." , "You are a great person /
bot!"]
def get_quote():
response = requests.get("https://zenquotes.io/api/random")
json_data = json.loads(response.text)
quote = json_data[0]['q'] + "-" + json_data[0]['a']
return(quote)
def update_encouragements(encouraging_message):
if "encouragements" in db.keys():
encouragements = db["encouragements"]
encouragements.appen(encouraging_message)
db["encouragements"] = encouragements
else:
db["encouragements"] = [encouraging_message]
def delete_encouragement(index):
encouragements = db["encouragements"]
if len(encouragements) > index:
del encouragements[index]
db["encouragements"] = encouragements
TOKEN = XXX
@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
msg = message.content
if msg.startswith('inspire'):
quote = get_quote()
await message.channel.send(quote)
options = starter_encouragements
if "encouragements" in db.keys():
options = options + db["encouragements"]
if any(word in msg for word in sad_words) :
await message.channel.send(random.choice(options))
if msg.startswith("$new"):
encouraging_message = msg.split("$new " ,1)[1]
update_encouragements(encouraging_message)
await message.channel.send("New encouraging message added.")
if msg.startswith("del"):
encouragements = []
if "encouragements" in db.keys():
index = msg.split("$del" , 1)[1]
delete_encouragement(index)
encouragements = db["encouragements"]
await message.channel.send(encouragements)
client.run(TOKEN)
'''
This a discord bot , the error i am getting is 'NoneType' object has no attribute 'keys'.It's an attribute error. I hope i could get some help. (About me , just a beginner trying new things bumped into this and loved it ) pls drop some tips too for making more efficient bots
'''
error is written below
-------->error: ' if "encouragements" in db.keys(): '
' AttributeError: 'NoneType' object has no
attribute 'keys' '
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论