-2

i have this error in my bot log

-(node:315) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'get' of undefined

at guildInvites.find.i (/app/server.js:477:46)

    at Map.find (/rbd/pnpm-volume/e510aebc-aa8d-41e6-bdbe-632fceb24fc5/node_modules/.registry.npmjs.org/discord.js/11.5.1/node_modules/discord.js/src/util/Collection.js:506:11)

at member.guild.fetchInvites.then.guildInvites (/app/server.js:477:33)

    at <anonymous>

    at process._tickCallback (internal/process/next_tick.js:189:7)

(node:315) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 355)

and this is the code

const invites = {};
const wait = require('util').promisify(setTimeout);
client.on('ready', () => {
  wait(1000);
  client.guilds.forEach(g => {
    g.fetchInvites().then(guildInvites => {
      invites[g.id] = guildInvites;
    });
  });
});

 member.guild.fetchInvites().then(guildInvites => {
    const ei = invites[member.guild.id];
    invites[member.guild.id] = guildInvites;
    const invite = guildInvites.find(i => ei.get(i.code).uses < i.uses);
    const inviter = client.users.get(invite.inviter.id);
    if(!inviter) inviter = `I can\'t figure out how ${member.displayName} joined the server.`;

i dont know how can i solve it, this is not all code i just send a part of my code but this where the error in

and pls can someone tell me how can i handle Promise bc i think this is the problem for example this is a code how can i handle it

client.on("message", async message=>{
  if (message.isMentioned(client.user)){
let msg = await message.channel.send("xxx")
msg.edit(`xxx`)
}
});
  • Ìf there were no invites in `invites` for a specific guild, ei will be empty/undefined and cause this problem. You need to handle that case in your code – fredrik Sep 11 '19 at 18:45
  • Though I missed the 2:nd `get` on the line below - `client.users.get`. That could also be the source – fredrik Sep 11 '19 at 18:50
  • ok how can i handle it – Őšämą Husseiŋ Sep 11 '19 at 18:52
  • https://stackoverflow.com/questions/5339121/how-do-you-implement-a-guard-clause-in-javascript – fredrik Sep 11 '19 at 18:55
  • @fredrik If the variable is defined when the event is emitted, the bot doesn't have a cache to compare the current invites to; it needs to be set *before* the listener is executed. – slothiful Sep 13 '19 at 02:25

1 Answers1

-1

When added to a new server, your current code won't set up the invites object for it. If a user joins and the bot hasn't restarted after being added, ei (invites[member.guild.id]) won't be defined yet. This is the source of your error.

To solve this issue, attach a listener to your client's guildCreate event that defines the guild's property within invites.

client.on('guildCreate', async guild => invites[guild.id] = await guild.fetchInvites());
slothiful
  • 5,548
  • 3
  • 12
  • 36