读取服务器成员的状态并添加/移除角色,discord.js v14

huangapple go评论81阅读模式
英文:

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

还请确保在你的开发者门户上启用特权意图!

参考链接:PresenceGuildMemberRoleManagerClient#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.

huangapple
  • 本文由 发表于 2023年7月11日 14:58:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76659388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定