不和谐.js每 X 秒使用 Client.on('ready', () => { } 发送一条消息;



我想每隔X秒发送一条消息,第一个反应的用户给他10个硬币。

这个问题不是来自硬币系统,它是正确执行的。

这段代码不工作,并告诉我Cannot read properties of undefined (reading 'send')

我知道这是因为我没有使用Client.on('message', message => { }),但我真的希望它能通过ready有什么不同的方法吗?你能帮我改正这个代码吗?

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

不能完全测试这段代码,但这应该工作假设所有这些代码进入一个文件,如果你得到错误或下面的任何注释,我会看看我能做些什么来帮助。

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

这是我第一次修改后的代码,

我现在得到一个错误返回: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]
})
}
}
});

最新更新