我无法解决该问题,该如何解决?

时间:2019-09-10 10:31:24

标签: javascript discord.js

我写了一个代码,我在stackOverflow和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:\Users\matti\Downloads\discordbot\node_modules\discord.js\src\structures\GuildMember.js:452:38)
    at CommandoClient.bot.on.member (C:\Users\matti\Downloads\discordbot\index.js:41:12)
    at CommandoClient.emit (events.js:198:13)
    at Guild._addMember (C:\Users\matti\Downloads\discordbot\node_modules\discord.js\src\structures\Guild.js:1192:19)
    at GuildMemberAddHandler.handle (C:\Users\matti\Downloads\discordbot\node_modules\discord.js\src\client\websocket\packets\handlers\GuildMemberAdd.js:12:13)
    at WebSocketPacketManager.handle (C:\Users\matti\Downloads\discordbot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (C:\Users\matti\Downloads\discordbot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\matti\Downloads\discordbot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\matti\Downloads\discordbot\node_modules\ws\lib\event-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.

2 个答案:

答案 0 :(得分:0)

var role = member.guild.roles.find('name', 'user');

只需将其更改为

var role = member.guild.roles.find(role => role.name === 'user');

答案 1 :(得分:0)

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 函数时,您也没有定义要添加的角色。