'Discord.js Send a message every X seconds with Client.on('ready', () => { });

I would like to send a message every X seconds and that the user who reacts first gives him 10 coins.

The problem does not come from the system of coins, it is correctly executed.

This code does not work and tells me Cannot read properties of undefined (reading 'send')

I know this is because I'm not using Client.on('message', message => { }) but I really wish it would go through ready is there any way to do it differently? Can you help me to correct this code?

Client.on('ready', () => {
    const fs = require('fs');

    const userCoin = JSON.parse(fs.readFileSync('Storage/userCoin.json', 'utf-8'));

    const channel2up22 = Client.channels.cache.get('935549530210983976');

                    //Random Drop
    const doSomething = () => {
        let Embed = new Discord.MessageEmbed()
                    .setTitle("Wild Gift 🎁 ! | GuinGame - v2.0 🎰")
                    .setColor('#caabd0')
                    .setDescription("Be the **first** to react ``'🚀'`` to this message to win **10 🪙!**")
                    .setThumbnail("https://media.giphy.com/media/Jv1Xu8EBCOynpoGBwd/giphy.gif")
                    .setFooter(text="🤖 GuinbearBot  |  🌐 Guinbeargang.io")
                    .setTimestamp()
                    channel2up22.send(Embed)
                    .then(message => {
                        message.react('🚀');
                    Client.on('messageReactionAdd', (reaction, user) => {
                        if (reaction.emoji.name === '🚀' && user.id == "338621907161317387") {
                            message.delete();
                            var Magic09 = 1;
                            while (Magic09 <= 10)
                                {
                                userCoin[user.id].CoinsAmount++;
                                Magic09++;
                                }
                        fs.writeFile('Storage/userCoin.json', JSON.stringify(userCoin), (err) => {if (err) console.error(err);})
                            let Embed = new Discord.MessageEmbed()
                            .setTitle("Wild Gift 🎁 ! | GuinGame - v2.0 🎰")
                            .setColor('#caabd0')
                            .setDescription("<@"+ user.id + "> has won the **Wild Gift 🎁 !**")
                            .setThumbnail("https://media.giphy.com/media/Jv1Xu8EBCOynpoGBwd/giphy.gif")
                            .setFooter(text="🤖 GuinbearBot  |  🌐 Guinbeargang.io")
                            .setTimestamp()
                            channel2up22.send(Embed)
                        }
                    })
                    }).catch(console.error);
      }
    setInterval(doSomething(), 5000)
  })

                    //Random Drop
        });


Solution 1:[1]

Not able to fully test this code but this should work assuming all this code goes into a single file, if you get errors or anything comment below and I'll see what I can do to help out.

// Near top of file but after you have defined your client with const Client = new Discord.Client()
const fs = require('fs')
const Discord = require('discord.js')
const Client = new Discord.Client ({
    intents: /*your intents here*/,
})
const guild = Client.guilds.cache.get('935549530210983976')
const channel2up22 = guild.channels.cache.get('935549530210983976');
// channels are an element of the guild not the client
const userCoin = JSON.parse(fs.readFileSync('Storage/userCoin.json', 'utf-8'));

// placing the function outside of a listener and can be called at anytime
function doSomething() {
    const Embed = new Discord.MessageEmbed()
        .setTitle("Wild Gift ? ! | GuinGame - v2.0 ?")
        .setColor('#caabd0')
        .setDescription("Be the **first** to react ``'?'`` to this message to win **10 ?!**")
        .setThumbnail("https://media.giphy.com/media/Jv1Xu8EBCOynpoGBwd/giphy.gif")
        .setFooter({
            text: "? GuinbearBot  |  ? Guinbeargang.io"
        })
        .setTimestamp()
    channel2up22.send({
        embeds: [Embed]
    }).then(message => {
        message.react('?');
    }).catch(console.error);
}

Client.on('ready', () => {
    setInterval(doSomething(), 5000)
    // every 5 seconds
})

// you want to avoid nesting listeners if at all possible
Client.on('messageReactionAdd', async (reaction, user) => {
    if (reaction.message.channel.id === channel2up22) {
        if (reaction.emoji.name === '?' && user.id === "338621907161317387") {
            // only lets one user react
            reaction.message.delete();
            var Magic09 = 1;
            while (Magic09 <= 10) {
                userCoin[user.id].CoinsAmount;
                Magic09++;
            }
            fs.writeFile('Storage/userCoin.json', JSON.stringify(userCoin), (err) => {
                if (err) console.error(err);
            })
            const Embed = new Discord.MessageEmbed()
                .setTitle("Wild Gift ? ! | GuinGame - v2.0 ?")
                .setColor('#caabd0')
                .setDescription(`${user} has won the **Wild Gift ? !**`)
                .setThumbnail("https://media.giphy.com/media/Jv1Xu8EBCOynpoGBwd/giphy.gif")
                .setFooter({
                    text: "? GuinbearBot  |  ? Guinbeargang.io"
                })
                .setTimestamp()
            channel2up22.send({
                embeds: [Embed]
            })
        }
    }
})

Solution 2:[2]

Here is my code after your first modifications,

I got now an error back : Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'channels') on the line : const channel2up22 = guild.channels.cache.get('935549530210983976');

const Discord = require("discord.js");
const Client = new Discord.Client;
const fs = require('fs')
const guild = Client.guilds.cache.get('925830522138148976');

const channel2up22 = 
 guild.channels.cache.get('935549530210983976');

const userCoin = JSON.parse(fs.readFileSync('Storage/userCoin.json', 'utf-8'));

function doSomething() {
    const Embed = new Discord.MessageEmbed()
        .setTitle("Wild Gift ? ! | GuinGame - v2.0 ?")
        .setColor('#caabd0')
        .setDescription("Be the **first** to react ``'?'`` to this message to win **10 ?!**")
        .setThumbnail("https://media.giphy.com/media/Jv1Xu8EBCOynpoGBwd/giphy.gif")
        .setFooter({
            text: "? GuinbearBot  |  ? Guinbeargang.io"
        })
        .setTimestamp()
    channel2up22.send({
        embeds: [Embed]
    }).then(message => {
        message.react('?');
    }).catch(console.error);
}

Client.on('ready', () => {
    setInterval(doSomething(), 5000)
});


Client.on('messageReactionAdd', async (reaction, user) => {
    if (reaction.message.channel.id === channel2up22) {
        if (reaction.emoji.name === '?' && user.id === "338621907161317387") {
            reaction.message.delete();
            var Magic09 = 1;
            while (Magic09 <= 10) {
                userCoin[user.id].CoinsAmount;
                Magic09++;
            }
            fs.writeFile('Storage/userCoin.json', JSON.stringify(userCoin), (err) => {
                if (err) console.error(err);
            })
            const Embed = new Discord.MessageEmbed()
                .setTitle("Wild Gift ? ! | GuinGame - v2.0 ?")
                .setColor('#caabd0')
                .setDescription(`${user} has won the **Wild Gift ? !**`)
                .setThumbnail("https://media.giphy.com/media/Jv1Xu8EBCOynpoGBwd/giphy.gif")
                .setFooter({
                    text: "? GuinbearBot  |  ? Guinbeargang.io"
                })
                .setTimestamp()
            channel2up22.send({
                embeds: [Embed]
            })
        }
    }
});

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
Solution 2