-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
87 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ pnpm-*.yaml | |
*.tsbuildinfo | ||
*.log | ||
*.env | ||
*.db | ||
|
||
.vscode/ | ||
.idea/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { join } from "path"; | ||
import "dotenv/config"; | ||
import { GilClient } from "../../lib/GilClient"; | ||
import { BetterSQLite3Adapter } from "../../lib/adapters/db/BetterSQLite3Adapter"; | ||
|
||
import sqlite from "better-sqlite3"; | ||
import { ConsoleAdapter } from "../../lib"; | ||
const database = sqlite("test.db"); | ||
|
||
const YokiBot = new GilClient({ | ||
token: process.env.TOKEN!, | ||
commandDirectory: join(__dirname, "..", "shared", "commands"), | ||
listenerDirectory: join(__dirname, "listeners"), | ||
loggingAdapter: new ConsoleAdapter(), | ||
databaseAdapter: new BetterSQLite3Adapter({ | ||
sqliteInstance: database, | ||
serverTable: "servers", | ||
serverIdKey: "server_id", | ||
serverStaffRolesKey: "staff_roles", | ||
roleTable: "roles", | ||
}), | ||
}); | ||
|
||
database.exec(` | ||
CREATE TABLE IF NOT EXISTS servers ( | ||
server_id TEXT PRIMARY KEY, | ||
prefix TEXT DEFAULT '!', | ||
premium_level INTEGER DEFAULT 0 | ||
); | ||
CREATE TABLE IF NOT EXISTS roles ( | ||
role_id TEXT PRIMARY KEY, | ||
server_id TEXT, | ||
role_name TEXT | ||
);`); | ||
YokiBot.start(); |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import sqlite from "better-sqlite3"; | ||
import { DatabaseAdapter, StoredRole, StoredServer } from "./DatabaseAdapter"; | ||
|
||
export class BetterSQLite3Adapter extends DatabaseAdapter { | ||
public constructor( | ||
readonly options: { | ||
sqliteInstance: sqlite.Database; | ||
serverTable: string; | ||
serverIdKey: string; | ||
serverStaffRolesKey: string; | ||
roleTable: string; | ||
}, | ||
) { | ||
super(); | ||
} | ||
|
||
public async getServer(serverId: string): Promise<StoredServer | null> { | ||
const server = this.options.sqliteInstance.prepare(`SELECT * FROM ${this.options.serverTable} WHERE ${this.options.serverIdKey} = ?`).get(serverId) as StoredServer; | ||
if (!server) return null; | ||
|
||
return { | ||
server_id: server[this.options.serverIdKey as keyof StoredServer]!, | ||
prefix: server.prefix, | ||
premium_level: server.premium_level, | ||
}; | ||
} | ||
|
||
public async createServer(serverId: string): Promise<StoredServer> { | ||
const server = this.options.sqliteInstance | ||
.prepare( | ||
`INSERT INTO ${this.options.serverTable} (${this.options.serverIdKey}, prefix, premium_level) | ||
VALUES (?, ?, ?) | ||
RETURNING *`, | ||
) | ||
.get(serverId, null, null) as StoredServer; | ||
|
||
return server; | ||
} | ||
|
||
public async getRoles(serverId: string): Promise<StoredRole[]> { | ||
const roles = this.options.sqliteInstance.prepare(`SELECT * FROM ${this.options.roleTable} WHERE server_id = ?`).all(serverId) as StoredRole[]; | ||
|
||
return roles; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters