我需要一些帮助... 我一直在尝试让我的机器人在24/7中搜索脱机的成员,这些成员脱机并且角色仍然存在并删除它们 (该机器人在Heroku服务器上运行)
我有这段代码,可以通过使用游戏活动来添加和删除特定角色。 我尝试使用forEach()函数,但被卡住了
client.on('presenceUpdate', (oldMember, newMember) => {
const guild = newMember.guild;
const playingRole = guild.roles.find(role => role.id === '671631357725638656');
if (newMember.user.bot || oldMember.presence.status !== newMember.presence.status) return;
const oldGame = oldMember.presence.game && [0, 1].includes(oldMember.presence.game.type) ? true : false;
const newGame = newMember.presence.game && [0, 1].includes(newMember.presence.game.type) ? true : false;
if (!oldGame && newGame) {
newMember.addRole(playingRole)
} else if (oldGame && !newGame) {
newMember.removeRole(playingRole)
}
});
谢谢!
答案 0 :(得分:1)
Discord JS V12:
Client.on("presenceUpdate", (oldPresence, newPresence) => { // We don't really need to use oldPresence here.
if (newPresence.guild.id !== "GuildID") {return false}; // Did this because it will listen to presenceUpdate events from all guilds. You may remove this line if you'd like.
const Role = newPresence.guild.roles.cache.get("RoleID"); // Finding the required role.
if (!Role) {return console.error("No role found.")}; // If the role doesn't exist we return an error.
if (newPresence.status == "offline") { // online - user is online; idle - user is AFK; offline - user is offline or invisible; dnd - user is in Do Not Disturb;
newPresence.member.roles.remove(Role).catch(e => {console.error(e)}); // Removing the role since the user is offline.
} else {
newPresence.member.roles.add(Role).catch(e => {console.error(e)}); // Adding the role since the user is Online/Idle/Do Not Disturb
};
});
Discord JS V11:
Client.on("presenceUpdate", (oldGuildMember, newGuildMember) => {
if (newGuildMember.guild.id !== "GuildID") {return false};
const Role = newGuildMember.guild.roles.get("RoleID");
if (!Role) {return console.error("No role found.")};
if (newGuildMember.presence.status == "offline") {
newGuildMember.removeRole(Role).catch(e => {console.error(e)});
} else {
newGuildMember.addRole(Role).catch(e => {console.error(e)});
};
});