logging in to api.shockbs.is-a.dev
About
the package (opens in a new tab) requires a token to access the api for its functionalities
Calling connect()
import shockbs from "shockbs";
// const shockbs = require("shockbs");
try {
shockbs.connect(process.env.ShockBS_APIKEY);
} catch(err) {
console.error("Unable to log in, reason: "+err.message);
}
Checking if it is already logged in
Please treat the shockbs package as global imports/variable!
import shockbs from "shockbs";
// const shockbs = require("shockbs");
const loggedIn = shockbs.connected();
if (loggedIn === true) {
console.log("Already Logged In");
} else {
console.error("[Shock API] Not Logged In Yet");
}
FAQs
Why are classes and functions still throwing erros even it is already connected?
in this case the package will throws an error saying: [API_NOT_CONNECTED] API not attempted to connect or connect() is not awaited, read: https://docs.shockbs.is-a.dev/guides/login#why-are-classes-and-functions-still-throwing-erros-even-ive-already-logged-in
this happens due to a mistake in your code,
are you doing this:
import {connect, AiChat, example} from "shockbs";
connect(process.env.ShockToken);
// started to call classes and functions without awaiting connect() to finish
const class = new AiChat({...});
const function = example()
this is wrong, you are not wating login() to complete before calling the class or function.
instead, you should wait connect() to finish
The best solution is to call connect() on the first line of your main file! This is because it will probably be completed before the classes and functions you've called in other files start executing.
or try to do like this if you need to write them together:
import shockbs from "shockbs";
let gptChat;
(async()=> {
await shockbs.login(process.env.ShockToken);
gptChat = new shockbs.gptChat({...});
})();
Thats it!