Home > database >  Rename command, ReferenceError: msg is not defined & TypeError: Cannot read properties of undefined
Rename command, ReferenceError: msg is not defined & TypeError: Cannot read properties of undefined

Time:07-01

Hello, im trying doing a channel rename for a discord, but i dont get it, im new to javascript.

Im having a lot of issues with it, i see a lot of post, but i dont get it for my command for being fix it

the errors that ReferenceError: message is not defined and that channel its not defined.

const fs = require('fs');

const Discord = require('discord.js');
const yaml = require('js-yaml');
const supportbot = yaml.load(
  fs.readFileSync('./Configs/supportbot.yml', 'utf8')
);

const cmdconfig = yaml.load(fs.readFileSync('./Configs/commands.yml', 'utf8'));

const Command = require('../Structures/Command.js');
const { channel } = require('diagnostics_channel');

module.exports = new Command({
  name: cmdconfig.RenameCommand,
  description: cmdconfig.RenameCommandDesc,
  options: [
    {
      name: 'channel',
      description: 'Especifica el canal',
      type: 'STRING',
      required: true,
    },
  ],

  async run(interaction) {
    const { getRole } = interaction.client;
    let ModDiscord = await getRole(supportbot.ModDiscord, interaction.guild);

    if (!ModDiscord) return interaction.reply('Algunos errores han fallado');
    const NoPerms = new Discord.MessageEmbed()
      .setTitle('Permisos invalidos!')
      .setDescription(
        `${supportbot.IncorrectPerms}\n\nRol Requerido: **Staff**`
      )
      .setColor(supportbot.WarningColour);

    if (interaction.member.roles.cache.has(ModDiscord.id)) {
      let channel = interaction.options.getString('channel');

      if (!channel)
        return msg.channel.send(`:x: Missing a Name, Please Specify the name!`);

      msg.channel.setName(channel);

      var confir = new Discord.MessageEmbed()
        .setColor('0x06F7C7')
        .setDescription(
          `:white_check_mark: ${msg.author}, I Have Successfully set the name of the Channel to **${channel}**!`
        );
      msg.channel.send({ embeds: [confir] });
      msg.delete();
    } else {
      return interaction.reply({ embeds: [NoPerms] });
    }
  },
});

CodePudding user response:

There is a lot wrong with your code. You suddenly try to access a msg variable out of nowhere, forget to await multiple promises and for some reason use var to declare a variable. Also, ideally, interactions should always be replied to. I cleaned up your code a bit which results in this:

module.exports = new Command({
    name: cmdconfig.RenameCommand,
    description: cmdconfig.RenameCommandDesc,
    options: [
        {
            name: "channel",
            description: "Especifica el canal",
            type: "STRING",
            required: true,
        },
    ],

    async run(interaction) {
        const { getRole } = interaction.client;
        let ModDiscord = await getRole(supportbot.ModDiscord, interaction.guild);

        if (!ModDiscord) {
            await interaction.reply("Algunos errores han fallado");
            return;
        }
        const NoPerms = new Discord.MessageEmbed()
            .setTitle("Permisos invalidos!")
            .setDescription(
                `${supportbot.IncorrectPerms}\n\nRol Requerido: **Staff**`
            )
            .setColor(supportbot.WarningColour);

        if (
            interaction.member.roles.cache.has(ModDiscord.id)
        ) {


            let channel = interaction.options.getString("channel");


            if (!channel) {
                await interaction.reply(`:x: Missing a Name, Please Specify the name!`);
                return;
            }

            await interaction.channel.setName(channel)

            const confir = new Discord.MessageEmbed()
                .setColor('0x06F7C7')
                .setDescription(`:white_check_mark: ${interaction.user}, I Have Successfully set the name of the Channel to **${channel}**!`)
            await interaction.reply({ embeds: [confir] });
        } else {
            await interaction.reply({ embeds: [NoPerms] });
        }

    },
});

Please look at what I changed and try to understand it and I also suggest that you use a linter if you don't already, it'll help you with some of these issues.

  • Related