使用 nextcord/discord.py 更改服务器 icon/pfp

Changing server icon/pfp using nextcord/discord.py

我正在尝试通过获取 URL 并发送“良好”来执行更改服务器 icon/pfp 的命令。命令结束时的文本。所以简而言之,我希望我的机器人获得 URL 而不是将服务器 pfp/icon 更改为指定的 url/image 例如:“,setpfp URL”

我的代码:

@bot.command()
async def setpfp(ctx,*,icon:typing.Union[bytes,str]):  
await ctx.guild.edit(icon=icon)  
await ctx.send("Good.")
`

完整回溯错误:

Ignoring exception in on_command_error
Traceback (most recent call last):
  File "/home/runner/Kituzomoa/venv/lib/python3.8/site-packages/nextcord/ext/commands/core.py", line 168, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 59, in setpfp
    await ctx.guild.edit(icon=icon)
  File "/home/runner/Kituzomoa/venv/lib/python3.8/site-packages/nextcord/guild.py", line 1542, in edit
    fields['icon'] = utils._bytes_to_base64_data(icon)
  File "/home/runner/Kituzomoa/venv/lib/python3.8/site-packages/nextcord/utils.py", line 480, in _bytes_to_base64_data
    mime = _get_mime_type_for_image(data)
  File "/home/runner/Kituzomoa/venv/lib/python3.8/site-packages/nextcord/utils.py", line 466, in _get_mime_type_for_image
    if data.startswith(b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A'):
TypeError: startswith first arg must be str or a tuple of str, not bytes

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

Traceback (most recent call last):
  File "/home/runner/Kituzomoa/venv/lib/python3.8/site-packages/nextcord/client.py", line 415, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 1787, in on_command_error
    raise error
  File "/home/runner/Kituzomoa/venv/lib/python3.8/site-packages/nextcord/ext/commands/bot.py", line 1052, in invoke
    await ctx.command.invoke(ctx)
  File "/home/runner/Kituzomoa/venv/lib/python3.8/site-packages/nextcord/ext/commands/core.py", line 933, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/home/runner/Kituzomoa/venv/lib/python3.8/site-packages/nextcord/ext/commands/core.py", line 177, in wrapped
    raise CommandInvokeError(exc) from exc
nextcord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: startswith first arg must be str or a tuple of str, not bytes

你要先从url下载图片,看完再传。

以下是我如何为我的机器人头像做的。公会图标也是如此。

        for image_record in db_avatars.find():
            url = image_record["url"]
            # create directory
            file_name = os.path.join(
                os.path.join(os.getcwd(), "avatars"), url.split("/")[-1]
            )
            # download images
            res = requests.get(url, stream=True)

            if res.status_code == 200:
                file_exists = os.path.exists(file_name)
                if not file_exists:
                    with open(file_name, "wb") as f:
                        shutil.copyfileobj(res.raw, f)
                    print("Image successfully downloaded: ", file_name)
                else:
                    print("This image already exists!")
            else:
                print("Image Couldn't be retrieved")
        # read the file and change avatar
        with open(self.select_random_image_path(), "rb") as file:
                print(file)
                new_avatar = file.read()
                await self.bot.wait_until_ready()
                await self.bot.user.edit(avatar=new_avatar)
                print("Avatar changed successfully!")