首页 > 解决方案 > 如何通过嵌入页脚向具有 ID 的用户发送消息?

问题描述

我正在尝试通过消息 ID 获取用户 ID;我要向其发送 DM 的用户 ID 位于嵌入的页脚中。我正在使用此代码:

msg = await ctx.channel.fetch_message(id)
id2 = int(msg.embeds[0].footer.text)
print(type(id2))
await ctx.send(id2, hidden=True)
# Everything works fine ^
# Everything breaks below
user = client.get_user(id2)
print(type(user))
await ctx.send(user, hidden=True)
await user.send("IT WORKED!!!")

我添加了一些调试代码,当它尝试将其转换为user = client.get_user(id2). 它把它变成虚无;我在函数中看到了这一点type(),并且在错误中:

An exception has occurred while executing command `answer`:
Traceback (most recent call last):
  File "/Users/dante_nl/Library/Python/3.8/lib/python/site-packages/discord_slash/client.py", line 1185, in invoke_command
    await func.invoke(ctx, **args)
  File "/Users/dante_nl/Library/Python/3.8/lib/python/site-packages/discord_slash/model.py", line 209, in invoke
    return await self.func(*args, **kwargs)
  File "/Users/dante_nl/Library/Mobile Documents/com~apple~CloudDocs/Ask/bot.py", line 105, in _answer
    await ctx.send(user, hidden=True)
  File "/Users/dante_nl/Library/Python/3.8/lib/python/site-packages/discord_slash/context.py", line 239, in send
    resp = await self._http.post_followup(base, self._token, files=files)
  File "/Users/dante_nl/Library/Python/3.8/lib/python/site-packages/discord/http.py", line 254, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty message

我不确定它为什么这样做,它从嵌入的页脚获取 ID,将其转换为数字,但它无法将其转换为用户并将 DM 发送给该用户。

提前致谢!

注意:我正在使用斜杠命令,不确定这是否有任何区别。

标签: pythondiscord.py

解决方案


您可以使用:

await client.fetch_user(id2)

如果这不起作用,那么可能 id2 不是有效的用户 ID。


推荐阅读