louisefindlay23 / youtubecommunitybot

YouTube Community Bot posts updates of a YouTube Channel's Community Posts to a Discord Server Channel.
4 stars 0 forks source link

Cannot read properties of undefined (reading '0') #4

Open Xaenchan opened 12 months ago

Xaenchan commented 12 months ago
npm start

> youtubecommunitybot@1.0.0 start
> node bot.js

Ready!
TypeError: Cannot read properties of undefined (reading '0')
    at .\bot.js:28:50
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

getting this error .env should be fine i think

BOT_TOKEN=(used one i had for another bot and a new one) SERVER_ID=(both IDs should be one my bot got invited to) CHANNEL_ID=(both IDs should be one my bot got invited to) YT_API_URL=https://yt.lemnoslife.com/channels?part=community&id=(tried with 2 who had community posts) YT_CHANNEL_ID=(tried with 2 who had community posts)

louisefindlay23 commented 11 months ago

Hi, @Xaenchan.

Thanks for trying out this project.

The error messages shows up on line 28 which should be the communityPosts variable. Have you tried console.log(data) to see if the API is returning anything?

CatharsisRobb commented 3 weeks ago
/home/robb/Documents/AiChan/node_modules/discord.js/src/client/Client.js:214
    if (!token || typeof token !== 'string') throw new DiscordjsError(ErrorCodes.TokenInvalid);
                                                   ^

Error [TokenInvalid]: An invalid token was provided.
    at Client.login (/home/robb/Documents/AiChan/node_modules/discord.js/src/client/Client.js:214:52)
    at Object.<anonymous> (/home/robb/Documents/AiChan/bot.js:120:8)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12)
    at node:internal/main/run_main_module:28:49 {
  code: 'TokenInvalid'
}

Hey! I'm getting a similar token issue and I'm not really sure whats going on. To be honest I'm still a budding hobbyist and I think my issue lands in the Youtube Operational API. I read everything I could on it, including the formatting from the creator of this thread, and I still am not sure what exactly I'm missing, though it's probably something really stupid haha. For noobies like me the formatting of what to put where is unclear from their public documentation, though I tried a few things from my Channel ID to my Youtube v3 API key to no avail. Any ideas? I know the rest of my tokens are correct (I have a few other local discord bots already, so I'm not a complete idiot :P)

louisefindlay23 commented 3 weeks ago

Hi, @CatharsisDrums. Thanks for trying this out.

The error message seems to be related to Discord.js token. What does line 120 of bot.js look like?

CatharsisRobb commented 3 weeks ago

client.login(token); is the only thing there. For clarity's sake everything is in the same folder so it shouldn't be a lack of finding the .env at least.

louisefindlay23 commented 2 weeks ago

client.login(token); is the only thing there. For clarity's sake everything is in the same folder so it shouldn't be a lack of finding the .env at least.

Does the token console.log correctly? @CatharsisRobb

CatharsisRobb commented 2 weeks ago

Admittedly I'm not sure. One of those details I've never had to mess with, so I'm not entirely sure how to check. Here's what I tried though:

All attempts were still followed by the same error output as my first post. Obviously I'm still new to this, so it's very possible I'm just not implementing anything properly, but otherwise the terminal only displays what I pasted in my first post. Feel free to let me know if I'm actually just stupid haha

louisefindlay23 commented 2 weeks ago

Thanks, @CatharsisRobb. The error seems to be with your undefined token variable.

Have you defined it before your code to read the token? What does the token variable look like?

CatharsisRobb commented 2 weeks ago

I left all the code default, so const token = process.env.BOT_TOKEN; and all other process.env defaults in bot.js are intact. I created a tokens.env using your guide and all tokens/IDs within should be correct. On the surface, it should read the .env fine, but it feels like it just isn't choosing to for some reason.

louisefindlay23 commented 1 week ago

@CatharsisRobb, try renaming tokens.env to .env

CatharsisRobb commented 1 week ago

Unfortunately no change,

Edit: there is a hidden .gitignore file that has .env /node_modules/ lastPostID.json listed in it. Not sure if any of that is relevant but figured I'd mention it since I found it (didn't have hidden files enabled for some reason, I usually do lmao)

louisefindlay23 commented 1 week ago

@CatharsisRobb, do you have a public GitHub repo with your code?

CatharsisRobb commented 1 week ago

No, but aside from changing the token values in the .env it's all the base code cloned directly from your repo on here

louisefindlay23 commented 1 week ago

@CatharsisRobb, do you have the dotenv npm package installed and the import dotenv line at the beginning?

CatharsisRobb commented 1 week ago

require("dotenv").config(); is at the beginning and I do have the package installed. If I swap it out for import 'dotenv/config' it throws a syntax error.

louisefindlay23 commented 1 week ago

@CatharsisRobb, have you tried manually defining the token? So const token = DiscordBotToken

CatharsisRobb commented 1 week ago

OK! - that wasn't it but in trying it and getting a reference error I found a settting that had changed since the last time I'd set up a bot with Discord's Dev Portal and was basically breaking it. It boots up now!

BUT - now I'm having the exact error the OP had: TypeError: Cannot read properties of undefined (reading '0') at /home/robb/youtubecommunitybot/bot.js:28:50 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) which points to the open bracket in this line: const communityPosts = data.items[0].community;

adding console.log(data) to the code like you asked OP just returns Data Not Defined so I've got a feeling I'm doing that incorrectly. So far I'm just happy it's booting at all and I can see the bot in my server members list

louisefindlay23 commented 5 days ago

@CatharsisRobb, that's great. Can you console.log(URL) to see if your YT API URL is picked up correctly?