'Cannot read properties of undefined (reading 'execute) when i using js

hello I have a discord bot and I want to create reaction roles but when I execute It doesn't work ? please any body help me

this is ap prt of the code put it only doesn't work actually i tried hard

module.exports = {
    name: 'reac',
    description: "Sets up a reaction role message!",
    async execute(message, args, Discord, client) {
        const channel = '974390749862961162';
        const students_rule = message.guild.roles.cache.find(role => role.name === "Student");
        const blueTeamRole = message.guild.roles.cache.find(role => role.name === "student");

here the index.js file and the code of this file :

const Discord = require('discord.js');

const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION" ]});

const prefix = '-';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));

for(const file of commandFiles){
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('Nashmi Bot is online!');
});
client.on('message', message => {
    if(!message.content.startsWith(prefix) || message.author.bot )return;
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'rules'){
        client.commands.get('rules').execute(message, args);
    }
    if (command ==='reac'){
        client.commands.get('reac').execute(message, args, Discord, client);
    }
});


Solution 1:[1]

In your command file, follow the pattern on the first few lines.

Instead of async execute(message, args, Discord, client), Try execute: async (message, args, Discord, client)

Solution 2:[2]

I think you should replace execute with run. Change your command file as following:

module.exports = {
    name: 'reac',
    description: "Sets up a reaction role message!",
    async run(message, args, Discord, client) => {
        const channel = '974390749862961162';
        const students_rule = message.guild.roles.cache.find(role => role.name === "Student");
        const blueTeamRole = message.guild.roles.cache.find(role => role.name === "student");

This works for me.

Important note: change all other command files to be like this run function as well.


I also suggest following this structure for your index.js file:

client.on("message", async message => {
  if (message.author.bot) return;
  else if (message.content.startsWith(prefix)){
    const args = message.content.slice(prefix.length).trim().split(/ +/g)
    const commandName = args.shift().toLowerCase();
    const command = await client.commands.get(commandName);
    if (!command) return;
    await command.run(client, message, args, Discord);
  }
}

That way, you don't have to create a lot of if and else if statements for each of your command, and it makes creating additional commands so much easier.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aleks02
Solution 2 panzer-chan