簡體   English   中英

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

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

我想每 5 秒發送一條消息,第一個反應的用戶給他 10 個硬幣。

這段代碼不起作用,我現在得到一個錯誤: Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'channels') on the line: const channel2up22 = guild.channels.cache.get('935549530210983976');

盡管我看過文檔,但我做不到,我還指定我的公會 ID 和我的頻道 ID 是正確的,我在發布問題之前檢查過它。

你有辦法糾正這個錯誤嗎? 先感謝您!

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

問題是您正試圖在client准備好之前獲得行會和頻道。 公會和頻道緩存僅在發出 ready 事件后可用,就像在幾乎所有其他類型的 Discord 結構上檢索信息時的情況一樣。 如您所見,下面的代碼片段在ready事件處理程序之外,這將導致guild始終undefined (如您收到的錯誤狀態):

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

相反,將那段代碼移到doSomething function 本身,這樣您的代碼只會在客戶端准備好后嘗試檢索公會和頻道。 當然,假設您僅在客戶端准備就緒后調用doSomething function(如果您從ready處理程序或其他幾個事件處理程序調用它,則為真)。 這是一個例子:

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

這個問題還有一些其他可能的解決方案,但這個很可能是最簡單的解決方案。

更新

感謝@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