-
-
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
8 changed files
with
114 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
version: '3' | ||
services: | ||
postgres: | ||
image: postgres:alpine3.19 | ||
ports: | ||
- '5432:5432' | ||
environment: | ||
POSTGRES_USER: guildedjs | ||
POSTGRES_PASSWORD: testpass | ||
POSTGRES_DB: guildedjs | ||
volumes: | ||
- postgres-vol:/var/lib/postgresql/data | ||
|
||
volumes: | ||
postgres-vol: |
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,49 @@ | ||
import { join } from "path"; | ||
import "dotenv/config"; | ||
import { GilClient } from "../../lib/GilClient"; | ||
|
||
import postgres from "postgres"; | ||
import { ConsoleAdapter } from "../../lib"; | ||
import { PostgresAdapter } from "../../lib/adapters/db/PostgresAdapter"; | ||
|
||
const sql = postgres({ | ||
user: "guildedjs", | ||
password: "testpass", | ||
database: "guildedjs", | ||
host: "localhost", | ||
}); | ||
|
||
const YokiBot = new GilClient({ | ||
token: process.env.TOKEN!, | ||
commandDirectory: join(__dirname, "..", "shared", "commands"), | ||
listenerDirectory: join(__dirname, "listeners"), | ||
loggingAdapter: new ConsoleAdapter(), | ||
databaseAdapter: new PostgresAdapter({ | ||
pgInstance: sql, | ||
roleTable: "roles", | ||
serverTable: "servers", | ||
serverIdKey: "server_id", | ||
}), | ||
}); | ||
|
||
async function startBot() { | ||
await sql` | ||
CREATE TABLE IF NOT EXISTS servers ( | ||
server_id TEXT PRIMARY KEY, | ||
prefix TEXT DEFAULT '!', | ||
premium_level INTEGER DEFAULT 0 | ||
); | ||
`; | ||
|
||
await sql` | ||
CREATE TABLE IF NOT EXISTS roles ( | ||
role_id TEXT PRIMARY KEY, | ||
server_id TEXT, | ||
role_name TEXT, | ||
role_level INTEGER | ||
); | ||
`; | ||
YokiBot.start(); | ||
} | ||
|
||
startBot(); |
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
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,47 @@ | ||
import postgres from "postgres"; | ||
import { DatabaseAdapter, StoredRole, StoredServer } from "./DatabaseAdapter"; | ||
|
||
export class PostgresAdapter extends DatabaseAdapter { | ||
private pg: postgres.Sql; | ||
|
||
public constructor( | ||
readonly options: { | ||
pgInstance: postgres.Sql; | ||
serverTable: string; | ||
serverIdKey: string; | ||
roleTable: string; | ||
}, | ||
) { | ||
super(); | ||
this.pg = options.pgInstance; | ||
} | ||
|
||
public async getServer(serverId: string): Promise<StoredServer | null> { | ||
const [server] = await this.pg` | ||
SELECT * FROM ${this.pg(this.options.serverTable)} WHERE ${this.pg(this.options.serverIdKey)} = ${serverId} | ||
`; | ||
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] = await this.pg` | ||
INSERT INTO ${this.pg(this.options.serverTable)} (${this.pg(this.options.serverIdKey)}) | ||
VALUES (${serverId}) | ||
RETURNING * | ||
`; | ||
return server as StoredServer; | ||
} | ||
|
||
public async getRoles(serverId: string): Promise<StoredRole[]> { | ||
const roles = await this.pg<StoredRole[]>` | ||
SELECT * FROM ${this.pg(this.options.roleTable)} WHERE ${this.pg("server_id")} = ${serverId} | ||
`; | ||
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