2
votes

I want to know when a guild member logs in, not when the member joins, so guildMemberAdd does not work in this case. Perhaps there is another way of going about what I want to do, so I will explain here.

When users of my website upgrade to a standard or pro membership they are able to join my discord server among other things. I still have to sort out how to determine that the discord user is a Standard/Pro subscribing member on my website, but I think I can send a one time invite link or a password which the member has to enter send the discord bot after the bot sends a welcome message asking for the password or something, but that should be relatively straightforward to do.

My concern is after a user has joined the discord server, what should I do if, for example, that user decides to unsubscribe from the standard/pro membership on my website? I want to kick that user now, so I was thinking that I could just detect when a guild member starts a session on my discord server with the bot and test to see if the user is still a standard/pro member from my website, but there doesn't appear to be any event for this.

Perhaps I should be thinking of this in another way. Is there a method for just kicking members from my discord server outside of the event callback context?? I just started with the API this morning, so forgive me if what I'm asking is simple. I literally and shamefully just copy/pasted the discord.js example in their docs to see if simple message detection works and it thankfully does (code below)

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
Still need to test, but I think that once the client is ready I should be able to get a specific member on discord with the following approach member = client.guilds.fetch('guild').guildmembers.fetch('member') and kick them with kick(member) or something similar.. I will test this later. I'm still open for other ideas thoughKTobiasson

1 Answers

0
votes

In order to track the users, I made an invite process which starts when a member of my website upgrades to a Pro or Standard account. I couldn't find a way to confirm the user connecting is in fact connecting with a specific invite to know which user it was other than sending a temporary discord server password. So I coded the bot to prompt a new user to input the temp password as a DM to the bot when the guildMemberAdd event is fired, this password points to the user on my website and then I store the discord member id during this transaction so if a member decides to cancel their subscription, I remove roles accordingly.

The solution below is working like a charm:

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 [email protected] 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 [email protected] 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};
}