Welcome to Qi-U Community for programmer and developer-Open, Learning and Share


0 votes
in Technique[技术] by (31.9m points)

discord.js v12 | TypeError: Cannot read property 'send' of undefined

Here is my entire code for my ban command. Good to note I am using Discord.JS Commando as well I have been struggling with this error but literally cannot figure out why I am getting it everything looks fine unless I have used a deprecated function. Would really appreciate someone to help me on this one I've been getting along quite well creating a rich featured bot before this happened.

const { Command } = require('discord.js-commando');
const { MessageEmbed } = require('discord.js');
const db = require('quick.db');

module.exports = class banCommand extends Command {
  constructor(client) {
      super(client, {
        name: 'ban',
        memberName: "ban",
        group: 'moderation',
        guildOnly: true,
        userPermissions: ['BAN_MEMBERS'],
        description: 'Bans the mentioned user from the server with additional modlog info.'

    async run(message, args) {
            if (!args[0]) return message.channel.send('**Please Provide A User To Ban!**')

            let banMember = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.guild.members.cache.find(r => r.user.username.toLowerCase() === args[0].toLocaleLowerCase()) || message.guild.members.cache.find(ro => ro.displayName.toLowerCase() === args[0].toLocaleLowerCase());
            if (!banMember) return message.channel.send('**User Is Not In The Guild**');
            if (banMember === message.member) return message.channel.send('**You Cannot Ban Yourself**')

            var reason = args.slice(1).join(' ');

            if (!banMember.bannable) return message.channel.send('**Cant Kick That User**')
            banMember.send(`**Hello, You Have Been Banned From ${message.guild.name} for - ${reason || 'No Reason'}**`).then(() =>
                message.guild.members.ban(banMember, { days: 7, reason: reason })).catch(() => null)
                message.guild.members.ban(banMember, { days: 7, reason: reason })
            if (reason) {
            var sembed = new MessageEmbed()
                .setAuthor(message.guild.name, message.guild.iconURL())
                .setDescription(`**${banMember.user.username}** has been banned for ${reason}`)
            } else {
                var sembed2 = new MessageEmbed()
                .setAuthor(message.guild.name, message.guild.iconURL())
                .setDescription(`**${banMember.user.username}** has been banned`)
            let channel = db.fetch(`modlog_${message.guild.id}`)
            if (channel == null) return;

            if (!channel) return;

            const embed = new MessageEmbed()
                .setAuthor(`${message.guild.name} Modlogs`, message.guild.iconURL())
                .setThumbnail(banMember.user.displayAvatarURL({ dynamic: true }))
                .setFooter(message.guild.name, message.guild.iconURL())
                .addField('**Moderation**', 'ban')
                .addField('**Banned**', banMember.user.username)
                .addField('**ID**', `${banMember.id}`)
                .addField('**Banned By**', message.author.username)
                .addField('**Reason**', `${reason || '**No Reason**'}`)
                .addField('**Date**', message.createdAt.toLocaleString())

            var sChannel = message.guild.channels.cache.get(channel)
            if (!sChannel) return;

Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (31.9m points)

The reason you are getting the TypeError: args.slice(...).join is not a function error is because the slice method creates a new array of the sliced data, and so can not join(' ') since there is no space to join with. (i.e. it is not a string)

What you are looking for is args.slice(1).toString().replace(",", " ")

This removes the 2nd part of the args array object, then converts it to a string, then removes the commas in the string and replaces them with spaces.

Welcome to Qi-U Community for programmer and developer-Open, Learning and Share

755k questions

547k answers


86.4k users