可以看出,Discord通过用户对象(GuildMember扩展的对象)处理DMs
from the documentation
. 从这里开始,它实现了一个TextBasedChannel,或者您所说的DM频道。要向用户发送消息,您可以执行以下操作:
async run(message, args) => {
message.author.send("Hello, this is a DM!");
}
或者,如果您想使用
GuildMember
...
async run(message, args) => {
message.member.user.send("Hello, this is a DM!");
}
DMs在Discord上有一些特殊的地方。不过是js。
-
如果你正在切分,所有的DMs都将转到切分1。(
Source
)
-
您将始终拥有发送嵌入的权限。
-
用户可以禁用DMs。
要检测DM消息,我喜欢对我的消息事件执行以下操作:
bot.on("message", async m => {
m.isDM = (m.guild ? false : true);
});
这是因为如果收到DM消息,帮会对象将为空。此外,请记住,在发送DM时,bot无法检查用户是否禁用了DMs。这意味着捕获所有DM消息以防失败非常重要。这里有一个例子。
async run(message, args) => {
message.author.send("Hello, I'm a DM that handles if users don't have permission!").catch(e => {
message.channel.send("There was an internal error attempting to send you a message.\n" + "```js\n" + e.stack + "\n```";
}
}
根据要求,以下是在commando中使用上述所有内容的示例:
client.on("commandRun", cmd => {
cmd.message.message.isDM = (m.guild ? false : true);
cmd.message.message.author.send("Hello, I'm a DM that handles if users don't have permission!").catch(e => {
cmd.message.message.channel.send("There was an internal error attempting to send you a message.\n" + "```js\n" + e.stack + "\n```";
});
});
祝你好运,编码愉快!