Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
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
        });
about 4 years ago ยท Juan Pablo Isaza
2 answers
Answer question

0

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]
            })
        }
    }
})
about 4 years ago ยท Juan Pablo Isaza Report

0

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]
            })
        }
    }
});

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!