检测当我的 Discord.js 机器人给成员分配角色。

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

Detecting when role is given to a member with my bot discord.js

问题

以下是代码的中文翻译部分:

// 需要必要的 discord.js 类

const { token } = require('./config.json');

// 创建一个新的客户端实例

const Discord = require('discord.js');
const settings = require('./config.json');

const { Client, GatewayIntentBits, Partials } = require('discord.js');
const client = new Client({
  intents: [
    GatewayIntentBits.DirectMessages,
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildBans,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
  ],
  partials: [Partials.Channel],
});

client.on('ready', () => {
   console.log("- 机器人已登录 -");
});
client.on('guildMemberUpdate', (oldMember, newMember) => {
    console.log("已更新")
    const oldRoles = oldMember.roles.cache,
          newRoles = newMember.roles.cache;

    // 是否有该角色?
    const oldHas = oldRoles.has('1093515437792305222'),
          newHas = newRoles.has('1093515437792305222');

    // 检查是否被移除或添加
    if (!oldHas && newHas) {
        console.log("获得角色")

    }
});
client.on('messageCreate', async (message) => {
  console.log("收到消息")
  if (message.content.includes('changeNick')) {
    message.member.setNickname(message.content.replace('changeNick ', ''));
}
});

client.login(settings.token);

请注意,这只是你提供的代码的翻译部分。如果你有其他问题或需要进一步的协助,请随时提问。

英文:

I am trying to detect when a user is given a certain role on a server.

My code is:

// Require the necessary discord.js classes

const { token } = require('./config.json');

// Create a new client instance

const Discord = require('discord.js');
const settings = require('./config.json');

const { Client, GatewayIntentBits, Partials } = require('discord.js');
const client = new Client({
  intents: [
    GatewayIntentBits.DirectMessages,
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildBans,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
  ],
  partials: [Partials.Channel],
});

client.on('ready', () => {
   console.log("- Bot logged in -");
});
client.on('guildMemberUpdate', (oldMember, newMember) => {
    console.log("updated")
    const oldRoles = oldMember.roles.cache,
          newRoles = newMember.roles.cache;

    // Has Role?
    const oldHas = oldRoles.has('1093515437792305222'),
          newHas = newRoles.has('1093515437792305222');

    // Check if removed or added
    if (!oldHas && newHas) {
        console.log("got role")

    }
});
client.on('messageCreate', async (message) => {
  console.log("got message")
  if (message.content.includes('changeNick')) {
    message.member.setNickname(message.content.replace('changeNick ', ''));
}
});

client.login(settings.token);

Everything works, apart from the role update. I have tried adding intentions, but maybe didn't set the right ones?

答案1

得分: 1

guildMemberUpdate 事件需要 GuildsGuildMembersGuildPresences 意图才能触发:

const client = new Client({
  intents: [
    GatewayIntentBits.DirectMessages,
    GatewayIntentBits.GuildBans,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.GuildPresences,
    GatewayIntentBits.Guilds,
    GatewayIntentBits.MessageContent,
  ],
  partials: [Partials.Channel],
});
英文:

The guildMemberUpdate event will need the Guilds, GuildMembers and GuildPresences intents to fire:

const client = new Client({
  intents: [
    GatewayIntentBits.DirectMessages,
    GatewayIntentBits.GuildBans,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.GuildPresences,
    GatewayIntentBits.Guilds,
    GatewayIntentBits.MessageContent,
  ],
  partials: [Partials.Channel],
});

huangapple
  • 本文由 发表于 2023年4月6日 21:04:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949861.html
匿名

发表评论

匿名网友

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

确定