我正在尝试为我的私人Discord JS Bot做Autorole函数

时间:2019-10-28 13:02:35

标签: javascript node.js discord discord.js

所以我试图用Discord JS做一个Autorole函数,但是问题是我遇到了一个很大的错误,我不知道为什么。那就是我使用的代码:

client.on('guildMemberAdd', member => {
    console.log(`[${b}:${c}` + member.user.tag + 'ist dem Server beigetreten!');

    var role = member.guild.roles.find('name', 'user');
    member.addRole(role);
    member.send('', Embeds.autorole)
});

(我已经在var中定义了角色)

错误是:

[${b}:${c}]DoomCity#3747ist dem Server beigetreten!
(node:20220) DeprecationWarning: Collection#find: pass a function instead
(node:20220) UnhandledPromiseRejectionWarning: TypeError: Supplied parameter was neither a Role nor a Snowflake.
    at GuildMember.addRole (D:\FVKR\Desktop\Team Unknown\Discord Bot\node_modules\discord.js\src\structures\GuildMember.js:454:38)
    at Client.<anonymous> (D:\FVKR\Desktop\Team Unknown\Discord Bot\index.js:198:12)
    at Client.emit (events.js:209:13)
    at Guild._addMember (D:\FVKR\Desktop\Team Unknown\Discord Bot\node_modules\discord.js\src\structures\Guild.js:1298:19)
    at GuildMemberAddHandler.handle (D:\FVKR\Desktop\Team Unknown\Discord Bot\node_modules\discord.js\src\client\websocket\packets\handlers\GuildMemberAdd.js:12:13)
    at WebSocketPacketManager.handle (D:\FVKR\Desktop\Team Unknown\Discord Bot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65)
    at WebSocketConnection.onPacket (D:\FVKR\Desktop\Team Unknown\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (D:\FVKR\Desktop\Team Unknown\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (D:\FVKR\Desktop\Team Unknown\Discord Bot\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:209:13)
(node:20220) 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:20220) [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.
(node:20220) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message
    at D:\FVKR\Desktop\Team Unknown\Discord Bot\node_modules\discord.js\src\client\rest\RequestHandlers\Sequential.js:85:15
    at D:\FVKR\Desktop\Team Unknown\Discord Bot\node_modules\snekfetch\src\index.js:215:21
    at processTicksAndRejections (internal/process/task_queues.js:85:5)
(node:20220) 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: 3)

我不知道如何解决

1 个答案:

答案 0 :(得分:1)

要删除DeprecationWarning: Collection#find: pass a function instead警告,您需要使用:

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

代替:

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

对于TypeError: Supplied parameter was neither a Role nor a Snowflake.,我认为这是因为找不到该角色。尝试在变量之后添加console.log

然后,对于DiscordAPIError: Cannot send an empty message错误,这是因为您无法发送空字符串。使用:

member.send(Embeds.autorole);

代替:

member.send('', Embeds.autorole);