【问题标题】:How to make an discord js bot to message in dm someone after he gets a certain role如何让一个不和谐的 js 机器人在某人获得某个角色后向他发送消息
【发布时间】:2021-08-16 23:02:32
【问题描述】:

我的代码:

const Discord = require('discord.js');
const client = new Discord.Client(); 
const prefix = '$';

client.once('ready' , () => {
    console.log('M-am trezit din morti!');
});

client.on('message' , message  =>{
   if(!message.content.startsWith(prefix) || message.author.bot) return;

   const args = message.content.slice(prefix.length).split(/ +/);
   const command= args.shift().toLowerCase();

   if(command === 'about'){
       
   }else if(command === 'troll'){
       const messages = ["Esti un ratat!!", "Akatsuki este cel mai tare!", "Lui Tobi ii plac baietii!", "Invata sa joci, noobule!" , "Esti pe langa cu jocul!", "Cum naiba ai ajuns in factiunea asta?"]

       const randomMessage = messages[Math.floor(Math.random() * messages.length)];
    
       message.channel.send(randomMessage)
   }
});

client.login('notgonnashowit');

【问题讨论】:

    标签: javascript node.js npm discord bots


    【解决方案1】:

    你可以这样使用:

        client.on("guildMemberUpdate", (oldMember, newMember) => {
          // when Member is updated check if the Member has a role by ID
          if (newMember._roles.includes("ROLE ID HERE")){
              // then DM them
              newMember.send('message to dm');
            } 
        });
    

    要使用上述内容,您必须在机器人仪表板中打开 SERVER MEMBERS INTENT:

    【讨论】:

    • 您也可以分配角色,然后使用.then() 发送消息。这可能比添加一个新的事件监听器更容易。
    • 我如何添加它?它似乎不起作用。当我把东西放在那里时它就不起作用
    猜你喜欢
    • 2020-11-18
    • 2021-01-19
    • 2021-04-23
    • 2019-06-25
    • 2021-04-09
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多