javascript - DM 特定用户 ID
问题描述
const Discord = require('discord.js');
const bot = new Discord.Client();
const PREFIX = "!!";
bot.on("message", async message => {
if(message.content.toLowerCase().startsWith(`${PREFIX}suggest`)) {
//a user says "!!suggest more commands"
message. /*ID <@206377170707152906>*/ .send(`The user {$message.author.name.toString()} has given a suggestion; "${message.content.toString()}`);
//then a DM is sent to the <@206377170707152906> ID saying (the ID is different from user ABC123's ID) "The user ABC123 has given a suggestion; "!!suggest more commands"
message.author.send(`The suggestion "${message.content.toString()}" has been sent!`);
return;
}}
如何将 ID "<@206377170707152906>" 放入 message.send 以便将 DM 发送到 "<@206377170707152906>" ID?
解决方案
要发送直接消息,您需要用户对象。您可以从 获取缓存的用户client.users
,但该用户可能不在缓存中。要“加载”该用户并向他发送消息,您可以执行以下操作:
client.users.fetch('123456789').then((user) => {
user.send("My Message");
});
不要忘记替换123456789
为您想要的 ID。在这种情况下,您只需要数字,不需要<@...>
推荐阅读
- twilio - 从 Twilio 频道中的消息中识别发件人
- html - Bootstrap 导航栏折叠图标不出现
- excel - 如何编写宏来刷新文件夹中所有 Excel 文件的所有数据连接?
- python - 执行多次运行时如何使用 Keras 保留纪元数?
- java - 地图
> 在没有 pre java8 for 循环的情况下创建 - java - 打印根据语言环境翻译的与时间相关的字符串,如“今天”和“昨天”?
- java - 如何解决与在 Debian 8 (Jessie) 上安装 Java 8 相关的问题?
- c++ - 从文件读取到向量
- reactjs - `npm list`,它不显示`create-react-app`
- bluetooth-lowenergy - 如何欺骗单个 BLE 数据包