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

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

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

问题

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

  1. // 需要必要的 discord.js 类
  2. const { token } = require('./config.json');
  3. // 创建一个新的客户端实例
  4. const Discord = require('discord.js');
  5. const settings = require('./config.json');
  6. const { Client, GatewayIntentBits, Partials } = require('discord.js');
  7. const client = new Client({
  8. intents: [
  9. GatewayIntentBits.DirectMessages,
  10. GatewayIntentBits.Guilds,
  11. GatewayIntentBits.GuildBans,
  12. GatewayIntentBits.GuildMessages,
  13. GatewayIntentBits.MessageContent,
  14. ],
  15. partials: [Partials.Channel],
  16. });
  17. client.on('ready', () => {
  18. console.log("- 机器人已登录 -");
  19. });
  20. client.on('guildMemberUpdate', (oldMember, newMember) => {
  21. console.log("已更新")
  22. const oldRoles = oldMember.roles.cache,
  23. newRoles = newMember.roles.cache;
  24. // 是否有该角色?
  25. const oldHas = oldRoles.has('1093515437792305222'),
  26. newHas = newRoles.has('1093515437792305222');
  27. // 检查是否被移除或添加
  28. if (!oldHas && newHas) {
  29. console.log("获得角色")
  30. }
  31. });
  32. client.on('messageCreate', async (message) => {
  33. console.log("收到消息")
  34. if (message.content.includes('changeNick')) {
  35. message.member.setNickname(message.content.replace('changeNick ', ''));
  36. }
  37. });
  38. client.login(settings.token);

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

英文:

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

My code is:

  1. // Require the necessary discord.js classes
  2. const { token } = require('./config.json');
  3. // Create a new client instance
  4. const Discord = require('discord.js');
  5. const settings = require('./config.json');
  6. const { Client, GatewayIntentBits, Partials } = require('discord.js');
  7. const client = new Client({
  8. intents: [
  9. GatewayIntentBits.DirectMessages,
  10. GatewayIntentBits.Guilds,
  11. GatewayIntentBits.GuildBans,
  12. GatewayIntentBits.GuildMessages,
  13. GatewayIntentBits.MessageContent,
  14. ],
  15. partials: [Partials.Channel],
  16. });
  17. client.on('ready', () => {
  18. console.log("- Bot logged in -");
  19. });
  20. client.on('guildMemberUpdate', (oldMember, newMember) => {
  21. console.log("updated")
  22. const oldRoles = oldMember.roles.cache,
  23. newRoles = newMember.roles.cache;
  24. // Has Role?
  25. const oldHas = oldRoles.has('1093515437792305222'),
  26. newHas = newRoles.has('1093515437792305222');
  27. // Check if removed or added
  28. if (!oldHas && newHas) {
  29. console.log("got role")
  30. }
  31. });
  32. client.on('messageCreate', async (message) => {
  33. console.log("got message")
  34. if (message.content.includes('changeNick')) {
  35. message.member.setNickname(message.content.replace('changeNick ', ''));
  36. }
  37. });
  38. 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 意图才能触发:

  1. const client = new Client({
  2. intents: [
  3. GatewayIntentBits.DirectMessages,
  4. GatewayIntentBits.GuildBans,
  5. GatewayIntentBits.GuildMembers,
  6. GatewayIntentBits.GuildMessages,
  7. GatewayIntentBits.GuildPresences,
  8. GatewayIntentBits.Guilds,
  9. GatewayIntentBits.MessageContent,
  10. ],
  11. partials: [Partials.Channel],
  12. });
英文:

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

  1. const client = new Client({
  2. intents: [
  3. GatewayIntentBits.DirectMessages,
  4. GatewayIntentBits.GuildBans,
  5. GatewayIntentBits.GuildMembers,
  6. GatewayIntentBits.GuildMessages,
  7. GatewayIntentBits.GuildPresences,
  8. GatewayIntentBits.Guilds,
  9. GatewayIntentBits.MessageContent,
  10. ],
  11. partials: [Partials.Channel],
  12. });

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:

确定