Discord.js : TypeError: Cannot read property 'remove' of undefined
Discord.js : TypeError: Cannot read property 'remove' of undefined
我无法从 message.mentions.roles.first() 和 message.mentions.members.first()
获取正确的信息
这是我在 Index.js
中的命令处理程序
bot.on('message', async (message, guild, helpList, roles) => {
if (!message.content.startsWith(prefix) || message.author.bot || !message.member.hasPermission('MANAGE_ROLES')) return;
const args = message.content.slice(prefix.length).split(' ') ;
const command = args.shift().toLowerCase();
const aRole = message.mentions.roles.first();
const Role = aRole.id
const name = args.join(' ');
const User = message.mentions.members.first();
if (!Role && command !== 'create' && command !== 'give' && command !== 'take' && command !== 'help' && command !== 'list') {return message.channel.send('Sorry, that role was not found.')}
if (!User && (command == 'give' || command == 'take') ) {return message.reply ("That name doesnt match a user.");}
try{
bot.commands.get(command).execute(message, Role, name, User, guild, helpList);
} catch(e){
console.log(e)
}
});
bot.commands.get(command).execute(message, Role, name, User, guild, helpList);
} catch(e){
console.log(e)
}
});
这是我的 take.js,一个从指定成员中删除角色的命令。
module.exports = {
name: 'take',
description: '',
execute(message, Role, User, roles){
try{
User.roles.remove(Role);
return message.channel.send (`${Role} has been removed from ${User}'s list of rolls.`);
}catch(e){
console.log(e);
console.log(Role)
console.log(User)
}}}
这是我得到的错误。
TypeError: Cannot read property 'remove' of undefined
at Object.execute (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/commands/take.js:6:16)
at Client.<anonymous> (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/index.js:34:33)
at Client.emit (events.js:310:20)
at MessageCreateAction.handle (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
at WebSocketShard.onPacket (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:437:22)
at WebSocketShard.onMessage (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:294:10)
at WebSocket.onMessage (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/ws/lib/event-target.js:125:16)
at WebSocket.emit (events.js:310:20)
711442214043254854
<@&711442214043254854> <@!271438275619586062>
为什么角色不起作用?任何帮助是极大的赞赏。
实际上在您调用 execute
函数时的代码中
bot.commands.get(command).execute(message, Role, name, User, guild, helpList);
take.js
中的 execute
函数将等待这些参数:
- [参数] => [收到的变量类型]
- 留言 => Message
- 角色 => 字符串(Role 提及的 id)
- User => String (不是预期的 GuildMember)
- 公会 => 未定义
- 帮助列表 => 未定义
实际上,按照您为 execute
函数提供参数的顺序,您提供的是变量 name
而不是 User
。因此,User
不是 GuildMember,而是一个字符串,当然 String.roles
是 undefined
,这就是错误显示 cannot read property 'remove' of undefined
.
的原因
要修复您的错误,您应该修改
bot.commands.get(command).execute(message, Role, name, User, guild, helpList);
至
bot.commands.get(command).execute(message, Role, User, name, guild, helpList);
(仔细看,我把name
参数和User
参数倒过来了)
等等,还没完
代码中的另一个问题是您声明了太多参数。
bot.on("message", callback(Message))
只会用一个参数 (see docs here) 调用 callback()
。
guild, helpList, roles
总是 undefined
作为这里的参数。
所以你应该删除那些不必要的参数:
bot.on("message", (message) => {
...
}
与 execute
函数调用相同,删除未使用的参数:
bot.commands.get(command).execute(message, Role, User);
我想就是这样。我希望我能被理解 :D
我无法从 message.mentions.roles.first() 和 message.mentions.members.first()
获取正确的信息这是我在 Index.js
中的命令处理程序bot.on('message', async (message, guild, helpList, roles) => {
if (!message.content.startsWith(prefix) || message.author.bot || !message.member.hasPermission('MANAGE_ROLES')) return;
const args = message.content.slice(prefix.length).split(' ') ;
const command = args.shift().toLowerCase();
const aRole = message.mentions.roles.first();
const Role = aRole.id
const name = args.join(' ');
const User = message.mentions.members.first();
if (!Role && command !== 'create' && command !== 'give' && command !== 'take' && command !== 'help' && command !== 'list') {return message.channel.send('Sorry, that role was not found.')}
if (!User && (command == 'give' || command == 'take') ) {return message.reply ("That name doesnt match a user.");}
try{
bot.commands.get(command).execute(message, Role, name, User, guild, helpList);
} catch(e){
console.log(e)
}
});
bot.commands.get(command).execute(message, Role, name, User, guild, helpList);
} catch(e){
console.log(e)
}
});
这是我的 take.js,一个从指定成员中删除角色的命令。
module.exports = {
name: 'take',
description: '',
execute(message, Role, User, roles){
try{
User.roles.remove(Role);
return message.channel.send (`${Role} has been removed from ${User}'s list of rolls.`);
}catch(e){
console.log(e);
console.log(Role)
console.log(User)
}}}
这是我得到的错误。
TypeError: Cannot read property 'remove' of undefined
at Object.execute (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/commands/take.js:6:16)
at Client.<anonymous> (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/index.js:34:33)
at Client.emit (events.js:310:20)
at MessageCreateAction.handle (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
at WebSocketShard.onPacket (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:437:22)
at WebSocketShard.onMessage (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:294:10)
at WebSocket.onMessage (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/ws/lib/event-target.js:125:16)
at WebSocket.emit (events.js:310:20)
711442214043254854
<@&711442214043254854> <@!271438275619586062>
为什么角色不起作用?任何帮助是极大的赞赏。
实际上在您调用 execute
函数时的代码中
bot.commands.get(command).execute(message, Role, name, User, guild, helpList);
take.js
中的 execute
函数将等待这些参数:
- [参数] => [收到的变量类型]
- 留言 => Message
- 角色 => 字符串(Role 提及的 id)
- User => String (不是预期的 GuildMember)
- 公会 => 未定义
- 帮助列表 => 未定义
实际上,按照您为 execute
函数提供参数的顺序,您提供的是变量 name
而不是 User
。因此,User
不是 GuildMember,而是一个字符串,当然 String.roles
是 undefined
,这就是错误显示 cannot read property 'remove' of undefined
.
要修复您的错误,您应该修改
bot.commands.get(command).execute(message, Role, name, User, guild, helpList);
至
bot.commands.get(command).execute(message, Role, User, name, guild, helpList);
(仔细看,我把name
参数和User
参数倒过来了)
等等,还没完
代码中的另一个问题是您声明了太多参数。
bot.on("message", callback(Message))
只会用一个参数 (see docs here) 调用 callback()
。
guild, helpList, roles
总是 undefined
作为这里的参数。
所以你应该删除那些不必要的参数:
bot.on("message", (message) => {
...
}
与 execute
函数调用相同,删除未使用的参数:
bot.commands.get(command).execute(message, Role, User);
我想就是这样。我希望我能被理解 :D