Error with guild.members.me

Our bot is divided into several shards and clusters. To be able to make some updates, we go through each cluster and then each guild with client.guilds.cache.values(). But then guild.members.me is often null. However, the bot is still on the server. What could be the reason for this?
7 Replies
d.js toolkit
d.js toolkit2w ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Mark
Mark2w ago
it should only be null if you're removing it from cache somehow, such as with a sweeper
BastiGameツ
BastiGameツOP2w ago
we do not clear the cache at any point. where and how would I do something like this?
const client = new Discord.Client({
intents: intents,
partials: [Partials.Message],
allowedMentions: { parse: ["users", "roles", "everyone"] },
failIfNotExists: false,
shardCount: totalShards,
shards: shards,
presence: {
status: config.bot.status,
afk: false,
activities: [
{
type: 4,
state: config.bot.statusMsg,
name: "Custom Status",
},
],
},
rest: {
invalidRequestWarningInterval: 500,
api: "https://discord.com/api",
},
});
const client = new Discord.Client({
intents: intents,
partials: [Partials.Message],
allowedMentions: { parse: ["users", "roles", "everyone"] },
failIfNotExists: false,
shardCount: totalShards,
shards: shards,
presence: {
status: config.bot.status,
afk: false,
activities: [
{
type: 4,
state: config.bot.statusMsg,
name: "Custom Status",
},
],
},
rest: {
invalidRequestWarningInterval: 500,
api: "https://discord.com/api",
},
});
that's the current client constructor.
const intents = [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildModeration,
GatewayIntentBits.GuildExpressions,
];
const intents = [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildModeration,
GatewayIntentBits.GuildExpressions,
];
- these are the intents - the rest is not mine, could that cause problems?
static async hasNeededDiscordPermissions(member, permissions, interaction = null) {
if (!member) {
Logger.warn(LogModule.BOT, null, "Cannot check member permissions because member is null or undefined");
return false;
}
}

static async hasNeededDiscordPermissions(member, permissions, interaction = null) {
if (!member) {
Logger.warn(LogModule.BOT, null, "Cannot check member permissions because member is null or undefined");
return false;
}
}

here comes the log that member was not found
if (!await PermissionUtil.hasNeededDiscordPermissions(guild.members.me, PermissionsBitField.Flags.ViewChannel)) return null;
if (!await PermissionUtil.hasNeededDiscordPermissions(guild.members.me, PermissionsBitField.Flags.ViewChannel)) return null;
with this code I call the hasNeededDiscordPermissions guild comes from client.guilds.cache.values() yes that will be checked Because of an NDA I am not allowed to send it in a public chat. can we continue writing via dm?
for (const guild of client.guilds.cache.values()) {

if (!guild.available) {
Logger.debug(LogModule.BOT, guild?.id, "Guild " + guild?.id + " is not available");
continue
}

const youTubeModule = await client.getGuildModule(guild.id, ModuleList.youtube);

if (youTubeModule) {
updateYouTubeCount += await this.#updateYouTubeSlots(client, guild, youTubeModule);
}
for (const guild of client.guilds.cache.values()) {

if (!guild.available) {
Logger.debug(LogModule.BOT, guild?.id, "Guild " + guild?.id + " is not available");
continue
}

const youTubeModule = await client.getGuildModule(guild.id, ModuleList.youtube);

if (youTubeModule) {
updateYouTubeCount += await this.#updateYouTubeSlots(client, guild, youTubeModule);
}
async #updateYouTubeSlots(client, guild, youtubeModule) {
if (!await ModuleSetupChecker.areSocialMediaSlotsSetup(youtubeModule)) return 0;

if (Object.values(youtubeModule.slots).length === 0) return 0;

let count = 0;
for (const slot of Object.values(youtubeModule.slots)) {
let youtuber;
let youtuberList = null;

youtuber = await client.redis.getString("youtuber:" + slot.youtuber.id);
if (!youtuber) continue;
youtuber = JSON.parse(youtuber);

if (!youtuber.video) continue;

count += await YouTubeSlotsUpdateHandler.updateCurrentVideoInSlot(client, guild, slot, youtuber, youtubeModule, youtuberList);
}

return count;
}
async #updateYouTubeSlots(client, guild, youtubeModule) {
if (!await ModuleSetupChecker.areSocialMediaSlotsSetup(youtubeModule)) return 0;

if (Object.values(youtubeModule.slots).length === 0) return 0;

let count = 0;
for (const slot of Object.values(youtubeModule.slots)) {
let youtuber;
let youtuberList = null;

youtuber = await client.redis.getString("youtuber:" + slot.youtuber.id);
if (!youtuber) continue;
youtuber = JSON.parse(youtuber);

if (!youtuber.video) continue;

count += await YouTubeSlotsUpdateHandler.updateCurrentVideoInSlot(client, guild, slot, youtuber, youtubeModule, youtuberList);
}

return count;
}
static async updateCurrentVideoInSlot(client, guild, slot, youtuber, youtubeModule, youtuberList) {

if (!guild.members.me) {
Logger.info(LogModule.YOUTUBE, guild.id, "Guild.members.me null for guild " + guild.id);
}

const videosChannel = await FetchUtil.fetchChannel(guild, null, slot.videosChannel.id);
if (!videosChannel) return 0;
static async updateCurrentVideoInSlot(client, guild, slot, youtuber, youtubeModule, youtuberList) {

if (!guild.members.me) {
Logger.info(LogModule.YOUTUBE, guild.id, "Guild.members.me null for guild " + guild.id);
}

const videosChannel = await FetchUtil.fetchChannel(guild, null, slot.videosChannel.id);
if (!videosChannel) return 0;
static async fetchChannel(guild, interaction, channelID) {
if (!guild) {
Logger.warn(LogModule.FETCH, null, "Cannot fetch channel because guild is null or undefined");
return null;
}

if (!channelID) {
Logger.warn(LogModule.FETCH, guild.id, "Cannot fetch channel because channel is null or undefined");
return null;
}

const date = Date.now();

if (ThrottleUtil.check(guild.channels.fetch, channelID, date, 60 * 1000)) return null;

if (!await PermissionUtil.hasNeededDiscordPermissions(guild.members.me, PermissionsBitField.Flags.ViewChannel)) return null;
static async fetchChannel(guild, interaction, channelID) {
if (!guild) {
Logger.warn(LogModule.FETCH, null, "Cannot fetch channel because guild is null or undefined");
return null;
}

if (!channelID) {
Logger.warn(LogModule.FETCH, guild.id, "Cannot fetch channel because channel is null or undefined");
return null;
}

const date = Date.now();

if (ThrottleUtil.check(guild.channels.fetch, channelID, date, 60 * 1000)) return null;

if (!await PermissionUtil.hasNeededDiscordPermissions(guild.members.me, PermissionsBitField.Flags.ViewChannel)) return null;
that the code from the beginning to the error
Panda
Panda2w ago
this is just to limit the execution to prevent ratelimits 😄 its not using it just the name of it to be able to handle all cases Tbh no I didnt knew, how is that done ? Just fetch ratelimits or also update and send ratelimits ? Well we built that to prevent channel rename rate limits from happening Which actually happened quite alot, so either we are not using it correctly or its not working for some reason in our project So your plan on fixing with the "guild.members.me" would be just to check and skip it ? Or refetch the cache everytime before ? I dont trust our code, so there must be something we do wrong 😆 Yes we do fetch them before, it is passed via the "guild" parameter
await client.guilds.fetch(guildID)
await client.guilds.fetch(guildID)
ohh, we do that everywhere 🤦‍♂️ so better client.guilds.cache.get ? Well, that could explain some errors we encounter currently.... 😆 Check Thank you ❤️
Krowe moh
Krowe moh2w ago
oh i don't need to do client.user.setActivity on ready in v14? interesting v13 didn't have this iirc
Krowe moh
Krowe moh2w ago
alright

Did you find this page helpful?