我想要的是为用户设置一个角色(如果他/她开始流式传输),并在#streaming中发送消息。但我不断收到TypeError: Cannot read 'add' of undefined
client.on('presenceUpdate', (oldMember, newMember) => {
const guild = newMember.guild;
const streamingRole = guild.roles.cache.find(role => role.id === 'streamer');
if (newMember.user.bot) return;
if (newMember.user.presence.activities.streaming) { // Started playing.
let announcement = `Ebrywan ${newMember} just started streaming`;
let channelID = client.channels.cache.find(channel => channel.name.toLowerCase() === "streaming");
if (channelID) channelID.send(announcement);
newMember.roles.add(streamingRole)
console.log(`${streamingRole.name} added to ${newMember.user.tag}`)
}
});
答案 0 :(得分:0)
从我从文档newMember可以看到的是存在,不是用户,您也可以添加角色。试试:
newMember.member.roles.add(streamingRole)
.member将给您成员,您可以获取成员的角色+也应该添加新角色
状态:https://discord.js.org/#/docs/main/stable/class/Presence 成员:https://discord.js.org/#/docs/main/stable/class/GuildMember