Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

javascript - How to properly define an id?

I'm trying to do an unban command. But I'm having trouble defining a member id. Error: TypeError: Cannot read property 'id' of undefined. I hope that there will be someone who can solve this problem. (By the way, I'm just learning and I understand that let works for everything after it.)

const { ReactionCollector, User } = require("discord.js");

module.exports = {
    name: 'unban',
    description: "Odbanuje ?lena.",
    async execute(message, args, Discord, client, chalk){
        await message.channel.messages.fetch({limit: 1}).then(messages =>{
            message.channel.bulkDelete(messages);
        });

        const channelId = client.channels.cache.get('802649418087530537');
        const author = message.author;
        const userName = message.mentions.users.first();
        
        let userId = message.guild.members.cache.get(userName.id);
        
        if(!message.member.permissions.has("BAN_MEMBERS")){
            message.reply('Nemá? pot?ebné permisse!')
            .then(msg => {
                msg.delete({ timeout: 5000 })
            });
            console.log(chalk.red('[ERROR] (./commands/unban/) Missing "BAN_MEMBERS" permission'));
            return;
        } else if(!args[1]){
            message.reply('`!unban <?len> <d?vod>`')
            .then(msg => {
                msg.delete({ timeout: 5000 })
            });
            console.log(chalk.red('[ERROR] (./commands/unban/) Missing args[1]'));
            return;
        }

        const banList = await message.guild.fetchBans();
        const bannedUser = banList.find(user => user.id === userId);

        if(bannedUser){
            let reason = args.slice(1).join(" ");

            const botId = '799652033509457940';
            
            const banEmbed = new Discord.MessageEmbed()
            .setColor('#2ca819')
            .setTitle('Unban')
            .addFields(
                {name:'?len:', value:`${userId}`},
                {name:'Udělil:', value:`${author}`},
                {name:'D?vod:', value:`${reason}`},
            )
            .setTimestamp()
            channelId.send(banEmbed)

            await userId.unban()

            console.log(chalk.green(`[INFO] (./commands/unban/) User "${userId.user.username}" was unbanned"`));
        }else{
            message.reply('Tohoto ?lena nem??u najít.')
            .then(msg => {
                msg.delete({ timeout: 5000 })
            });

            console.log(chalk.red(`[ERROR] (./commands/unban/) Can not find target`));
        }
    }
}
question from:https://stackoverflow.com/questions/65927644/how-to-properly-define-an-id

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Okay so you are just getting first user mention from message mentions, you are not checking if there is any user or not

const { ReactionCollector, User } = require("discord.js");

module.exports = {
    name: 'unban',
    description: "Odbanuje ?lena.",
    async execute(message, args, Discord, client, chalk){
        await message.channel.messages.fetch({limit: 1}).then(messages =>{
            message.channel.bulkDelete(messages);
        });

        const channelId = client.channels.cache.get('802649418087530537');
        const author = message.author;
        const userName = message.mentions.users.first();

        if (!userName) return message.channel.send("please mention a user");
        
        let userId = message.guild.members.cache.get(userName.id);
        
        if(!message.member.permissions.has("BAN_MEMBERS")){
            message.reply('Nemá? pot?ebné permisse!')
            .then(msg => {
                msg.delete({ timeout: 5000 })
            });
            console.log(chalk.red('[ERROR] (./commands/unban/) Missing "BAN_MEMBERS" permission'));
            return;
        } else if(!args[1]){
            message.reply('`!unban <?len> <d?vod>`')
            .then(msg => {
                msg.delete({ timeout: 5000 })
            });
            console.log(chalk.red('[ERROR] (./commands/unban/) Missing args[1]'));
            return;
        }

        const banList = await message.guild.fetchBans();
        const bannedUser = banList.find(user => user.id === userId);

        if(bannedUser){
            let reason = args.slice(1).join(" ");

            const botId = '799652033509457940';
            
            const banEmbed = new Discord.MessageEmbed()
            .setColor('#2ca819')
            .setTitle('Unban')
            .addFields(
                {name:'?len:', value:`${userId}`},
                {name:'Udělil:', value:`${author}`},
                {name:'D?vod:', value:`${reason}`},
            )
            .setTimestamp()
            channelId.send(banEmbed)

            await userId.unban()

            console.log(chalk.green(`[INFO] (./commands/unban/) User "${userId.user.username}" was unbanned"`));
        }else{
            message.reply('Tohoto ?lena nem??u najít.')
            .then(msg => {
                msg.delete({ timeout: 5000 })
            });

            console.log(chalk.red(`[ERROR] (./commands/unban/) Can not find target`));
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...