0
votes

I sort of need help here, honestly not sure where I went wrong, here is the full code. I am sort of new, just trying to bring back the mention user and the reason back in a message instead of doing anything with this information.

const { client, MessageEmbed } = require('discord.js');
const { prefix } = require("../config.json");




module.exports = {
    name: "report",
    description: "This command allows you to report a user for smurfing.",
    catefory: "misc",
    usage: "To report a player, do $report <discord name> <reason>",
    async execute(message, client){

        function getUserFromMention(mention) {
            if (!mention) return;
        
            if (mention.startsWith('<@') && mention.endsWith('>')) {
                mention = mention.slice(2, -1);
        
                if (mention.startsWith('!')) {
                    mention = mention.slice(1);
                }
        
                return client.users.cache.get(mention);
            }
        }
        

        const args = message.content.slice(prefix.length).trim().split(/ +/);
        const command = args.shift().toLowerCase();

        const offender = getUserFromMention(args[0]);

        if (args.length < 2) {
            return message.reply('Please mention the user you want to report and specify a reason.');
        }
    
        const reason = args.slice(1).join(' ');

        message.reply("You reported",offender,"for reason:", reason)
    }

}

If I put no mention, I end up with this

And If I do put a mention this I get the above error with no response.

(node:4044) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'cache' of undefined

Index.js:

const fs = require('fs');
const Discord = require("discord.js");
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
client.prefix = prefix;
client.commands = new Discord.Collection();


const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
    const event = require(`./events/${file}`);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(...args,client));
    } else {
        client.on(event.name, (...args) => event.execute(...args,client));
    }
}

client.login(token);
2
Can you please show the code where you invoke this? - MrMythical
@MrMythical Edited it. - Cherie
By invoke, I mean where you put <something>.execute() - MrMythical
Also, to put strings together, you have to use +, not ,. Use this in message.reply: "You reported " + offender + " for reason: " + reason - MrMythical
Alright, gotcha on the string part, thank you will change now @MrMythical - Cherie

2 Answers

0
votes

You don't have to create a function to get a mention from a message, you can use the Message.mentions property to get the mentions, check the docs for other info about it. This should solve your issue.

const { prefix } = require("../config.json");

module.exports = {
    name: "report",
    description: "This command allows you to report a user for smurfing.",
    catefory: "misc",
    usage: "To report a player, do $report <discord name> <reason>",
    async execute(message, client) {

        const args = message.content.slice(1).trim().split(/ +/);
        const offender = message.mentions.users.first();
        // users is a collection, so we use the first method to get the first element
        // Docs: https://discord.js.org/#/docs/collection/master/class/Collection 

        if (args.length < 2 || !offender.username) {
            return message.reply('Please mention the user you want to report and specify a reason.');
        }
    
        const reason = args.slice(1).join(' ');

        message.reply(`You reported ${offender} for reason: ${reason}`);
    }
}
0
votes

Why are you calling client in a command file if you already started a new instance of a client in your root file? try removing client from the top of the code. Hope that works