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

时间:2021-03-28 09:24:58

标签: javascript node.js discord discord.js

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

当我网站的用户升级为标准或专业会员时,他们可以加入我的 Discord 服务器等。我仍然需要弄清楚如何确定 Discord 用户是我网站上的 Standard/Pro 订阅会员,但我想我可以发送一次邀请链接或会员必须输入的密码,然后发送 Discord bot bot 会发送一条欢迎消息,要求输入密码或其他内容,但这应该相对简单。

我担心的是,在用户加入 Discord 服务器后,例如,如果该用户决定取消订阅我网站上的标准/专业会员资格,我该怎么办?我现在想踢那个用户,所以我想我可以检测公会成员何时在我的不和谐服务器上与机器人开始会话并测试该用户是否仍然是我网站上的标准/专业会员,但是似乎没有任何活动。

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

const Discord = require("discord.js")
const client = new Discord.Client()

client.on("ready", () => {
  console.log(`Logged in as ${client.user.tag}!`)
});

client.on("message", (msg) => {
  if (msg.content === "ping") {
    msg.reply("Pong!")
  }
});

client.on("guildMemberAdd", (member) => {
    member.send(
      `Welcome on the server! Please be aware that we won't tolerate troll, spam or harassment.`
    );
});

client.login(process.env.DISCORD_EVERBOT_TOKEN);

1 个答案:

答案 0 :(得分:0)

为了跟踪用户,我做了一个邀请过程,当我的网站成员升级到 Pro 或 Standard 帐户时开始。我找不到一种方法来确认连接的用户实际上是通过特定邀请连接以了解它是哪个用户,而不是发送临时不和谐服务器密码。因此,我对机器人进行了编码,以在触发 guildMemberAdd 事件时提示新用户将临时密码作为 DM 输入机器人,此密码指向我网站上的用户,然后在此期间存储不和谐成员 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};
}