TypeError: embed.addField is not a function

62 Views Asked by At
const csv = require('fast-csv');

const fs = require('fs');


async function readCSV(filePath) {

    return new Promise((resolve, reject) => {

        const data = [];

        const readStream = fs.createReadStream(filePath);


        csv.parseStream(readStream, { headers: true })

            .on('data', row => { data.push(row); })

            .on('end', () => { resolve(data); })

            .on('error', error => { reject(error); });

    });

}



module.exports = {

    data: new SlashCommandBuilder()

        .setName("랭킹")

        .setDescription("유저 랭킹을 조회해요!"),

    /**

     * @param {import("discord.js").CommandInteraction} interaction

     * @param {import("discord.js").Client} client

     */

    async execute(interaction) {

        await interaction.deferReply({ ephemeral: false });


        try {

            const data = await readCSV('database.csv');


            // 데이터를 마일리지 기준으로 내림차순 정렬

            data.sort((a, b) => b.mileage - a.mileage);


            // 상위 10개의 데이터만 추출

            const topRanking = data.slice(0, 10);


            const embed = new EmbedBuilder()

                .setTitle('랭킹')

                .setDescription('상위 10명의 유저 랭킹입니다.')

                .setColor("Green")

                .setTimestamp();


            topRanking.forEach((item, index) => {

                embed.addField(`#${index + 1} ${item.user}`, `마일리지: ${item.mileage.toLocaleString()}\n총 비행 횟수: ${item.fly.toLocaleString()}`, false);

            });


            return await interaction.editReply({ embeds: [embed] });

        } catch (error) {

            console.error(error);

            return await interaction.editReply({ content: "데이터를 읽는 도중 오류가 발생했습니다." });

        }

    },



};`

TypeError: embed.addField is not a function

This is a Discord bot that displays the ranking in a csv file using a command, but an error appears.

1

There are 1 best solutions below

0
JustinL On
topRanking.forEach((item, index) => {
    embed.addFields({
        name: `#${index + 1} ${item.user}`,
        value: `마일리지: ${item.mileage.toLocaleString()}\n총 비행 횟수: ${item.fly.toLocaleString()}`,
        inline: false,
    });
});

In DiscordJSv14 it is

.addFields({
name: "Header",
value: "Description",
inline: true/false
})