我正在尝试通过消息 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。
提前致谢!
注意:我正在使用斜杠命令,不确定这是否有任何区别。
答案 0 :(得分:1)
您可以使用:
await client.fetch_user(id2)
如果这不起作用,那么 id2 可能不是有效的用户 ID。