当前位置: 首页 > 知识库问答 >
问题:

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

岑光熙
2023-03-14

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

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

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

也许我应该用另一种方式来考虑这个问题。有没有一种方法,只是踢成员从我的不和谐服务器之外的事件回调上下文??我今天早上才开始使用应用编程接口,所以如果我问的很简单,请原谅我。我从字面上可耻地只是复制/粘贴他们文档中的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个答案

西门胜涝
2023-03-14

为了跟踪用户,我做了一个邀请过程,当我的网站成员升级到专业或标准帐户时开始。我找不到一种方法来确认用户连接实际上是与特定的邀请连接,以知道它是哪个用户,而不是发送临时的不和谐服务器密码。因此,我对机器人进行了编码,以提示新用户在触发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};
}
 类似资料: