Home > Back-end >  discord bot command, mention undefined
discord bot command, mention undefined

Time:11-07

I began to code a discord bot and would like to set a command which makes it say "user received a hug from another user" but it actually says "undefined received a hug from user" while I set another way for the undefined mention.

const Discord = require("discord.js");
const { UserAgent } = require("discord.js/src/util/Constants");
const ytdl = require("ytdl-core");
const Client = new Discord.Client({
    intents : [
        Discord.Intents.FLAGS.GUILDS,
        Discord.Intents.FLAGS.GUILD_MESSAGES
    ]
});

const prefix ="<";

Client.on("ready", () => {
    console.log("bot opérationnel")

});

Client.on("message", message => {
    if (message.author.bot) return;
    if (message.content.startsWith (prefix   "hug")){
        let mention = message.mentions.members.first();

        if(mention == undefined){
            message.channel.send("u have to hug someone who actually exists");
        }
        else {
            const embed = new Discord.MessageEmbed()
            .setColor("DARK_RED")
            .setTitle(":hugging: "   mention.displayname   " received a hug from "   message.author.username   " !")
            .setImage("https://cdn.weeb.sh/images/ryCG-OatM.gif");

        message.channel.send({embeds: [embed]});
        }
    }
});

Client.login("token");

CodePudding user response:

<GuildMember>.displayname is not a valid property. The correct property is <GuildMember>.displayName (notice the camelcasing)

I have corrected your code:

const Discord = require("discord.js");
const { UserAgent } = require("discord.js/src/util/Constants");
const ytdl = require("ytdl-core");
const Client = new Discord.Client({
    intents : [
        Discord.Intents.FLAGS.GUILDS,
        Discord.Intents.FLAGS.GUILD_MESSAGES
    ]
});

const prefix ="<";

Client.on("ready", () => {
    console.log("bot opérationnel")

});

Client.on("message", message => {
    if (message.author.bot) return;
    if (message.content.startsWith (prefix   "hug")){
        let mention = message.mentions.members.first();

        if(mention == undefined){
            message.channel.send("u have to hug someone who actually exists");
        }
        else {
            const embed = new Discord.MessageEmbed()
            .setColor("DARK_RED")
            .setTitle(":hugging: "   mention.displayName   " received a hug from "   message.author.username   " !")
            .setImage("https://cdn.weeb.sh/images/ryCG-OatM.gif");

        message.channel.send({embeds: [embed]});
        }
    }
});

Client.login("token");

CodePudding user response:

mention == undefined will just return nothing it would be like this instead if(!mention) and the correct code would be

Client.on("message", message => {
    if (message.author.bot) return;
    if (message.content.startsWith (prefix   "hug")){
        let mention = message.mentions.members.first();

        if (!mention) {
            return message.channel.send("u have to hug someone who actually exists");
      
        } else {
            const embed = new Discord.MessageEmbed()
            .setColor("DARK_RED")
            .setTitle(":hugging: "   mention.displayname   " received a hug from "   message.author.username   " !")
            .setImage("https://cdn.weeb.sh/images/ryCG-OatM.gif");

        message.channel.send({embeds: [embed]});
        }
    }
});
  • Related