[英]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.