英文:
Read status of server members and give/remove roles , discord.js v14
问题
I need my bot read member servers status and if here write my server tag ( discord.gg/... ) give them role and if they take it out remove it. How can i do this?
client.on('presenceUpdate', async (oldPresence, newPresence) => {
const role = newPresence.guild.roles.cache.get('910192386267840513');
const member = newPresence.member;
const activities = member.user.presence.activities[0];
if (activities && (activities.state.includes('.gg/') || activities.state.includes('discord.gg/'))) {
return newPresence.member.roles.add(role);
}
else if (member.roles.cache.get(role.id)) {
newPresence.member.roles.remove(role);
}
});
I found that code but giving errors.
EDIT 2
I changed my code and it didn't give errors but doesn't work.
const { Client, GatewayIntentBits, Partials } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildMembers,
],
partials: [
Partials.Channel,
Partials.Message,
Partials.User,
Partials.GuildMember,
Partials.Reaction,
],
});
module.exports = client;
client.on("presenceUpdate", (oldPresence, newPresence) => {
if (newPresence.activities.some(a => a.state && a.state.includes("gg"))) {
console.log("1 role gave");
newPresence.member.roles.add("1127983091386962060");
} else {
newPresence.member.roles.remove("1127983091386962060");
}
});
英文:
I need my bot read member servers status and if here write my server tag ( discord.gg/... ) give them role and if they take it out remove it. How can i do this?
client.on('presenceUpdate', async (oldPresence, newPresence) => {
const role = newPresence.guild.roles.cache.get('910192386267840513');
const member = newPresence.member;
const activities = member.user.presence.activities[0];
if (activities && (activities.state.includes('.gg/') || activities.state.includes('discord.gg/'))) {
return newPresence.member.roles.add(role);
}
else if (member.roles.cache.get(role.id)) {
newPresence.member.roles.remove(role);
}});
I found that code but giving errors.
EDIT 2
I changed my code and didn't give errors but doesn't working.
const {Client, ActivityType,GatewayIntentBits,Partials,} = require("discord.js");
const { Client } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildMembers,
],
partials: [
Partials.Channel,
Partials.Message,
Partials.User,
Partials.GuildMember,
Partials.Reaction,
],
});
module.exports = client;
client.on("presenceUpdate", (oldPresence, newPresence) => {
if (newPresence.activities.some(a => a.state && a.state.includes("gg"))) {
console.log(" 1 role gave");
newPresence.member.roles.add("1127983091386962060");
} else {
newPresence.member.roles.remove("1127983091386962060");
}
});
答案1
得分: 0
你的代码中有很多重复和不必要的行!以下是其简化版本:
client.on("presenceUpdate", (oldPresence, newPresence) => {
if (newPresence.activities.some(a => a.state && a.state.includes("discord.gg"))) {
newPresence.member.roles.add("role_id");
} else {
newPresence.member.roles.remove("role_id");
}
});
你可以根据自己的需求随时修改代码。
我建议为使代码正常工作,使用以下意图:
Guilds
GuildMembers
GuildPresences
还请确保在你的开发者门户上启用特权意图!
参考链接:Presence、GuildMemberRoleManager、Client#presenceUpdate。
英文:
You're code has a lot of repetitions and unnecessary lines!
Here is a shortened version of it:
client.on("presenceUpdate", (oldPresence, newPresence) => {
if (newPresence.activities.some(a => a.state && a.state.includes("discord.gg")) {
newPresence.member.roles.add("role_id");
} else {
newPresence.member.roles.remove("role_id");
}
});
You can always modify the code as per your use.
I recommend using the following intents for the code to work:
Guilds
GuildMembers
GuildPresences
Make sure to enable privileged intents on your developer portal too!
Reference: Presence, GuildMemberRoleManager, Client#presenceUpdate.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论