javascript - 有没有办法让机器人知道公会成员何时登录不和谐服务器?
问题描述
我想知道公会成员何时登录,而不是成员何时加入,因此guildMemberAdd
在这种情况下不起作用。也许还有另一种方法可以解决我想做的事情,所以我将在这里解释。
当我的网站用户升级到标准或专业会员时,他们可以加入我的不和谐服务器等。我仍然需要弄清楚如何确定不和谐用户是我网站上的标准/专业订阅会员,但我想我可以发送一次性邀请链接或会员必须输入的密码,然后发送不和谐机器人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);
解决方案
为了跟踪用户,我做了一个邀请过程,当我的网站成员升级到 Pro 或 Standard 帐户时开始。除了发送临时不和谐服务器密码外,我找不到确认连接的用户实际上是通过特定邀请连接以了解它是哪个用户的方法。因此,我对机器人进行了编码,以提示新用户在事件触发时将临时密码作为 DM 输入机器人guildMemberAdd
,该密码指向我网站上的用户,然后我在此交易期间存储不和谐成员 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};
}
推荐阅读
- javascript - 该语法是什么意思?
- spring - 创建名称为“projectController”的 bean 时出错:通过字段“服务”表示的依赖关系不满足;
- reactjs - 电话号码未保留在 react-phone-input-2 中
- kubernetes - 默认用户禁止创建命名空间,尝试为 EKS 安装 meshery
- python - 无法使用 salesforce_bulk 读取所有 Salesforce 字段
- javascript - 无法读取未定义的属性“slug”,但绝对存在
- scikit-learn - 对正类和 sklearn 指标 pos_label=0 的困惑
- javascript - 如何使用Angular在页面中添加两个sidenav?
- javascript - 在列表中显示和隐藏文本
- c# - c# Linq 如何使用 if 条件编写 Inter Join