Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'id' of undefined at line 24. #24

Open
ghost opened this issue Feb 8, 2022 · 4 comments
Open

TypeError: Cannot read property 'id' of undefined at line 24. #24

ghost opened this issue Feb 8, 2022 · 4 comments

Comments

@ghost
Copy link

ghost commented Feb 8, 2022

Hello, I am @SevenworksGD's main account. I keep getting an error about it reading the property "id" of undefined on line 24.
Can you help me as soon as possible? Thanks.

require('isomorphic-fetch');

const GD = require("gd.js")

const gd = new GD();

const wait = time => new Promise(resolve => setTimeout(resolve, time));

async function messagesUpTo(user, messageId) {
  if (messageId == null) return user.getMessages(10);
  for (let i = 10;; i *= 2) {
    let fetchedMessages = await user.getMessages(i);
    const index = fetchedMessages.findIndex(msg => msg.id == messageId);
    if (index != -1) return fetchedMessages.slice(0, index);
  }
}

async function messageBot() {
  const me = await gd.users.login({ username: "...", password: "...." });
  let lastMessageId = null;
  while (true) {
    const messages = await messagesUpTo(me, lastMessageId);
    lastMessageId = messages[0].id;
    for (const message of messages) {
      if (message.subject == 'Hello') {
        await user.sendMessage(message.from.accountID, 'Hello', 'How are you');
      }
    }
    await wait(15 * 1000); 
  }
}

messageBot();
@101arrowz
Copy link
Owner

That probably means you ran out of messages, you can just continue if you see that messages.length == 0 before accessing messages[0].id.

@ghost
Copy link
Author

ghost commented Feb 20, 2022

I get (node:1184) UnhandledPromiseRejectionWarning: ReferenceError: user is not defined when I have messages and (node:4472) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'id' of undefined when I have no messages.

@101arrowz
Copy link
Owner

This is a bug in the package, will fix it when I get a chance.

@ghost
Copy link
Author

ghost commented Feb 21, 2022

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant