在 discord.js,我可以使用 Discord Bot 向用户发送直接消息吗?
In discord.js, Can I send DirectMessage to user With DiscordBot?
我想用 Discord Bot 向用户发送私人消息。
用户与机器人不在同一服务器上。
如果我可以使用 author.sendMessage,我该如何初始化(查找)作者变量?
我能找到有用户id的用户吗?
感谢阅读。
您的 Client object have a users
属性 其中包含与机器人共享服务器的所有缓存用户。
因此,您可以使用 <Client>.users.get('id')
通过用户 ID 获取用户。
此外,您应该考虑使用 <User>.send('Hi')
,因为 sendMessage
已被弃用。
首先,sendMessage
已弃用,将在进一步的更新中删除。要发送消息,您可以使用 send
。要通过用户 ID 查找用户并向他们发送 DM,您只需执行 Client.users.get("User ID here").send("Message to Send")
。希望这个回答对你有用。
And The user is not in the same server with bot.
和
I make a mistake, I want to send Privatemessage to User who isn't in same server with bot I added..
那是不可能做到的。
机器人需要至少有 1 个公共服务器,用户才能发送直接消息。
如果用户与机器人在同一台服务器上,那么您只能使用此 post.
上的任何其他方法发送 DM
client.users.get("someID").send("someMessage");
我发现你应该试试这个。它应该工作!使用时去掉空格!
client.on('message', msg => {
if (msg.content === `"Your message!"`) {
msg.channel.type === (`"dm"`) + msg.author.sendMessage(`"Your other message"`)
}
}
对于任何对如何使用 Discord.js v12 执行此操作感兴趣的人,方法如下:
client.users.cache.get('<id>').send('<message>');
希望这对某人有所帮助!
使用:mesage.author.send("your message!")
这些答案很好,但你不能保证用户被缓存 client.users.cache.get
,尤其是意图更新限制了 Discord 发送给你的数据量。
一个完整的证明解决方案是这样的(假设环境是异步的):
const user = await client.users.fetch("<id>").catch(() => null);
if (!user) return message.channel.send("User not found:(");
await user.send("message").catch(() => {
message.channel.send("User has DMs closed or has no mutual servers with the bot:(");
});
不仅这个答案可以完成工作,而且如果用户存在,它会首先尝试从缓存中获取用户,如果不存在,库将尝试从 Discord 中获取用户 API 这完全关闭了缓存破坏一切。我还添加了一些 .catch
语句来防止未处理的拒绝并给出更好的响应!
类似于向消息作者发送DM(假设消息对象命名为'message'):
await message.author.send("message").catch(() => {
message.channel.send("User has DMs closed or has no mutual servers with the bot:(");
});
现在请不要太依赖缓存(获取公会和频道时除外,因为它们总是由库缓存),正如其他答案已经指出的那样,机器人必须至少有一个与用户共享的服务器,用户必须打开他们的 DM 并解锁机器人。
在 discord.js 的新版本中只有这样的工作方式:
bot.users.fetch('User ID').then(dm => {
dm.send('Message to send')
})
我想用 Discord Bot 向用户发送私人消息。
用户与机器人不在同一服务器上。
如果我可以使用 author.sendMessage,我该如何初始化(查找)作者变量?
我能找到有用户id的用户吗?
感谢阅读。
您的 Client object have a users
属性 其中包含与机器人共享服务器的所有缓存用户。
因此,您可以使用 <Client>.users.get('id')
通过用户 ID 获取用户。
此外,您应该考虑使用 <User>.send('Hi')
,因为 sendMessage
已被弃用。
首先,sendMessage
已弃用,将在进一步的更新中删除。要发送消息,您可以使用 send
。要通过用户 ID 查找用户并向他们发送 DM,您只需执行 Client.users.get("User ID here").send("Message to Send")
。希望这个回答对你有用。
And The user is not in the same server with bot.
和
I make a mistake, I want to send Privatemessage to User who isn't in same server with bot I added..
那是不可能做到的。
机器人需要至少有 1 个公共服务器,用户才能发送直接消息。
如果用户与机器人在同一台服务器上,那么您只能使用此 post.
上的任何其他方法发送 DM
client.users.get("someID").send("someMessage");
我发现你应该试试这个。它应该工作!使用时去掉空格!
client.on('message', msg => {
if (msg.content === `"Your message!"`) {
msg.channel.type === (`"dm"`) + msg.author.sendMessage(`"Your other message"`)
}
}
对于任何对如何使用 Discord.js v12 执行此操作感兴趣的人,方法如下:
client.users.cache.get('<id>').send('<message>');
希望这对某人有所帮助!
使用:mesage.author.send("your message!")
这些答案很好,但你不能保证用户被缓存 client.users.cache.get
,尤其是意图更新限制了 Discord 发送给你的数据量。
一个完整的证明解决方案是这样的(假设环境是异步的):
const user = await client.users.fetch("<id>").catch(() => null);
if (!user) return message.channel.send("User not found:(");
await user.send("message").catch(() => {
message.channel.send("User has DMs closed or has no mutual servers with the bot:(");
});
不仅这个答案可以完成工作,而且如果用户存在,它会首先尝试从缓存中获取用户,如果不存在,库将尝试从 Discord 中获取用户 API 这完全关闭了缓存破坏一切。我还添加了一些 .catch
语句来防止未处理的拒绝并给出更好的响应!
类似于向消息作者发送DM(假设消息对象命名为'message'):
await message.author.send("message").catch(() => {
message.channel.send("User has DMs closed or has no mutual servers with the bot:(");
});
现在请不要太依赖缓存(获取公会和频道时除外,因为它们总是由库缓存),正如其他答案已经指出的那样,机器人必须至少有一个与用户共享的服务器,用户必须打开他们的 DM 并解锁机器人。
在 discord.js 的新版本中只有这样的工作方式:
bot.users.fetch('User ID').then(dm => {
dm.send('Message to send')
})