Javascript 有没有办法让机器人知道帮会成员何时登录discord服务器?

Javascript 有没有办法让机器人知道帮会成员何时登录discord服务器?,javascript,node.js,discord,discord.js,Javascript,Node.js,Discord,Discord.js,我想知道帮会成员何时登录,而不是何时加入,因此guildMemberAdd在这种情况下不起作用。也许有另一种方式来实现我想做的事情,所以我将在这里解释 当我的网站的用户升级到标准或专业会员时,他们可以加入我的discord服务器。我仍然需要弄清楚如何确定discord用户是我网站上的标准/专业订阅会员,但我认为我可以发送一个一次性邀请链接或一个会员必须输入的密码,在机器人发送欢迎消息询问密码或其他信息后发送discord机器人,但这应该是相对简单的 我担心的是,在用户加入discord服务器后,

我想知道帮会成员何时登录,而不是何时加入,因此
guildMemberAdd
在这种情况下不起作用。也许有另一种方式来实现我想做的事情,所以我将在这里解释

当我的网站的用户升级到标准或专业会员时,他们可以加入我的discord服务器。我仍然需要弄清楚如何确定discord用户是我网站上的标准/专业订阅会员,但我认为我可以发送一个一次性邀请链接或一个会员必须输入的密码,在机器人发送欢迎消息询问密码或其他信息后发送discord机器人,但这应该是相对简单的

我担心的是,在用户加入discord服务器后,如果该用户决定取消我网站上的standard/pro会员资格,我该怎么办?我现在想踢那个用户,所以我想我可以检测一个帮会成员何时在我的discord服务器上用bot启动会话,并测试该用户是否仍然是我网站上的标准/专业成员,但似乎没有任何事件

也许我应该用另一种方式来思考这个问题。是否有一种方法可以将成员从discord服务器踢到事件回调上下文之外??今天早上我刚开始学习API,如果我问的很简单,请原谅。我只是在他们的文档中复制/粘贴discord.js示例,看看简单的消息检测是否有效,谢天谢地,它确实有效(下面的代码)


为了跟踪用户,我做了一个邀请过程,当我的网站的一个成员升级到专业或标准帐户时开始。除了发送临时discord服务器密码外,我找不到一种方法来确认用户连接实际上是与特定的邀请进行连接,以了解它是哪个用户。因此,我对bot进行了编码,以提示新用户在触发
guildMemberAdd
事件时将临时密码作为DM输入bot,该密码指向我网站上的用户,然后我在该事务期间存储discord成员id,因此如果成员决定取消订阅,我将相应地删除角色

下面的解决方案非常有效:

client.on("message", async (msg) => {
    if(msg.author.id === client.user.id) { return; }

    if(msg.channel.type == 'dm'){
        try{
            let user = await User.findOne({ discord_id: msg.member.id }).exec();

            if(user)
                await msg.reply("I know you are, but what am I?");

            else {
                user = await User.findOne({ discord_temp_pw: msg.content }).exec();

                if(!user){
                    await msg.reply(`"${msg.content}" is not a valid password. Please make sure to enter the exact password without spaces.`)
                }
                else {
                    const role = user.subscription.status;

                    if(role === "Basic")
                    {
                        await msg.reply(`You have a ${role} membership and unfortunately that means you can't join either of the community channels. Please sign up for a Standard or Pro account to get involved in the discussion.

If you did in fact sign up for a Pro or Standard account, we're sorry for the mistake. Please contact us at info@mydomain.com so we can sort out what happened.`)
                    }
                    else{
                        const roleGranted = await memberGrantRole(msg.member.id, role);
                        const userId = user._id;

                        if(roleGranted){
                            let responseMsg = `Welcome to the team. With a ${role} membership you have access to `
                            
                            if(role === "Pro")
                                await msg.reply(responseMsg + `both the Standard member channel and the and the Pro channel. Go and introduce yourself now!`);

                            else
                                await msg.reply(responseMsg + `the Standard member channel. Go and introduce yourself now!`);
                        }
                        else{
                            await msg.reply("Something went wrong. Please contact us at info@mydomain.com so we can sort out the problem.");
                        }
                        user = { discord_temp_pw: null, discord_id: msg.member.id };

                        await User.findByIdAndUpdate(
                            userId,
                            { $set: user }
                        ).exec();
                    }
                }
            }
        }
        catch(err){
            console.log(err);
        }
    }
}
client.on("guildMemberAdd", (member) => {
    member.send( 
`Welcome to the server ${member.username}!

Please enter the password that you received in your email invitation below to continue.` 
    );
});
const memberGrantRole = async(member_id, role) => {
    const guild = client.guilds.cache.get(process.env.DISCORD_SERVER_ID);
    const member = guild.members.cache.get(member_id);

    try{
        await member.roles.add(role);
    }
    catch(err){
        return {err, success: false};
    }
    return {err: null, success: true};
}

仍然需要测试,但我认为一旦
客户机
准备就绪,我应该能够通过以下方法获得一个特定的不和谐成员
member=client.guilds.fetch('guild').guildmembers.fetch('member')
,并用
kick(member)
或类似的方法踢他们。。稍后我将对此进行测试。不过,我仍然对其他想法持开放态度
client.on("message", async (msg) => {
    if(msg.author.id === client.user.id) { return; }

    if(msg.channel.type == 'dm'){
        try{
            let user = await User.findOne({ discord_id: msg.member.id }).exec();

            if(user)
                await msg.reply("I know you are, but what am I?");

            else {
                user = await User.findOne({ discord_temp_pw: msg.content }).exec();

                if(!user){
                    await msg.reply(`"${msg.content}" is not a valid password. Please make sure to enter the exact password without spaces.`)
                }
                else {
                    const role = user.subscription.status;

                    if(role === "Basic")
                    {
                        await msg.reply(`You have a ${role} membership and unfortunately that means you can't join either of the community channels. Please sign up for a Standard or Pro account to get involved in the discussion.

If you did in fact sign up for a Pro or Standard account, we're sorry for the mistake. Please contact us at info@mydomain.com so we can sort out what happened.`)
                    }
                    else{
                        const roleGranted = await memberGrantRole(msg.member.id, role);
                        const userId = user._id;

                        if(roleGranted){
                            let responseMsg = `Welcome to the team. With a ${role} membership you have access to `
                            
                            if(role === "Pro")
                                await msg.reply(responseMsg + `both the Standard member channel and the and the Pro channel. Go and introduce yourself now!`);

                            else
                                await msg.reply(responseMsg + `the Standard member channel. Go and introduce yourself now!`);
                        }
                        else{
                            await msg.reply("Something went wrong. Please contact us at info@mydomain.com so we can sort out the problem.");
                        }
                        user = { discord_temp_pw: null, discord_id: msg.member.id };

                        await User.findByIdAndUpdate(
                            userId,
                            { $set: user }
                        ).exec();
                    }
                }
            }
        }
        catch(err){
            console.log(err);
        }
    }
}
client.on("guildMemberAdd", (member) => {
    member.send( 
`Welcome to the server ${member.username}!

Please enter the password that you received in your email invitation below to continue.` 
    );
});
const memberGrantRole = async(member_id, role) => {
    const guild = client.guilds.cache.get(process.env.DISCORD_SERVER_ID);
    const member = guild.members.cache.get(member_id);

    try{
        await member.roles.add(role);
    }
    catch(err){
        return {err, success: false};
    }
    return {err: null, success: true};
}