I just asked a question for something similar to this today, however, I got the answer and came across a new problem. I am creating a user info command for a discord bot, and if the user is not playing a game, the bot will crash.
this link contains images of the console log when a user is in-game and when a user is not in-game. You just have to read the descriptions of the images. https://imgur.com/a/ZLL24FY
code:
const Discord = require('discord.js');
const moment = require('moment');
module.exports = {
name: "userinfo",
aliases: ['ui', 'user', 'uinfo'],
category: "info",
description: "shows information of the user you mentioned",
usage: "[command | alias]",
run: async (client, message, args) => {
const user = message.mentions.users.first() || message.author;
const member = message.mentions.members.first() || message.member;
const joinDiscord = moment(user.createdAt).format('llll');
const joinServer = moment(user.joinedAt).format('llll');
var presence = user.presence.activities.filter(x=>x.type === "PLAYING")
console.log(presence);
const embed = new Discord.MessageEmbed()
.setColor('#724e72')
.setTitle(`**Information for** ${user.tag}`)
.setAuthor(`${message.author.tag}`, message.author.displayAvatarURL())
.setDescription('Shows information for mentioned user')
.setThumbnail(user.displayAvatarURL())
.addField(`${user.tag}`, `${user}`, true)
//.addField("ID:", `${user.id}`, true)
.addField("Nickname:", `${member.displayName}`, true)
.addField("Status:", `${user.presence.status}`, true)
//.addField("In Server", message.guild.name, true)
.addField("Game:", `${presence}`, true)
.addField("Bot:", `${user.bot}`, true)
.addField("Joined The Server On:", `${moment.utc(message.member.joinedAt).format("dddd, MMMM Do YYYY")}`)
.addField("Account Created On:", `${moment.utc(user.createdAt).format("dddd, MMMM Do YYYY")}`, true)
.addField("Roles:", member.roles.cache.map(r => '`'+r.name+'`').slice(0,-1))
.setTimestamp()
.setFooter('a')
message.channel.send(embed);
}
}
If anybody can help out, thank you. I have not used discord.js or JavaScript in at least a year so any help is appreciated.
When the user is not in a game then presence is an empty array. When an empty array is stringified then it is converted into an empty string. This means that when you do this
.addField("Game:", `${presence}`, true)
it is adding a field with an empty value to the embed as indicated in the error. You can either do something like this:
/* ... */
.addField("Game:", `${presence}`||'None', true)
or just check if presence is empty before adding it to the embed like this
/* ... */
const embed = new Discord.MessageEmbed()
.setColor('#724e72')
.setTitle(`**Information for** ${user.tag}`)
.setAuthor(`${message.author.tag}`, message.author.displayAvatarURL())
.setDescription('Shows information for mentioned user')
.setThumbnail(user.displayAvatarURL())
.addField(`${user.tag}`, `${user}`, true)
//.addField("ID:", `${user.id}`, true)
.addField("Nickname:", `${member.displayName}`, true)
.addField("Status:", `${user.presence.status}`, true)
//.addField("In Server", message.guild.name, true)
if (presence) embed.addField("Game:", `${presence}`, true)
embed.addField("Bot:", `${user.bot}`, true)
.addField("Joined The Server On:", `${moment.utc(message.member.joinedAt).format("dddd, MMMM Do YYYY")}`)
.addField("Account Created On:", `${moment.utc(user.createdAt).format("dddd, MMMM Do YYYY")}`, true)
.addField("Roles:", member.roles.cache.map(r => '`'+r.name+'`').slice(0,-1))
.setTimestamp()
.setFooter('a')
/* ... */