sveltekit uses what's called server-only modules where code located under $lib/server/ can only be accessed on the server. this is where i'd put all the db and bot functionality.
for example
$lib/server/db could contain an index.ts file that sets up the db and exports functions like getUsers
const db = setUpDB(DB_CONNECTION_STRING, DB_PASSWORD); // it's been a while since i've done this
export const getUsers = () => {
return db.getUsers();
}you can import env variables from $env/public and $env/private. more info here and here
so if you had a .env file like this
DB_PASSWORD="abc123"
you'd import that like this
import { DB_PASSWORD } from '$env/static/private';then you could use it to setup your db.
// lib/server/db/index.ts
import { DB_PASSWORD } from '$env/static/private';
const db = setupDB(DB_PASSWORD); // maybe other stuff here as wellthis will be the same for setting up the bot
you can call all those twurple functions inside a module too
// lib/server/bot/index.ts
import { BOT_CREDENTIALS } from '$env/static/private'; // bot credentials are inside `.env`.
const bot = new Twurple(BOT_CREDENTIALS);
bot.addCommand(/* ... */); // or whatever needs to be done
export { bot };you can also import the db here if needed.
import { db } from '$lib/server/db';
/* bot setup */
bot.addCommand('usesDB', someFunctionThatNeedsToUseTheDb(db));you can pass the bot instance around to your server modules and load functions
// src/routes/+page.server.ts
// or any other +page.server.ts file
import { bot } from '$lib/server/bot';
export const load = async ({ params }) => {
return await bot.doWhatever();
}if you're using remote functions, this is actually a lot easier. there's a good example here. load functions will probably be phased out in favor of remote functions in future svelte versions.
sveltekit does this by leveraging vite which can do some pretty sweet things
the benefit of doing it all like this is that you never have to worry about leaking private info or secrets because sveltekit knows when you're using server code in a public route. more info about all that here