AttributeError:' nonepy'对象没有属性'键'

发布于 2025-02-10 02:17:50 字数 2449 浏览 3 评论 0原文

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文