如何在discord.py中创建一个可以向特定用户发送文件的命令?


@bot.command()
async def sendfile(user: discord.User, *, file: discord.File):
"""Send a file to a user."""

await user.send(content="here you go", file=discord.File(file))


Traceback (most recent call last):
File "C:UsersberkaAppDataLocalProgramsPythonPython311Libsite-packagesdiscordextcommandsconverter.py", line 1233, in _actual_conversion
return converter(argument)
^^^^^^^^^^^^^^^^^^^
File "C:UsersberkaAppDataLocalProgramsPythonPython311Libsite-packagesdiscordfile.py", line 97, in __init__
self.fp = open(fp, 'rb')
^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument: '<@1065847074253443205>'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:UsersberkaAppDataLocalProgramsPythonPython311Libsite-packagesdiscordextcommandsbot.py", line 1350, in invoke
await ctx.command.invoke(ctx)
File "C:UsersberkaAppDataLocalProgramsPythonPython311Libsite-packagesdiscordextcommandscore.py", line 1015, in invoke
await self.prepare(ctx)
File "C:UsersberkaAppDataLocalProgramsPythonPython311Libsite-packagesdiscordextcommandscore.py", line 932, in prepare
await self._parse_arguments(ctx)
File "C:UsersberkaAppDataLocalProgramsPythonPython311Libsite-packagesdiscordextcommandscore.py", line 839, in _parse_arguments
transformed = await self.transform(ctx, param, attachments)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersberkaAppDataLocalProgramsPythonPython311Libsite-packagesdiscordextcommandscore.py", line 709, in transform
return await run_converters(ctx, converter, argument, param)  # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersberkaAppDataLocalProgramsPythonPython311Libsite-packagesdiscordextcommandsconverter.py", line 1342, in run_converters
return await _actual_conversion(ctx, converter, argument, param)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersberkaAppDataLocalProgramsPythonPython311Libsite-packagesdiscordextcommandsconverter.py", line 1242, in _actual_conversion
raise BadArgument(f'Converting to "{name}" failed for parameter "{param.name}".') from exc
discord.ext.commands.errors.BadArgument: Converting to "File" failed for parameter "file".
@bot.command()
async def sendfile(user: discord.User, file: discord.File):
"""Send a file to a user."""

await user.send(content="here you go", file=file)

如果您想要接收文件上的命令,请参考discord.Attachment。然后,您需要将此附件转换为discord。文件并发送给目标。所以你的命令应该是这样的:

@bot.command()
async def sendfile(user: discord.User, attachment: discord.Attachment):
"""Send a file to a user."""
file = await attachment.to_file()
await user.send(content="here you go", file=file)

相关内容

  • 没有找到相关文章

最新更新