【问题标题】:'str' object has no attribute 'text'. Cant seem to be able to fix it“str”对象没有属性“text”。似乎无法修复它
【发布时间】:2022-01-24 00:49:12
【问题描述】:

有人可以帮我解决这个错误吗,我查看了其他有相同错误但似乎无法将其应用于我的代码的错误,我不知道这是因为我不擅长编码还是因为它不能应用于我的代码,我希望人们发送更新的代码,然后他们用文字告诉我该怎么做,因为我通常不明白它

这是我的代码:

@client.command()
async def ipcheck(ctx, IP):
    await ctx.send(get('https://ipapi.co/' + (IP) + '/json/').text)

这是错误:

Ignoring exception in command ipcheck:
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\User\Desktop\Programming\TEST.py", line 51, in ipcheck
    await ctx.send(get('https://ipapi.co/' + (IP) + '/json/').text)
AttributeError: 'str' object has no attribute 'text'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'text'

【问题讨论】:

    标签: python error-handling discord discord.py


    【解决方案1】:

    看来get('https://ipapi.co/' + (IP) + '/json/')已经返回了一个字符串,你不需要使用text属性

    @client.command()
    async def ipcheck(ctx, IP):
        await ctx.send(get('https://ipapi.co/' + (IP) + '/json/'))
    

    【讨论】:

    • 我刚刚尝试过,但是当我这样做时,机器人只在聊天回中发送:“h”,我不知道这是为什么,因为我已经用普通的 python 命令行尝试过,然后它就可以工作了它不是 API。感谢您的快速回复。如果你知道如何解决这个问题,我想得到另一个反应。顺便说一句,它没有显示任何错误
    • 您是否尝试过打印get('https://ipapi.co/' + (IP) + '/json/') 返回的内容?也许它只返回单个“h”
    • 这就是我所做的,但它适用于打印,只是不适用于不和谐机器人。如果你情绪低落,我们可能会尝试在不和谐的通话中解决这个问题,因为我不知道如何解决
    • 是的,请随时加我:kwieeciol#6035
    • 我刚刚加了你,随时给我打电话。很抱歉每次都回复晚了。
    猜你喜欢
    • 2018-08-10
    • 2023-04-07
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    相关资源
    最近更新 更多