简体   繁体   中英

Cannot read properties of undefined (reading 'send') (Discord.js v13)

i have been trying to make a leaveserver code for my bot, but i always get the error message Cannot read properties of undefined (reading 'send') I really wanna know why it always says the error messages! Please help me! Thanks!

const Discord = require("discord.js")
const rgx = /^(?:<@!?)?(\d+)>?$/;

var self = this

const OWNER_ID = require("../../config.json").OWNER_ID;
module.exports = {
  name: "leaveserver",
  description: "leavs a server",
  run: async (client, message, args) => {
    if (!OWNER_ID)
      return message.channel.send("This command is Owner Only")},
      
async run(message, args) {
    const guildId = args[0];
    if (!rgx.test(guildId))
    return message.channel.send("Please Provide a valid server id!")
    const guild = message.client.guild.cache.get(guildId);
    if (!guild) return message.channel.send(message, 0, 'Unable to find server, please check the provided ID');
    await guild.leave();
    const embed = new MessageEmbed()
      .setTitle('Leave Guild')
      .setDescription(`I have successfully left **${guild.name}**.`)
      .setFooter(message.member.displayName,  message.author.displayAvatarURL({ dynamic: true }))
      .setTimestamp()
      .setColor(message.guild.me.displayHexColor);
    message.channel.send({embeds: [embed]});
  } 
}

Try this code

const {
    MessageEmbed
} = require("discord.js")
const rgx = /^(?:<@!?)?(\d+)>?$/;
const OWNER_ID = require("../../config.json").OWNER_ID;

module.exports = {
    name: "leaveserver",
    description: "leaves a server",
    run: async (client, message, args) => {
        const guildId = args[0];
        const guild = message.client.guilds.cache.get(guildId);

        if (!OWNER_ID) return message.channel.send("This command is Owner Only");

        if (!rgx.test(guildId)) return message.channel.send("Please Provide a valid server id!")
        if (!guild) return message.channel.send(message, 0, 'Unable to find server, please check the provided ID');

        await guild.leave();
        
        const embed = new MessageEmbed()
            .setTitle('Leave Guild')
            .setDescription(`I have successfully left **${guild.name}**.`)
            .setFooter({
                text: message.member.displayName,
                iconURL: message.author.displayAvatarURL({
                    dynamic: true
                })
            })
            .setTimestamp()
            .setColor(message.guild.me.displayHexColor)
        message.channel.send({
            embeds: [embed]
        })
    }
}

Removed the duplicated async run(message, args) {

Try to do this:

const client = new Discord.Client({intents:
["GUILDS", "GUILD_MESSAGES"]});

client.on("message" , msg => {

   await guild.leave();
    const embed = new MessageEmbed()
      .setTitle('Leave Guild')
      .setDescription(`I have successfully left **${guild.name}**.`)
      .setFooter(msg.member.displayName,  msg.author.displayAvatarURL({ dynamic: true }))
      .setTimestamp()
      .setColor(msg.guild.me.displayHexColor);
    msg.channel.send({embeds: [embed]});
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM