I have been working on a bot for 12 days now and I need to upgrade to v12. Everything was going smoothly until I tried a command that will give a specified role to a mentioned user.
I have 2 variables. One is a role variable that the user has specified and the member variable (which is equaled to message.guild.members.get(args[0])).
The code looks like this:
let Member = message.mentions.members.first() || message.guild.members.get(arguments[0]);
if (!Member) return message.channel.send("Error: Either the member does not exist or you haven't put the member to give the role to!");
let role = message.guild.roles.cache.some(r => r.name === arguments[1]) || message.guild.roles.cache.some(r => r.id === arguments[1]) || message.mentions.roles.first();
if (!role) return nessage.channel.send("Error: Please provide a valid role!");
// if (role === "undefined") {
// let role = message.guild.roles.cache.some(r => r.name === arguments[1]) || message.guild.roles.cache.some(r => r.id === arguments[1]) || message.mentions.roles.first();
// }
if (Member.roles.cache.has(role.id)) {
return message.channel.send("Error: The mentioned user already has that role!");
} else {
let role = message.guild.roles.cache.some(r => r.name === arguments[1]) || message.guild.roles.cache.some(r => r.id === arguments[1]) || message.mentions.roles.first();
console.log(message);
console.log("role" + role);
Member.roles.add(role.id).catch(e => console.log("Error: " + e.message));
}
The code looked fine to me and expected it to work, but then I got an error in the console. It doesn't say what variable is not defined, it just says it's undefined.
I tried checking if the role variable is undefined, but I commented out since it didn't work. Then, I have let it print the message and check if the role is undefined. The role variable is fine. The message variable is fine. Even the Member variable is fine.
I use the latest Node.js.
Here's the console log:
Message {
channel: TextChannel {
type: 'text',
deleted: false,
id: '729663938148565134',
name: 'general',
rawPosition: 0,
parentID: '729663938148565132',
permissionOverwrites: Collection [Map] {},
topic: 'Beware, there will be a lot of mentions.',
nsfw: undefined,
lastMessageID: '733951355240316948',
rateLimitPerUser: 0,
lastPinTimestamp: 1594845448699,
guild: Guild {
members: [GuildMemberManager],
channels: [GuildChannelManager],
roles: [RoleManager],
presences: [PresenceManager],
voiceStates: [VoiceStateManager],
deleted: false,
available: true,
id: '729663937557168181',
shardID: 0,
name: 'Bot Testing',
icon: null,
splash: null,
region: 'europe',
memberCount: 6,
large: false,
features: [],
applicationID: null,
afkTimeout: 300,
afkChannelID: null,
systemChannelID: '729663938148565134',
embedEnabled: undefined,
premiumTier: 0,
premiumSubscriptionCount: 0,
verificationLevel: 'MEDIUM',
explicitContentFilter: 'DISABLED',
mfaLevel: 0,
joinedTimestamp: 1594035922599,
defaultMessageNotifications: 'ALL',
systemChannelFlags: [SystemChannelFlags],
vanityURLCode: null,
description: null,
banner: null,
rulesChannelID: null,
publicUpdatesChannelID: null,
ownerID: '402159567200583680',
emojis: [GuildEmojiManager]
},
messages: MessageManager {
cacheType: [Function: LimitedCollection],
cache: [LimitedCollection [Map]],
channel: [Circular]
},
_typing: Map { '402159567200583680' => [Object] }
},
deleted: false,
id: '733951355240316948',
type: 'DEFAULT',
content: '>giveRole <@!655895332722442282> Mod',
author: User {
id: '402159567200583680',
bot: false,
username: '????????KFM????????',
discriminator: '3147',
avatar: '5f322e4ab3ec14e9fa83e1c77d8bb36d',
lastMessageID: '733951355240316948',
lastMessageChannelID: '729663938148565134',
flags: UserFlags { bitfield: 0 }
},
pinned: false,
tts: false,
nonce: '733951341017169920',
system: false,
embeds: [],
attachments: Collection [Map] {},
createdTimestamp: 1595058039246,
editedTimestamp: null,
reactions: ReactionManager {
cacheType: [Function: Collection],
cache: Collection [Map] {},
message: [Circular]
},
mentions: MessageMentions {
everyone: false,
users: Collection [Map] { '655895332722442282' => [User] },
roles: Collection [Map] {},
_members: Collection [Map] { '655895332722442282' => [GuildMember] },
_channels: null,
crosspostedChannels: Collection [Map] {}
},
webhookID: null,
application: null,
activity: null,
_edits: [],
flags: MessageFlags { bitfield: 0 },
reference: null
}
roletrue
Error: undefined
Thanks in advance!
EDIT: It looks like the role.id is undefined. I will try to see how to fix that.
Error: undefined
at the end of ur console log? if so , it just means that ur exception has no message defined!!! – AppleCiderGuy