每 5 秒发送一条消息 Client.on('ready', () => { });在 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
准备好之前获取公会和频道。公会和频道缓存仅在发出就绪事件后可用,就像在几乎所有其他类型的 Discord 结构上检索信息时一样。如您所见,下面的代码片段在 ready
事件处理程序之外,这将导致 guild
始终为 undefined
(作为您得到的错误状态):
const guild = Client.guilds.cache.get('925830522138148976');
const channel2up22 = guild.channels.cache.get('935549530210983976');
相反,将那段代码移到 doSomething
函数本身,这样您的代码只会在客户端准备就绪后才尝试检索公会和频道。当然,假设您仅在客户端准备就绪后调用 doSomething
函数(如果您从 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,我设法正确修改了我的代码,使其 returns 每 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)
});
我想每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
准备好之前获取公会和频道。公会和频道缓存仅在发出就绪事件后可用,就像在几乎所有其他类型的 Discord 结构上检索信息时一样。如您所见,下面的代码片段在 ready
事件处理程序之外,这将导致 guild
始终为 undefined
(作为您得到的错误状态):
const guild = Client.guilds.cache.get('925830522138148976');
const channel2up22 = guild.channels.cache.get('935549530210983976');
相反,将那段代码移到 doSomething
函数本身,这样您的代码只会在客户端准备就绪后才尝试检索公会和频道。当然,假设您仅在客户端准备就绪后调用 doSomething
函数(如果您从 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,我设法正确修改了我的代码,使其 returns 每 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)
});