为什么我不能向不和谐的用户发送 DM?

时间:2021-01-27 18:35:17

标签: javascript discord discord.js bots

我正在尝试在被禁止时向用户发送消息。我之前尝试过,只能发送banEmbed,但后来也停止工作了。错误:DiscordAPIError: Cannot send messages to this user

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

module.exports = {
    name: 'ban',
    description: "Dočasně zabanuje člena.",
    async execute(message, args, Discord, client, chalk, ms){
        await message.channel.messages.fetch({limit: 1}).then(messages =>{
            message.channel.bulkDelete(messages);
        });

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

            const botId = '799652033509457940';
            userId.ban();
            
            const banEmbed = new Discord.MessageEmbed()
            .setColor('#a81919')
            .setTitle('Ban')
            .addFields(
                {name:'Člen:', value:`${userId}`},
                {name:'Udělil:', value:`${author}`},
                {name:'Délka:', value:`${ms(ms(args[1]))}`},
                {name:'Důvod:', value:`${reason}`},
            )
            .setTimestamp()
            channelId.send(banEmbed)

            const banMsgEmbed = new Discord.MessageEmbed()
            .setColor('#a81919')
            .setTitle('Ban')
            .addFields(
                {name:'Udělil:', value:`${author}`},
                {name:'Délka:', value:`${ms(ms(args[1]))}`},
                {name:'Důvod:', value:`${reason}`},
            )
            .setTimestamp()
            userId.send(banMsgEmbed)

            setTimeout(function () {
                message.guild.members.unban(userId);

                const unbanEmbed = new Discord.MessageEmbed()
                .setColor('#25a819')
                .setTitle('Unban')
                .addFields(
                {name:'Člen:', value:`${userId}`},
                {name:'Udělil:', value:`<@${botId}>`},
                {name:'Důvod:', value:`Ban vypršel.`},
                )
                .setTimestamp()
                channelId.send(unbanEmbed)
                    
                const unbanMsgEmbed = new Discord.MessageEmbed()
                .setColor('#25a819')
                .setTitle('Unban')
                .setURL('https://discord.gg/9tuHHaHeGY')
                .addFields(
                {name:'Udělil:', value:`<@${botId}>`},
                {name:'Důvod:', value:`Ban vypršel.`},
                )
                .setFooter('Pokud se chceš připojit, tak zde je odkaz: https://discord.gg/9tuHHaHeGY')
                .setTimestamp()
                userId.send(unbanMsgEmbed)
            }, ms(args[1]));

            console.log(chalk.green(`[INFO] /ban/ ${userId.user.username}, ${ms(ms(args[1]))}`));
        }else{
            message.channel.send('Nemůžeš zabanovat tohoto člena');
            console.log(chalk.red(`[ERROR] /ban/ Can not find target`));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果我没记错的话,机器人不能 DM 不与机器人共享服务器的用户。也许在禁止用户之前直接 DM 用户?