简体   繁体   English

使用 Client.on('ready', () => { }) 每 5 秒发送一条消息; 在 Discord.js

[英]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.我想每 5 秒发送一条消息,第一个反应的用户给他 10 个硬币。

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');这段代码不起作用,我现在得到一个错误: 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.尽管我看过文档,但我做不到,我还指定我的公会 ID 和我的频道 ID 是正确的,我在发布问题之前检查过它。

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.问题是您正试图在client准备好之前获得行会和频道。 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.公会和频道缓存仅在发出 ready 事件后可用,就像在几乎所有其他类型的 Discord 结构上检索信息时的情况一样。 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):如您所见,下面的代码片段在ready事件处理程序之外,这将导致guild始终undefined (如您收到的错误状态):

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.相反,将那段代码移到doSomething function 本身,这样您的代码只会在客户端准备好后尝试检索公会和频道。 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).当然,假设您仅在客户端准备就绪后调用doSomething function(如果您从ready处理程序或其他几个事件处理程序调用它,则为真)。 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.感谢@Cannicide ,我设法正确修改了我的代码,使其每 5 秒返回一条消息而不会崩溃。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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