【问题标题】:discord.js PMing users by command through mentioning [closed]discord.js 通过提及通过命令 PMing 用户 [关闭]
【发布时间】:2017-12-17 18:30:23
【问题描述】:

我正在尝试创建一个命令,其中它有一个机器人 PM 提到的用户遵循前缀预先输入的消息。我该怎么做?

【问题讨论】:

  • 我不太明白。你的意思是像 !pm @user hello there.. 这样的命令吗?机器人 pms sad 用户说“你好”?
  • 我的意思是这样的命令,但是,我没有输入实际消息本身,而是输入几个代表实际消息的字母,这些字母是预先输入的。

标签: javascript node.js discord


【解决方案1】:

抱歉回复晚了。在我的评论中要求澄清后睡着了。只需创建一个对象来将您的键(字母)映射到消息。

let messageObject = {
    ab: "This is a message that sent from the keyword ab.",
    vd: "This is another message that sent from the keyword vd",
    test: "And a third diff message that sent from the keyword test"
};

client.on("message", message => {
    if(message.content.toLowerCase().startsWith("!dm")) {
        let args = message.content.split(" ").slice(1);
        let user = message.guild.member(args[0].replace(/[<@!>]/g,"")).user;
        let keyword= args[1];

        if(keyword in messageObject){
            return user.send(messageObject[keyword]);
        }
        return message.channel.send("That is not a valid keyword!");
    }
});

【讨论】:

  • 我已经尝试了您的代码,但对我来说,它似乎无法正常工作。我的机器人根本没有给我发消息。
  • 有没有报错?您可以将我链接到您的整个代码,除了机器人令牌@Christopher
  • 存在“TypeError: Cannot read property 'Replace' of undefined”
  • 哦,我的错,忘了引号。编辑我的答案,再试一次? @克里斯托弗
  • 非常感谢。现在可以了。
【解决方案2】:

确保您拥有最新版本的 node 和 discord.js。在 discord.js 的最近更新中添加了使用提及

bot.on('message', message => {
  if (message.content.startsWith(prefix + "pm")) {
    for (let user of message.mentions.users.values()) {
      if (message.content.includes("a couple letters")) {
        user.send("pre-recorded message");
      } else if (message.content.includes("different letters")) {
        user.send(" a different pre-recorded message");
      }
    }
  }
});

【讨论】:

    猜你喜欢
    • 2021-12-03
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    相关资源
    最近更新 更多