How to show roles of user discord.js / userinfo command
I'm trying to make a 'userinfo' command, and I'm currently stuck on showing roles of the user.
Here's my code:
const Discord = module.require('discord.js');
const moment = require('moment');
module.exports.run = async (bot, message, args) =>
let user = message.mentions.users.first()
module.exports.help =
name: 'userinfo'
I'm getting error TypeError: Cannot read property 'map' of undefined and I don't know how to fix it.
Thanks for help. <3
javascript bots discord discord.js
add a comment |
I'm trying to make a 'userinfo' command, and I'm currently stuck on showing roles of the user.
Here's my code:
const Discord = module.require('discord.js');
const moment = require('moment');
module.exports.run = async (bot, message, args) =>
let user = message.mentions.users.first()
module.exports.help =
name: 'userinfo'
I'm getting error TypeError: Cannot read property 'map' of undefined and I don't know how to fix it.
Thanks for help. <3
javascript bots discord discord.js
add a comment |
I'm trying to make a 'userinfo' command, and I'm currently stuck on showing roles of the user.
Here's my code:
const Discord = module.require('discord.js');
const moment = require('moment');
module.exports.run = async (bot, message, args) =>
let user = message.mentions.users.first()
module.exports.help =
name: 'userinfo'
I'm getting error TypeError: Cannot read property 'map' of undefined and I don't know how to fix it.
Thanks for help. <3
javascript bots discord discord.js
I'm trying to make a 'userinfo' command, and I'm currently stuck on showing roles of the user.
Here's my code:
const Discord = module.require('discord.js');
const moment = require('moment');
module.exports.run = async (bot, message, args) =>
let user = message.mentions.users.first()
module.exports.help =
name: 'userinfo'
I'm getting error TypeError: Cannot read property 'map' of undefined and I don't know how to fix it.
Thanks for help. <3
javascript bots discord discord.js
javascript bots discord discord.js
edited Nov 11 '18 at 21:32
barbsan
2,45131223
2,45131223
asked Nov 11 '18 at 21:21
Silent WatcherSilent Watcher
31
31
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
User.roles
is undefined
because that property doesn't exist: try using GuildMember.roles
instead:
let member = message.mentions.members.first() || message.member,
user = member.user;
let embed = new Discord.RichEmbed()
// ... all the other stuff ...
.addField('Roles:', member.roles.map(r => `$r`).join(' | '), true)
The other properties will still use user
, but .roles
will be related to the GuildMember.
Thank you, your suggestion to use 'GuildMember' worked. <3 :D
– Silent Watcher
Nov 12 '18 at 15:40
Happy to hear that :)
– Federico Grandi
Nov 12 '18 at 17:19
add a comment |
TypeError: Cannot read property 'map' of undefined - this means that somewhere in your code happening a situation of execution .map
function of undefined
variable.
You have only one map
. Here:
.addField('Roles:', user.roles.map(r => `$r`).join(' | '), true)
// ^^^
This line trows this error, so this means that user.roles
is undefined
.
Try to:
let user = message.mentions.users.first() || message.author;
console.log(user.roles); // it is undefined
add a comment |
Propably user.roles
is undefined
.
If it's ok that user has no roles, you can replace:
.addField('Roles:', user.roles.map(r => `$r`).join(' | '), true)
with:
.addField('Roles:', user.roles ? user.roles.map(r => `$r`).join(' | ') : "", true)
This will set empty string in case when there's no roles
property in user
object.
Another option is to set user.roles
to empty array if it doesn't exist (or do something else in this if
, i.e. throw error):
let user = /* get user*/
if (!user.roles)
user.roles = ;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253350%2fhow-to-show-roles-of-user-discord-js-userinfo-command%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
User.roles
is undefined
because that property doesn't exist: try using GuildMember.roles
instead:
let member = message.mentions.members.first() || message.member,
user = member.user;
let embed = new Discord.RichEmbed()
// ... all the other stuff ...
.addField('Roles:', member.roles.map(r => `$r`).join(' | '), true)
The other properties will still use user
, but .roles
will be related to the GuildMember.
Thank you, your suggestion to use 'GuildMember' worked. <3 :D
– Silent Watcher
Nov 12 '18 at 15:40
Happy to hear that :)
– Federico Grandi
Nov 12 '18 at 17:19
add a comment |
User.roles
is undefined
because that property doesn't exist: try using GuildMember.roles
instead:
let member = message.mentions.members.first() || message.member,
user = member.user;
let embed = new Discord.RichEmbed()
// ... all the other stuff ...
.addField('Roles:', member.roles.map(r => `$r`).join(' | '), true)
The other properties will still use user
, but .roles
will be related to the GuildMember.
Thank you, your suggestion to use 'GuildMember' worked. <3 :D
– Silent Watcher
Nov 12 '18 at 15:40
Happy to hear that :)
– Federico Grandi
Nov 12 '18 at 17:19
add a comment |
User.roles
is undefined
because that property doesn't exist: try using GuildMember.roles
instead:
let member = message.mentions.members.first() || message.member,
user = member.user;
let embed = new Discord.RichEmbed()
// ... all the other stuff ...
.addField('Roles:', member.roles.map(r => `$r`).join(' | '), true)
The other properties will still use user
, but .roles
will be related to the GuildMember.
User.roles
is undefined
because that property doesn't exist: try using GuildMember.roles
instead:
let member = message.mentions.members.first() || message.member,
user = member.user;
let embed = new Discord.RichEmbed()
// ... all the other stuff ...
.addField('Roles:', member.roles.map(r => `$r`).join(' | '), true)
The other properties will still use user
, but .roles
will be related to the GuildMember.
answered Nov 12 '18 at 15:04
Federico GrandiFederico Grandi
3,16621229
3,16621229
Thank you, your suggestion to use 'GuildMember' worked. <3 :D
– Silent Watcher
Nov 12 '18 at 15:40
Happy to hear that :)
– Federico Grandi
Nov 12 '18 at 17:19
add a comment |
Thank you, your suggestion to use 'GuildMember' worked. <3 :D
– Silent Watcher
Nov 12 '18 at 15:40
Happy to hear that :)
– Federico Grandi
Nov 12 '18 at 17:19
Thank you, your suggestion to use 'GuildMember' worked. <3 :D
– Silent Watcher
Nov 12 '18 at 15:40
Thank you, your suggestion to use 'GuildMember' worked. <3 :D
– Silent Watcher
Nov 12 '18 at 15:40
Happy to hear that :)
– Federico Grandi
Nov 12 '18 at 17:19
Happy to hear that :)
– Federico Grandi
Nov 12 '18 at 17:19
add a comment |
TypeError: Cannot read property 'map' of undefined - this means that somewhere in your code happening a situation of execution .map
function of undefined
variable.
You have only one map
. Here:
.addField('Roles:', user.roles.map(r => `$r`).join(' | '), true)
// ^^^
This line trows this error, so this means that user.roles
is undefined
.
Try to:
let user = message.mentions.users.first() || message.author;
console.log(user.roles); // it is undefined
add a comment |
TypeError: Cannot read property 'map' of undefined - this means that somewhere in your code happening a situation of execution .map
function of undefined
variable.
You have only one map
. Here:
.addField('Roles:', user.roles.map(r => `$r`).join(' | '), true)
// ^^^
This line trows this error, so this means that user.roles
is undefined
.
Try to:
let user = message.mentions.users.first() || message.author;
console.log(user.roles); // it is undefined
add a comment |
TypeError: Cannot read property 'map' of undefined - this means that somewhere in your code happening a situation of execution .map
function of undefined
variable.
You have only one map
. Here:
.addField('Roles:', user.roles.map(r => `$r`).join(' | '), true)
// ^^^
This line trows this error, so this means that user.roles
is undefined
.
Try to:
let user = message.mentions.users.first() || message.author;
console.log(user.roles); // it is undefined
TypeError: Cannot read property 'map' of undefined - this means that somewhere in your code happening a situation of execution .map
function of undefined
variable.
You have only one map
. Here:
.addField('Roles:', user.roles.map(r => `$r`).join(' | '), true)
// ^^^
This line trows this error, so this means that user.roles
is undefined
.
Try to:
let user = message.mentions.users.first() || message.author;
console.log(user.roles); // it is undefined
edited Nov 11 '18 at 21:44
answered Nov 11 '18 at 21:33
qiAlexqiAlex
2,0261724
2,0261724
add a comment |
add a comment |
Propably user.roles
is undefined
.
If it's ok that user has no roles, you can replace:
.addField('Roles:', user.roles.map(r => `$r`).join(' | '), true)
with:
.addField('Roles:', user.roles ? user.roles.map(r => `$r`).join(' | ') : "", true)
This will set empty string in case when there's no roles
property in user
object.
Another option is to set user.roles
to empty array if it doesn't exist (or do something else in this if
, i.e. throw error):
let user = /* get user*/
if (!user.roles)
user.roles = ;
add a comment |
Propably user.roles
is undefined
.
If it's ok that user has no roles, you can replace:
.addField('Roles:', user.roles.map(r => `$r`).join(' | '), true)
with:
.addField('Roles:', user.roles ? user.roles.map(r => `$r`).join(' | ') : "", true)
This will set empty string in case when there's no roles
property in user
object.
Another option is to set user.roles
to empty array if it doesn't exist (or do something else in this if
, i.e. throw error):
let user = /* get user*/
if (!user.roles)
user.roles = ;
add a comment |
Propably user.roles
is undefined
.
If it's ok that user has no roles, you can replace:
.addField('Roles:', user.roles.map(r => `$r`).join(' | '), true)
with:
.addField('Roles:', user.roles ? user.roles.map(r => `$r`).join(' | ') : "", true)
This will set empty string in case when there's no roles
property in user
object.
Another option is to set user.roles
to empty array if it doesn't exist (or do something else in this if
, i.e. throw error):
let user = /* get user*/
if (!user.roles)
user.roles = ;
Propably user.roles
is undefined
.
If it's ok that user has no roles, you can replace:
.addField('Roles:', user.roles.map(r => `$r`).join(' | '), true)
with:
.addField('Roles:', user.roles ? user.roles.map(r => `$r`).join(' | ') : "", true)
This will set empty string in case when there's no roles
property in user
object.
Another option is to set user.roles
to empty array if it doesn't exist (or do something else in this if
, i.e. throw error):
let user = /* get user*/
if (!user.roles)
user.roles = ;
answered Nov 11 '18 at 22:43
barbsanbarbsan
2,45131223
2,45131223
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253350%2fhow-to-show-roles-of-user-discord-js-userinfo-command%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown