diff --git a/bbLibConfig.example.ts b/bbLibConfig.example.ts index a23dcc7..9ac7d99 100644 --- a/bbLibConfig.example.ts +++ b/bbLibConfig.example.ts @@ -1,8 +1,8 @@ -import { BBLibConfig } from './src/types/bbLibConfig.d.ts' -export const bbLibConfig: BBLibConfig = { - 'name': 'BBLib', - 'version': '0.0.0', - 'db': { // Settings for the MySQL database, this is required for use with the API, if you do not want to set this up, you will need to rip all code relating to the DB out of the bot +import { BBLibConfig } from './src/types/bbLibConfig.d.ts'; +const bbLibConfig: BBLibConfig = { + 'name': 'BBLib', // Name of the lib + 'version': '0.0.0', // Version of the lib + 'db': { // Settings for the MySQL database, used only for setup 'host': '', // IP address for the db, usually localhost 'localhost': '', // IP address for a secondary OPTIONAL local testing DB, usually also is localhost, but depends on your dev environment 'port': 3306, // Port for the db @@ -11,3 +11,5 @@ export const bbLibConfig: BBLibConfig = { 'name': '', // Name of the database Schema to use for the bot }, }; + +export default bbLibConfig; diff --git a/db/dbClient.ts b/db/dbClient.ts new file mode 100644 index 0000000..99fc050 --- /dev/null +++ b/db/dbClient.ts @@ -0,0 +1,12 @@ +import bbLibConfig from '../bbLibConfig.ts'; +import { Client } from '../deps.ts'; + +const dbClient = await new Client().connect({ + hostname: bbLibConfig.db.host, + port: bbLibConfig.db.port, + db: bbLibConfig.db.name, + username: bbLibConfig.db.username, + password: bbLibConfig.db.password, +}); + +export default dbClient; diff --git a/db/initialize.ts b/db/initialize.ts index 24a849b..00f6b6a 100644 --- a/db/initialize.ts +++ b/db/initialize.ts @@ -1,7 +1,8 @@ -// This file will create all tables for the groupup schema +// This file will create all tables for the bblib schema // DATA WILL BE LOST IF DB ALREADY EXISTS, RUN AT OWN RISK import bbLibConfig from '../bbLibConfig.ts'; +import dbClient from './dbClient.ts'; console.log('Attempting to create DB'); await dbClient.execute(`CREATE SCHEMA IF NOT EXISTS ${bbLibConfig.db.name};`); @@ -12,9 +13,8 @@ console.log('Attempt to drop all tables'); await dbClient.execute(`DROP VIEW IF EXISTS db_size;`); await dbClient.execute(`DROP PROCEDURE IF EXISTS INC_CNT;`); await dbClient.execute(`DROP TABLE IF EXISTS command_cnt;`); -await dbClient.execute(`DROP TABLE IF EXISTS guild_settings;`); -await dbClient.execute(`DROP TABLE IF EXISTS active_events;`); -await dbClient.execute(`DROP TABLE IF EXISTS custom_activities;`); +await dbClient.execute(`DROP TABLE IF EXISTS banned_guilds`); +await dbClient.execute(`DROP TABLE IF EXISTS banned_users`); console.log('Tables dropped'); console.log('Attempting to create table command_cnt'); @@ -35,61 +35,32 @@ await dbClient.execute(` ) BEGIN declare oldCnt bigint unsigned; - set oldCnt = (SELECT count FROM command_cnt WHERE command = cmd); - UPDATE command_cnt SET count = oldCnt + 1 WHERE command = cmd; + set oldCnt = (SELECT count FROM ${bbLibConfig.db.name}.command_cnt WHERE command = cmd); + UPDATE ${bbLibConfig.db.name}.command_cnt SET count = oldCnt + 1 WHERE command = cmd; END `); console.log('Stored Procedure created'); -console.log('Attempting to create table guild_settings'); +console.log('Attempting to create table banned_guilds'); await dbClient.execute(` - CREATE TABLE guild_settings ( - guildId bigint unsigned NOT NULL, - lfgChannelId bigint unsigned NOT NULL, - managerRoleId bigint unsigned NOT NULL, - logChannelId bigint unsigned NOT NULL, - PRIMARY KEY (guildId, lfgChannelId) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -`); -console.log('Table created'); - -console.log('Attempting to create table active_events'); -await dbClient.execute(` - CREATE TABLE active_events ( - messageId bigint unsigned NOT NULL, - channelId bigint unsigned NOT NULL, - guildId bigint unsigned NOT NULL, - ownerId bigint unsigned NOT NULL, - eventTime datetime NOT NULL, - notifiedFlag tinyint(1) NOT NULL DEFAULT 0, - lockedFlag tinyint(1) NOT NULL DEFAULT 0, - PRIMARY KEY (messageId, channelId) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -`); -console.log('Table created'); -/** - * notifiedFlag - * 0 = Not notified - * 1 = Notified Successfully - * -1 = Failed to notify - * lockedFlag - * 0 = Not locked - * 1 = Locked Successfully - * -1 = Failed to lock - * - * If both are -1, the event failed to delete - */ - -console.log('Attempting to create table custom_activities'); -await dbClient.execute(` - CREATE TABLE custom_activities ( - id int unsigned NOT NULL AUTO_INCREMENT, - guildId bigint unsigned NOT NULL, - activityTitle char(35) NOT NULL, - activitySubtitle char(50) NOT NULL, - maxMembers tinyint NOT NULL, + CREATE TABLE banned_guilds ( + id bigint unsigned NOT NULL, + soft tinyint(1) NOT NULL, + hard tinyint(1) NOT NULL, PRIMARY KEY (id), - UNIQUE KEY custom_activities_id_UNIQUE (id) + UNIQUE KEY banned_guilds_id_UNIQUE (id) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +`); +console.log('Table created'); + +console.log('Attempting to create table banned_users'); +await dbClient.execute(` + CREATE TABLE banned_users ( + id bigint unsigned NOT NULL, + soft tinyint(1) NOT NULL, + hard tinyint(1) NOT NULL, + PRIMARY KEY (id), + UNIQUE KEY banned_users_id_UNIQUE (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; `); console.log('Table created'); @@ -104,7 +75,7 @@ await dbClient.execute(` table_rows AS "rows" FROM information_schema.TABLES WHERE - table_schema = "${config.db.name}" + table_schema = "${bbLibConfig.db.name}" AND table_name <> "db_size"; `); console.log('View Created'); diff --git a/db/populateDefaults.ts b/db/populateDefaults.ts new file mode 100644 index 0000000..cb0f0b5 --- /dev/null +++ b/db/populateDefaults.ts @@ -0,0 +1,22 @@ +// This file will populate the tables with default values +import bbLibConfig from '../bbLibConfig.ts'; +import dbClient from './dbClient.ts'; + +console.log('Attempting to insert default actions into command_cnt'); +const actions = [ + 'cmd-ban-guild-soft', + 'cmd-ban-guild-hard', + 'cmd-ban-user-soft', + 'cmd-ban-user-hard', + 'cmd-unban-guild', + 'cmd-unban-user', +]; +for (const action of actions) { + await dbClient.execute(`INSERT INTO ${bbLibConfig.db.name}.command_cnt(command) values(?)`, [action]).catch((e) => { + console.log(`Failed to insert into database`, e); + }); +} +console.log('Insertion done'); + +await dbClient.close(); +console.log('Done!'); diff --git a/deno.json b/deno.json index ce9e5f1..669f9d8 100644 --- a/deno.json +++ b/deno.json @@ -13,8 +13,8 @@ "db/", "mod.ts", "deps.ts", - "config.ts", - "config.example.ts" + "bbLibConfig.ts", + "bbLibConfig.example.ts" ], "exclude": [] }, @@ -34,8 +34,8 @@ "db/", "mod.ts", "deps.ts", - "config.ts", - "config.example.ts" + "bbLibConfig.ts", + "bbLibConfig.example.ts" ], "exclude": [], "useTabs": true, diff --git a/src/db.ts b/src/db.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/types/bbLibConfig.d.ts b/src/types/bbLibConfig.d.ts index 28e197d..b08cf64 100644 --- a/src/types/bbLibConfig.d.ts +++ b/src/types/bbLibConfig.d.ts @@ -1,7 +1,7 @@ import { DBConfig } from './config.d.ts'; export interface BBLibConfig { - name: string; - version: string; - db: DBConfig; + name: string; + version: string; + db: DBConfig; } diff --git a/src/types/config.d.ts b/src/types/config.d.ts index a184e9d..5ff840b 100644 --- a/src/types/config.d.ts +++ b/src/types/config.d.ts @@ -35,6 +35,7 @@ export interface DBConfig { username: string; password: string; name: string; + commonName?: string; } // Links Config, holds list of named strings that are used in many places