简体   繁体   中英

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

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

This code does not work, 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');

As much as I've watched the doc, I can't do it, I also specify that my Guild ID and my Channel id is correct, I checked it just before posting the question.

Would you have a way to remedy this error,? Thank you in advance!

 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')); // 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'; () => { // every 5 seconds setInterval(doSomething(). 5000) }), // 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] }) } } });

The issue is that you are trying to get the guild and channel before your client is ready. The guild and channel caches are only available after the ready event is emitted, as is the case when retrieving information on nearly every other type of Discord structure. As you can see, the below piece of code is outside the ready event handler, which will cause guild to always be undefined (as the error you're getting states):

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

Instead, move that piece of code into the doSomething function itself, so your code only attempts to retrieve the guild and channel once the client is ready. Assuming, of course, that you are only calling the doSomething function after the client is ready (this will be true if you call it from the ready handler or several other event handlers). Here's an example:

function doSomething() {
    const guild = Client.guilds.cache.get('925830522138148976');
    const channel2up22 = guild.channels.cache.get('935549530210983976');

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

There are some other possible solutions to this problem, but this one is most likely the simplest solution.

UPDATE

Thanks to @Cannicide , I managed to modify my code correctly so that it returns a message every 5 seconds without crashing.

 const Discord = require("discord.js"); const Client = new Discord.Client; const fs = require('fs') const userCoin = JSON.parse(fs.readFileSync('Storage/userCoin.json', 'utf-8')); function doSomething() { const guild = Client.guilds.cache.get('925830522138148976'); const channel2up22 = guild.channels.cache.get('966206342740181012'); 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(''). });catch(console.error), } Client,on('messageReactionAdd'. (reaction. user) => { const guild = Client.guilds;cache.get('925830522138148976'). const channel2up22 = guild.channels;cache.get('966206342740181012'). if (reaction.message.channel.id == channel2up22) { if (reaction.emoji.name == '' && user.id;== "936254253285146635") { 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). }) let 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(Embed) } } }); Client.on('ready', () => { setInterval(doSomething, 5000) });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM