我如何才能发出命令,将特定的消息发送给特定的人



我刚开始编程,但在尝试阅读了几个小时的文档后,我放弃了。

我正试图创建一个类似!dm "User" "Message content"的命令,但我无法使其工作,我发现了三个让我发疯的问题。

我不知道如何分离两个参数,也不知道如何指定要发送的用户,还不知道如何从参数中获取用户ID。

这是我的命令:

const Discord = require('discord.js');

module.exports.run = async (client, message, args) => {
const A = args.join(' ')
message.author = args.join(message.mentions.members);
message.author.send(A)
}

但正如你们所看到的,我不知道我在做什么,我希望你们能帮助我

我想用dm发送信息。

在您的命令处理程序中,我假设您获取参数的方式是args.split(" ")

这在大多数情况下都有效,但就像这里一样,当你在论点中需要空格时,它会变得非常棘手。

我建议使用我放在下面的代码来获得你的命令字和args

// make sure whatever your prefix is, is already defined
const prefix = "!"
const input = message.content.slice(prefix.length).trim();
const args = [];
input.match(/"[^"]+"|[S]+/g).forEach((element) => {
if (!element) return null;
return args.push(element.replace(/"/g, ''));
});
console.log(args);
// get the command keyword (first word after prefix)
let cmd = args.shift().toLowerCase();

const command = client.commands.get(cmd); // get the command from the name however you stored it
command.run(client, message, args) // run the command

然后在你的命令下,你可以做

module.exports.run = async (client, message, args) => {
// get either the user from the first mention or from a given userId
let userToSendTo = message.guild.member(message.mentions.users.first()) || message.guild.members.cache.get(args[0])
// args[1] contains the entire message because of the argument parsing code I gave you (above)
userToSendTo.send(args[1])
}

以上所有代码都依赖于用户以格式编写

dm@user";包含空格的消息用引号"包装;

最新更新