Skip to content

Install & Setup

This section will cover setting up your project, installing the required dependencies and ensuring we are ready to code.

Installation

To start with go to the parent folder of where you want your project to be created and then run the following command:

zsh
bun create @sodacore@alpha

This will prompt you for some information about your project, go ahead and fill this in. Once you get to the plugin selection, ensure you select HTTP, WebSockets, Prisma, Discord and I18n plugins. Then press enter. This will create the folders and initialise the project with some example code you can work from.

Once the project is created, open your project folder in your IDE.

Congrats, you now have a basic project setup, if you were to run bun dev in your project folder, you will see some logging output and you can then go to your browser and

Database setup

To setup our database, we are going to use Prisma ORM to deal with this, and use the SQLite database for simplicity, of course you can use any database you want, but for this guide we will use SQLite.

To start with, the @sodacore/prisma plugin has already installed the Prisma CLI for us, so we need to run simply run:

zsh
bun prisma init --datasource-provider sqlite

This command defines the preconfigured provider as sqlite for us.

This will create a prisma folder in your project, let's open up the ./prisma/schema.prisma file and take a look, let's create a simple schema for our to-do application.

prisma
generator client {
  provider = "prisma-client-js"
  output   = "../src/generated/prisma"
}

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

model Todos { 
  id        Int      @id @default(autoincrement()) 
  title     String
  completed Boolean  @default(false) 
  createdAt DateTime @default(now()) 
  updatedAt DateTime @updatedAt
} 

This will cover our basic to-dos offering us a title, a completed status, and timestamps for creation and updates.

Environment Variables

Next, we need to define some environment variables for our project, because we ran the prisma init command earlier, it has already created a .env file for us, and tweak like so:

ini
DATABASE_URL="file:./db.sqlite"
DISCORD_TOKEN="YOUR_DISCORD_BOT_TOKEN"
DISCORD_CLIENT_ID="YOUR_DISCORD_CLIENT_ID"
DISCORD_GUILD_ID="YOUR_DISCORD_GUILD_ID"
HTTP_PORT="3000"
TRANSLATIONS_PATH="./translations"

This should cover everything we need for our project, you can change the HTTP_PORT to whatever you want, but for this guide we will use 3000.

The reason we have used the DISCORD_GUILD_ID is this means you can install your bot commands to a specific guild, you can avoid this if you want, but when you install globally, it can take up to an hour for the commands to be available, so this is a good way to test your commands quickly.

Setting up the framework

Now we have our project setup, let's write some code to initialise the framework and load our plugins.

Go to the src/main.ts file and edit the contents like so:

typescript
import { Application } from '@sodacore/core';
import { env } from 'bun';
import { resolve } from 'node:path';
import process from 'node:process';
import HttpPlugin from '@sodacore/http';
import DiscordPlugin from '@sodacore/discord';
import PrismaPlugin from '@sodacore/prisma';
import I18nPlugin from '@sodacore/i18n';

// Initialise application.
const app = new Application({
	autowire: true,
	basePath: env.SODACORE_ENV === 'prod'
		? resolve(process.cwd(), './dist')
		: undefined,
});

// Install the HTTP plugin.
app.use(new HttpPlugin({
	port: env.HTTP_PORT ? parseInt(env.HTTP_PORT) : 3000,
}));

// Install the Discord plugin.
app.use(new DiscordPlugin({
	token: env.DISCORD_TOKEN,
	clientId: env.DISCORD_CLIENT_ID,
	guildId: env.DISCORD_GUILD_ID,
}));

// Install the I18n plugin.
app.use(new I18nPlugin({
	translationsPath: env.TRANSLATIONS_PATH,
	defaultLang: 'en-GB',
}));

// Install the Prisma plugin.
app.use(new PrismaPlugin());

// Start the application.
app.start().catch(console.error);

Next Steps

Now we have done all of this our project should be in a good state to start coding.

Released under the Apache-2.0 License.