我如何解决这个添加角色问题,因为我找不到方法?



我写了一个代码,我在堆栈溢出和idk的某个地方找到了如何修复此错误 bc 每当有人加入时,我都会收到此错误,但它没有给任何人角色

我试图更改命令,我试图寻求帮助,但没有人可以帮助我

});
bot.on('guildMemberAdd', member => {
console.log('User' + member.user.tag + 'has joined the server!');
var role = member.guild.roles.find('name', 'user');
member.addRole();
});

IDK如何解决

Useranimeclone#0900has joined the server!
(node:4716) DeprecationWarning: Collection#find: pass a function instead
at GuildMember.addRole (C:UsersmattiDownloadsdiscordbotnode_modulesdiscord.jssrcstructuresGuildMember.js:452:38)
at CommandoClient.bot.on.member (C:UsersmattiDownloadsdiscordbotindex.js:41:12)
at CommandoClient.emit (events.js:198:13)
at Guild._addMember (C:UsersmattiDownloadsdiscordbotnode_modulesdiscord.jssrcstructuresGuild.js:1192:19)
at GuildMemberAddHandler.handle (C:UsersmattiDownloadsdiscordbotnode_modulesdiscord.jssrcclientwebsocketpacketshandlersGuildMemberAdd.js:12:13)
at WebSocketPacketManager.handle (C:UsersmattiDownloadsdiscordbotnode_modulesdiscord.jssrcclientwebsocketpacketsWebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (C:UsersmattiDownloadsdiscordbotnode_modulesdiscord.jssrcclientwebsocketWebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (C:UsersmattiDownloadsdiscordbotnode_modulesdiscord.jssrcclientwebsocketWebSocketConnection.js:296:17)
at WebSocket.onMessage (C:UsersmattiDownloadsdiscordbotnode_moduleswslibevent-target.js:120:16)
at WebSocket.emit (events.js:198:13)
(node:4716) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)(node:4716) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
var role = member.guild.roles.find('name', 'user');

只需将其更改为

var role = member.guild.roles.find(role => role.name === 'user');
bot.on('guildMemberAdd', member => {
console.log(`User: ${member.user.tag} has joined the server!`); //Logs to console
var role = member.guild.roles.get('user'); //Grab role from server.
member.addRole(role); //Define role to add
});

你应该使用get((,因为它可以通过名称/ID 获取角色,并使你的代码更干净。

我使用模板文字进行日志记录:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

此外,在调用addRole函数时,您没有定义要添加的角色。

最新更新