代码之家  ›  专栏  ›  技术社区  ›  William

向特定人员发送DMs:不一致。js公司

  •  1
  • William  · 技术社区  · 8 年前

    我在向特定的人发送PM时遇到了一些问题。我知道如何将DM发送给消息的作者。然而,我期待着尝试发送DM直接到一个特定的人。

    async run(message, args) {
        if (args == 'mock') {
          console.log(message.author);
          message.send(Bloodmorphed,
            '1. *No bad mana mocks (funki manas)* \n' +
            '2. Minimum 500k smite must be used at all times \n' +
            '3. No causing the game to lag using skills on attack/hit/struck \n' +
            '4. Must use delerium on attack/attack or attack/hit \n' +
            '5. No use of stash is allowed \n' +
            '6. No 2nd character to view is allowed \n' +
            '7. Matches should only have the two duelist and the host in the game \n' +
            '8. No stopping your attack once you start unless the opponent and host agree \n' +
            '9. 10 minute limit \n' +
            '10. Dueling area should be cleared by the host prior to the duel \n' +
            '11. Must use Nv Valkyrie or Dopplezon \n' +
            '12. Duels last until someone dies \n' +
            '13. Any death after joining the game will count as a loss \n' +
            '14. Each player will have a chance to be 2nd joiner and 3rd joiner. Higher ranked player will be 2nd joiner first. If both are un-ranked, the challenged will be 2nd joiner first \n' +
            '15. Duels must be in a neutral game \n' +
            '16. No mercs / summoned units allowed \n');
        } else if (args == 'legit') {
          message.send('Legit rules test');
        } else {
          message.reply('Error: The command you have entered is correct. Use !help for help on commands.');
        }
      }
    }
    

    我似乎不太明白discord最初是如何处理DMs的。不一致地浏览文件。js和discord,js commando不;似乎没什么帮助。

    1 回复  |  直到 8 年前
        1
  •  3
  •   FireController1847    6 年前

    可以看出,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。

    1. 如果你正在切分,所有的DMs都将转到切分1。( Source )
    2. 您将始终拥有发送嵌入的权限。
    3. 用户可以禁用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```";
        });
    });
    

    祝你好运,编码愉快!

    推荐文章