英文:
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
事件需要 Guilds
、GuildMembers
和 GuildPresences
意图才能触发:
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],
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论