Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
How to Render live member count to ejs file

I'm trying to get the memberCount from my discord bot to be sent to my home.js file but I cannot find a way to send the live memberCount from the async arrow function to a variable in order to render it to my home.js file.

The error I get:

TypeError: Cannot read property 'memberCount' of undefined in discord.js

The code:

const express = require("express");
const bodyParser = require("body-parser");
const { Client, Guild, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const tracks = require(__dirname + '/tracks.js')
require("dotenv").config();

const app = express();

// members check up
const guild = client.guilds.cache.get('797158503247511592');
const memberCount = guild.memberCount;
const members = memberCount.toLocaleString();

console.log(members)


app.set('view engine', 'ejs');

app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(express.static("public"));

app.get("/", function(req, res){
  res.render("home");
});

app.listen(3000, function(){
  console.log("Server started on port 3000.");
});

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
  console.log(`${client.guild}`)
});

client.on('interactionCreate', async interaction => {
  if (!interaction.isCommand()) return;

  if (interaction.commandName === 'ping') {
    await interaction.reply('Pong!');
  }
});

client.login(process.env.TOKEN);
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

This is because the you try to get the guild before the client is even logged in... Hope this helps

const express = require("express");
const bodyParser = require("body-parser");
const { Client, Guild, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const tracks = require(__dirname + '/tracks.js')
require("dotenv").config();

const app = express();

app.set('view engine', 'ejs');

app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(express.static("public"));

app.get("/", function(req, res){
  res.render("home");
});

app.listen(3000, function(){
  console.log("Server started on port 3000.");
});

let membercount = 0
client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
  membercount = client.guilds.cache.get('797158503247511592').memberCount
});

client.on('interactionCreate', async interaction => {
  if (!interaction.isCommand()) return;

  if (interaction.commandName === 'ping') {
    await interaction.reply('Pong!');
  }
});

client.login(process.env.TOKEN);

console.log(membercount)
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda