从 BOT 所在的服务器之一向任何用户发送 DM

时间:2021-03-18 04:44:50

标签: python discord.py

我的机器人中有一项功能,它将从用户那里获得的 DM 连同用户 ID 一起发送到机器人的官方服务器。所以我希望能够用一个可以为我完成工作的命令来响应消息。 这是我到目前为止所拥有的:

def is_owner(ctx):
    return ctx.message.author.id == my_id
@commands.check(is_owner)
@client.command()
async def dm(ctx, id:discord.User, *text):
    message = " ".join(text)
    for member in client.guilds:
        user = client.get_user(id)
    await user.send(message)
    await ctx.author.send("Message sent!")
@dm.error
async def dm_error(ctx, error):
    if isinstance(error, discord.ext.commands.errors.CheckFailure):
        pass  

当我运行代码时,我收到此错误并且我似乎无法修复它:

Ignoring exception in command dm:
Traceback (most recent call last):
File "C:\Users\servi\AppData\Local\Programs\Python\Python39\lib\site- 
  packages\discord\ext\commands\bot.py", line 903, in invoke
  await ctx.command.invoke(ctx)
File "C:\Users\servi\AppData\Local\Programs\Python\Python39\lib\site- 
  packages\discord\ext\commands\core.py", line 851, in invoke
await self.prepare(ctx)
File "C:\Users\servi\AppData\Local\Programs\Python\Python39\lib\site- 
  packages\discord\ext\commands\core.py", line 786, in prepare
await self._parse_arguments(ctx)
File "C:\Users\servi\AppData\Local\Programs\Python\Python39\lib\site- 
  packages\discord\ext\commands\core.py", line 697, in _parse_arguments
transformed = await self.transform(ctx, param)
File "C:\Users\servi\AppData\Local\Programs\Python\Python39\lib\site- 
  packages\discord\ext\commands\core.py", line 552, in transform
return await self.do_conversion(ctx, converter, argument, param)
File "C:\Users\servi\AppData\Local\Programs\Python\Python39\lib\site- 
  packages\discord\ext\commands\core.py", line 505, in do_conversion
return await self._actual_conversion(ctx, converter, argument, param)
File "C:\Users\servi\AppData\Local\Programs\Python\Python39\lib\site- 
  packages\discord\ext\commands\core.py", line 451, in _actual_conversion
ret = await instance.convert(ctx, argument)
File "C:\Users\servi\AppData\Local\Programs\Python\Python39\lib\site- 
  packages\discord\ext\commands\converter.py", line 239, in convert
raise UserNotFound(argument)
discord.ext.commands.errors.UserNotFound: User "user_id_that_i_specified" not found.

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用 await client.fetch_user(id) 来获取用户对象。

@client.command()
async def dm(ctx, _id : int, *, text):
    user = await client.fetch_user(_id)
    await user.send(text)

你会像!dm <specified_user_id> hello this is a test dm