Hey everyone! I’m new to JavaScript and working with APIs. I’m trying to build a Discord bot that notifies users when a Twitch channel starts streaming. Although I’ve seen similar solutions online, I’m running into issues with getJSON.
Here’s my current index.js file:
const Discord = require('discord.js');
const commando = require('discord.js-commando');
const bot = new commando.Client();
const BotToken = 'myBotToken';
const TwitchClientID = 'myTwitchClientID';
const TwitchURL = 'https://api.twitch.tv/kraken/streams/mychannel';
const channelName = 'mychannel';
bot.registry.registerGroup('random', 'Random');
bot.registry.registerDefaults();
bot.registry.registerCommandsIn(__dirname + '/commands');
bot.login('myLoginToken');
bot.on('message', (message) => {
if (message.content === '!StreamStatus') {
checkStreamStatus((err, isLive) => {
if (err) {
message.reply('Oops, something went wrong!');
} else {
message.reply(isLive ? 'Stream is live!' : 'Stream is offline.');
}
});
}
});
function checkStreamStatus(callback) {
// Implementation needed here
}
I tried using both jQuery.getJSON and $.getJSON, but neither worked. When I issue the !StreamStatus command, the bot crashes with the message ‘getJSON is not defined’.
My package.json looks like this:
{
"name": "twitch-discord-bot",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"discord.js": "^11.1.0",
"discord.js-commando": "^0.9.0",
"jquery": "^3.2.1",
"xmlhttprequest": "^1.8.0"
}
}
Can anyone help me understand why getJSON isn’t working? Thanks in advance!
I encountered a similar challenge when developing a Discord bot for Twitch notifications. The issue lies in using jQuery methods in a Node.js environment. Instead, I’d suggest utilizing the built-in ‘https’ module for making API requests.
Here’s a modified version of your checkStreamStatus function that should work:
const https = require('https');
function checkStreamStatus(callback) {
const options = {
hostname: 'api.twitch.tv',
path: `/kraken/streams/${channelName}`,
headers: {
'Client-ID': TwitchClientID,
'Accept': 'application/vnd.twitchtv.v5+json'
}
};
https.get(options, (res) => {
let data = '';
res.on('data', (chunk) => data += chunk);
res.on('end', () => {
const parsedData = JSON.parse(data);
const isLive = parsedData.stream !== null;
callback(null, isLive);
});
}).on('error', (err) => callback(err));
}
This approach doesn’t require additional dependencies and should resolve your getJSON issue. Remember to handle potential errors and rate limiting from the Twitch API.
I ran into a similar issue when I was building my first Discord bot for Twitch notifications. The problem is that jQuery’s getJSON is designed for browser environments, not Node.js. In your server-side JavaScript, you’ll need to use a different method to make HTTP requests.
I’d recommend using the ‘node-fetch’ package instead. It’s a lightweight module that brings the Fetch API to Node.js. Here’s how you can modify your code:
-
First, install node-fetch: npm install node-fetch
-
Then, update your checkStreamStatus function:
const fetch = require('node-fetch');
function checkStreamStatus(callback) {
fetch(TwitchURL, {
headers: {
'Client-ID': TwitchClientID,
'Accept': 'application/vnd.twitchtv.v5+json'
}
})
.then(response => response.json())
.then(data => {
const isLive = data.stream !== null;
callback(null, isLive);
})
.catch(error => callback(error));
}
This should resolve your issue. Remember to keep your Twitch Client ID secret and consider using environment variables for sensitive information. Good luck with your bot!
hey there! try axios instead of getjson. install it with npm install axios and update checkstreamstatus to use axios.get with the right headers. hope it helps! lmk if u need more info:)