Skip to content

Commit

Permalink
add: merge configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Insiro committed Jul 7, 2022
1 parent fe44598 commit e8b63ba
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"private": true,
"scripts": {
"tsc": "tsc",
"build": "",
"build": "yarn tsc -p tsconfig.json",
"lint": "yarn eslint \"src/**/*\"",
"lint-fix": "yarn lint --fix",
"start": "ts-node src/index.ts",
Expand Down
33 changes: 32 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
import dotenv from 'dotenv';
dotenv.config();
import { DataSourceOptions } from 'typeorm';
import { BotServer } from './entity/BotServer';
import { NewsDate } from './entity/NewsDate';
import * as fs from 'fs';

const fileName = 'src/bot.env'
if (fs.existsSync(fileName))
dotenv.config({path:fileName});
else
dotenv.config();

export const dataSourceOption: DataSourceOptions = {
type: 'postgres',
host: process.env.dbHost || 'database host',
port: parseInt(process.env.dbPort)|| 5432,
username: process.env.dbUsername||'db user name',
database: process.env.dbName||'db name',
password: process.env.dbPWD ||'database password',
synchronize: false,
logging: false,
entities: [BotServer, NewsDate],
migrations: ['./migration/*.ts', './migration/*.js'],
subscribers: ['./subscriber/*.ts', './subscriber/*.js'],
ssl: false,
extra: {
insecureAuth: true,
ssl: {
rejectUnauthorized: false,
},
},
};


export const CyphersApiKey =
process.env.cyphersKey || 'Your Neople Open API Key';
Expand Down
28 changes: 3 additions & 25 deletions src/data-sources.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
import 'reflect-metadata';
import dotenv from 'dotenv';
import { DataSource, DataSourceOptions } from 'typeorm';
import { BotServer } from './entity/BotServer';
import { NewsDate } from './entity/NewsDate';
import { dataSourceOption } from './config';

dotenv.config();
const dataSourceOption: DataSourceOptions = {
type: 'postgres',
host: process.env.dbHost || 'database host',
port: parseInt(process.env.dbPort)|| 5432,
username: process.env.dbUsername||'db user name',
database: process.env.dbName||'db name',
password: process.env.dbPWD ||'database password',
synchronize: false,
logging: false,
entities: [BotServer, NewsDate],
migrations: ['./migration/*.ts', './migration/*.js'],
subscribers: ['./subscriber/*.ts', './subscriber/*.js'],
ssl: false,
extra: {
insecureAuth: true,
ssl: {
rejectUnauthorized: false,
},
},
};
const AppDataSource = new DataSource(dataSourceOption);

const AppDataSource = new DataSource(dataSourceOption as DataSourceOptions);
export default AppDataSource;
15 changes: 6 additions & 9 deletions src/utils/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ export const deleteGuild = async (guild: Guild): Promise<void> => {
await botServerRepository.remove(server);
};

const registerGuildStr = async (guildID: string): Promise<boolean> => {
const registerGuildStr = async (guildID: string): Promise<BotServer | null> => {
try {
const server = new BotServer();
server.serverId = guildID;
await botServerRepository.insert(server);
return true;
return server;
} catch {
return false;
return null;
}
};

export const registerGuild = async (guild: Guild): Promise<void> => {
const result = await registerGuildStr(guild.id.toString());
if (result === false) {
if (result === null) {
guild.systemChannel?.send('failed to register server');
guild.leave();
}
Expand All @@ -34,13 +34,10 @@ export const getGuildInfoStr = async (
guildID: string
): Promise<BotServer | null> => {
if (guildID === undefined || guildID === '') return null;
let server: BotServer | null = await botServerRepository.findOne({
const server: BotServer | null = await botServerRepository.findOne({
where: { serverId: guildID },
});
if (server === null) {
registerGuildStr(guildID);
server = new BotServer();
}
if (server === null) return registerGuildStr(guildID);
server.serverId = guildID;
return server;
};
Expand Down

0 comments on commit e8b63ba

Please sign in to comment.