简体   繁体   English

继续获取<property object at 0x000001f7e7fe6f70></property>

[英]Keep Getting <property object at 0x000001F7E7FE6F70>

import lightbulb
from PIL import Image
from io import BytesIO
import requests

plugin = lightbulb.Plugin('Images', 'Create Images')

@plugin.command
@lightbulb.option('user', 'who to create image of',required = True)
@lightbulb.command('slap', 'Slap someone')
@lightbulb.implements(lightbulb.PrefixCommand)
async def wanted(ctx: lightbulb.context, user = hikari.User):
    
    slap = Image.open('images\slap.png')
    userAvatarUrl = user.display_avatar_url
    response = requests.get(userAvatarUrl)
    pfp = Image.open(BytesIO(response.content))

    pfp = pfp.resize(100,100)

    slap.paste(pfp,(120,120))
    slap.save('Profile.jpg')

    await ctx.respond(file = hikari.File('Profile.jpg'))    



def load(bot):
    bot.add_plugin(plugin)

I am trying to fetch the user's avatar url but I keep on getting <property object at 0x000001F7E7FE6F70>.我正在尝试获取用户的头像 url 但我一直在获取 <property object at 0x000001F7E7FE6F70>。 I am using Hikari and Lightbulb for this project.我在这个项目中使用 Hikari 和 Lightbulb。 I have checked the documentation and tried to figure it out but nothing seems to work.我已经检查了文档并试图弄清楚,但似乎没有任何效果。

Full Traceback :完整追溯

  File "C:\Users\HP\Desktop\discord bot\env\lib\site-packages\lightbulb\app.py", line 1038, in handle_message_create_for_prefix_commands
    await self.process_prefix_commands(context)
  File "C:\Users\HP\Desktop\discord bot\env\lib\site-packages\lightbulb\app.py", line 1010, in process_prefix_commands
    await context.invoke()
  File "C:\Users\HP\Desktop\discord bot\env\lib\site-packages\lightbulb\context\base.py", line 311, in invoke
    await self.command.invoke(self)
  File "C:\Users\HP\Desktop\discord bot\env\lib\site-packages\lightbulb\commands\prefix.py", line 119, in invoke
    await self(context, **kwargs)
  File "C:\Users\HP\Desktop\discord bot\env\lib\site-packages\lightbulb\commands\base.py", line 605, in __call__
    return await self.callback(context, **kwargs)
  File "c:\Users\HP\Desktop\discord bot\extensions\Fun\images.py", line 17, in wanted
    response = requests.get(userAvatarUrl)
  File "C:\Users\HP\Desktop\discord bot\env\lib\site-packages\requests\api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
  File "C:\Users\HP\Desktop\discord bot\env\lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\HP\Desktop\discord bot\env\lib\site-packages\requests\sessions.py", line 573, in request
    prep = self.prepare_request(req)
  File "C:\Users\HP\Desktop\discord bot\env\lib\site-packages\requests\sessions.py", line 484, in prepare_request
    p.prepare(
  File "C:\Users\HP\Desktop\discord bot\env\lib\site-packages\requests\models.py", line 368, in prepare
    self.prepare_url(url, params)
  File "C:\Users\HP\Desktop\discord bot\env\lib\site-packages\requests\models.py", line 439, in prepare_url
    raise MissingSchema(
requests.exceptions.MissingSchema: Invalid URL '<property object at 0x000001F7E7FE6F70>': No scheme supplied. Perhaps you meant http://<property object at 0x000001F7E7FE6F70>?

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

Traceback (most recent call last):
  File "C:\Users\HP\Desktop\discord bot\env\lib\site-packages\lightbulb\app.py", line 1057, in handle_message_create_for_prefix_commands
    raise new_exc
lightbulb.errors.CommandInvocationError: An error occurred during command 'slap' invocation

From the code you have supplied I can see that you defined user as an argument to the command function.从您提供的代码中,我可以看到您将user定义为命令 function 的参数。 This is not how options are passed to the command function by default (if you add pass_options=True to the @lightbulb.command decorator then this would work though).这不是默认情况下将选项传递给命令 function 的方式(如果将pass_options=True添加到@lightbulb.command装饰器,那么这将起作用)。

By default command options are stored in an attribute on the context - Context.options .默认情况下,命令选项存储在上下文的属性中 - Context.options In this case, you would retrieve the user object using ctx.options.user .在这种情况下,您将使用ctx.options.user

By supplying the argument in the command function and having it default to the hikari.User class, when you call user.display_avatar_url , you are just returning the property object attached to the class, not an actual User object. By supplying the argument in the command function and having it default to the hikari.User class, when you call user.display_avatar_url , you are just returning the property object attached to the class, not an actual User object.

To fix your code you should do one of the following:要修复您的代码,您应该执行以下操作之一:

Use ctx.options.user instead of an argument in the command function使用ctx.options.user代替命令 function 中的参数

@plugin.command
@lightbulb.option('user', 'who to create image of', type=hikari.User)
@lightbulb.command('slap', 'Slap someone')
@lightbulb.implements(lightbulb.PrefixCommand)
async def wanted(ctx: lightbulb.context):
    ...
    userAvatarUrl = ctx.options.user.display_avatar_url
    ...

Or add the pass_options argument to the @lightbulb.command decorator或者将pass_options参数添加到@lightbulb.command装饰器

@plugin.command
@lightbulb.option('user', 'who to create image of', type=hikari.User)
@lightbulb.command('slap', 'Slap someone', pass_options=True)
@lightbulb.implements(lightbulb.PrefixCommand)
async def wanted(ctx: lightbulb.context, user: hikari.User):
    ...

Important: Note how I added a kwarg type=hikari.User to the @lightbulb.option decorator - this ensures that lightbulb converts the option into a User object before calling your command function.重要提示:请注意我是如何将 kwarg type=hikari.User添加到@lightbulb.option装饰器的 - 这可确保灯泡在调用命令 function 之前将选项转换为User object。 If this was omitted, then the option would be a string, not a User object.如果省略,则选项将是字符串,而不是User object。

NB you are using the synchronous HTTP library requests here which will block your bot while the request is running.注意,您在这里使用的是同步 HTTP 库requests ,这将在请求运行时阻止您的机器人。 You should instead use an async library such as asyncio (included when you install hikari) which will allow other commands to run while the HTTP request is in progress.您应该改用异步库,例如asyncio (安装 hikari 时包含),这将允许在 HTTP 请求正在进行时运行其他命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError: ('事务 %r 未在此连接上打开',<py2neo.client.http.httptransaction object at 0x7fe3fe8f9e80> )</py2neo.client.http.httptransaction> - ValueError: ('Transaction %r is not open on this connection', <py2neo.client.http.HTTPTransaction object at 0x7fe3fe8f9e80>) <sqlite3.Row对象位于0x1017fe3f0>而不是数据库内容 - <sqlite3.Row object at 0x1017fe3f0> instead of database contents 连接错误(<urllib3.connection.HTTPConnection object at 0x7f70f408b3c8> : 建立新连接失败 - ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f70f408b3c8>: Failed to establish a new connection ValueError:无法序列化: <myproject.storage.AzureStorage object at 0x7f85185e66d0> - ValueError: Cannot serialize: <myproject.storage.AzureStorage object at 0x7f85185e66d0> <function attribute at 0x01F0FC70>在文本文件而不是值 - <function attribute at 0x01F0FC70> in text file instead of values 为什么我得到这个?? <generator object <genexpr>在 0x000001C468108DC8></generator> - why am i getting this?? <generator object <genexpr> at 0x000001C468108DC8> __str__输出“0x00E4F558处的实例” - __str__ outputs “instance at 0x00E4F558” 入门 <generator object inner at 0x7f2ad7874050> 错误烧瓶 - Getting <generator object inner at 0x7f2ad7874050> error Flask Python:如何创建一个函数? 例如 f(x) = ax^2 - Python: How to create a function? e.g. f(x) = ax^2 &quot; <function status.<locals> .uptimed at 0x000001C35A56FE20&gt;&quot;,这是什么? - "<function status.<locals>.uptimed at 0x000001C35A56FE20>", What is this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM