OK, this may sound like a really simple question, but the below code keeps giving me an error on the console. After some research, it looks as though I may have messed up setting the variables, but I'm not sure how to fix. For context, this module is to update channels with server stats. Code:
function updateStats(client) {
const Discord = require('discord.js');
const config = require("../settings/configuration");
const settings = require("../settings/configuration");
const guild = client.guilds.cache.get(settings.BOT_SETTINGS.Guild_ID);
const totalUsers = client.channels.fetch('883654989271154728');
const onlineUsers = client.channels.fetch('883655041813200926');
setInterval(function() {
const interval = (async function() {
for await (const startTime of setInterval(interval, Date.now())) {
const now = Date.now();
console.log(now);
if ((now - startTime) > 1000)function updateStats(client) {
const Discord = require('discord.js');
const config = require("../settings/configuration");
const settings = require("../settings/configuration");
const guild = client.guilds.cache.get(settings.BOT_SETTINGS.Guild_ID);
const totalUsers = client.channels.fetch('883654989271154728');
const onlineUsers = client.channels.fetch('883655041813200926');
setInterval(function() {
const interval = (async function() {
for await (const startTime of setInterval(interval, Date.now())) {
const now = Date.now();
console.log(now);
if ((now - startTime) > 1000)
break;
}
console.log(Date.now());
})();
console.log('Getting stats update..')
var userCount = guild.memberCount;
var onlineCount = guild.members.filter(m => m.presence.status === 'online').size
$console.log("Total Users: " userCount);
$console.log("Online Users: " onlineCount);
totalUsers.setName("Total Users: " userCount)
.then(newChannel => console.log(`Stat channel renamed to: ${newChannel.name}`))
.catch(console.error);
onlineUsers.setName("Online Users: " onlineCount)
.then(newChannel => console.log(`Stat channel renamed to: ${newChannel.name}`))
.catch(console.error);
}, 30000)
}
module.exports = {
updateStats
}
break;
}
console.log(Date.now());
})();
console.log('Getting stats update..')
var userCount = guild.memberCount;
var onlineCount = guild.members.filter(m => m.presence.status === 'online').size
console.log("Total Users: " userCount);
console.log("Online Users: " onlineCount);
totalUsers.setName("Total Users: " userCount)
.then(newChannel => console.log(`Stat channel renamed to: ${newChannel.name}`))
.catch(console.error);
onlineUsers.setName("Online Users: " onlineCount)
.then(newChannel => console.log(`Stat channel renamed to: ${newChannel.name}`))
.catch(console.error);
}, 30000)
}
module.exports = {
updateStats
}
Error on console:
[Error] An error happened in process:
ReferenceError: Cannot access 'interval' before initialization
at /home/container/events/Stats.js:10:51
at Timeout._onTimeout (/home/container/events/Stats.js:49:9)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)
CodePudding user response:
Just change const interval = (async function() {
to let interval = (async function() {
and then just use interval = (async function() {
like that:
let interval = (async function() {
for await (const startTime of setInterval(interval, Date.now())) {
const now = Date.now();
console.log(now);
if ((now - startTime) > 1000)function updateStats(client) {
const Discord = require('discord.js');
const config = require("../settings/configuration");
const settings = require("../settings/configuration");
const guild = client.guilds.cache.get(settings.BOT_SETTINGS.Guild_ID);
const totalUsers = client.channels.fetch('883654989271154728');
const onlineUsers = client.channels.fetch('883655041813200926');
setInterval(function() {
interval = (async function() {