generated from QuantGeekDev/ts-tg-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from QuantGeekDev/bugfix/dist
Bugfix/dist
- Loading branch information
Showing
21 changed files
with
192 additions
and
148 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,30 +1,33 @@ | ||
// TODO: Use path aliases | ||
import { loadEnv } from '../helpers/load-env.js' | ||
import { validateEnv } from '../helpers/validate-env.js' | ||
import { startBot } from './bot.js' | ||
import { connectToDb } from './database.js' | ||
import { loadEnv } from '../helpers/load-env.js'; | ||
import { validateEnv } from '../helpers/validate-env.js'; | ||
import { startBot } from './bot.js'; | ||
import { connectToDb } from './database.js'; | ||
import debugCreator from 'debug'; | ||
|
||
const debug = debugCreator('app:'); | ||
|
||
export async function startApp() { | ||
debug('Starting app...'); | ||
try { | ||
loadEnv() | ||
validateEnv(['TOKEN', 'DB_CONNECTION_STRING']) | ||
loadEnv(); | ||
validateEnv(['TELEGRAM_TOKEN', 'DB_CONNECTION_STRING']); | ||
} catch (error) { | ||
console.error('Error occurred while loading environment:', error) | ||
process.exit(1) | ||
console.error('Error occurred while loading environment:', error); | ||
process.exit(1); | ||
} | ||
|
||
let database | ||
let database; | ||
try { | ||
database = await connectToDb() | ||
database = await connectToDb(); | ||
} catch (error) { | ||
console.error('Error occurred while connecting to the database:', error) | ||
process.exit(2) | ||
console.error('Error occurred while connecting to the database:', error); | ||
process.exit(2); | ||
} | ||
|
||
try { | ||
await startBot(database) | ||
await startBot(database); | ||
} catch (error) { | ||
console.error('Error occurred while starting the bot:', error) | ||
process.exit(3) | ||
console.error('Error occurred while starting the bot:', error); | ||
process.exit(3); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { MongoClient } from 'mongodb' | ||
import type { Chat, Database, User } from '../types/database.js' | ||
import { MongoClient } from 'mongodb'; | ||
import type { Chat, Database, User } from '../types/database.js'; | ||
|
||
export async function connectToDb() { | ||
const client = new MongoClient(process.env.DB_CONNECTION_STRING) | ||
await client.connect() | ||
const mongoDb = client.db() | ||
const user = mongoDb.collection<User>('user') | ||
const chat = mongoDb.collection<Chat>('chat') | ||
const database: Database = { user, chat } | ||
return database | ||
const client = new MongoClient(process.env.DB_CONNECTION_STRING); | ||
await client.connect(); | ||
const mongoDb = client.db(); | ||
const user = mongoDb.collection<User>('user'); | ||
const chat = mongoDb.collection<Chat>('chat'); | ||
const database: Database = { user, chat }; | ||
return database; | ||
} |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { I18n } from '@grammyjs/i18n' | ||
import { I18n } from '@grammyjs/i18n'; | ||
|
||
export function initLocaleEngine(path: string, defaultLanguage = 'en') { | ||
const i18n = new I18n({ | ||
directory: path, | ||
defaultLanguage: defaultLanguage, | ||
defaultLanguageOnMissing: true, | ||
useSession: true | ||
}) | ||
return i18n | ||
}); | ||
return i18n; | ||
} |
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { Composer } from 'grammy' | ||
import type { CustomContext } from '../types/context.js' | ||
import { Composer } from 'grammy'; | ||
import type { CustomContext } from '../types/context.js'; | ||
|
||
export const startController = new Composer<CustomContext>() | ||
export const startController = new Composer<CustomContext>(); | ||
startController.command('start', async ctx => { | ||
await ctx.text('start', { | ||
name: ctx.entities.user.name, | ||
chatName: ctx.entities.chat?.title ?? 'PM' | ||
}) | ||
}) | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { Composer } from 'grammy' | ||
import type { CustomContext } from '../types/context.js' | ||
import { Composer } from 'grammy'; | ||
import type { CustomContext } from '../types/context.js'; | ||
|
||
export const stopController = new Composer<CustomContext>() | ||
export const stopController = new Composer<CustomContext>(); | ||
stopController.command('stop', async ctx => { | ||
await ctx.text('stop', { | ||
name: ctx.entities.user.name, | ||
chatName: ctx.entities.chat?.title ?? 'PM' | ||
}) | ||
}) | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
export declare global { | ||
namespace NodeJS { | ||
interface ProcessEnv { | ||
TOKEN: string | ||
DB_CONNECTION_STRING: string | ||
TELEGRAM_TOKEN: string; | ||
DB_CONNECTION_STRING: string; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import dotenv from 'dotenv' | ||
import { resolvePath } from './resolve-path.js' | ||
import dotenv from 'dotenv'; | ||
import { resolvePath } from './resolve-path.js'; | ||
|
||
export function loadEnv(configPath = '.env') { | ||
const fullPath = resolvePath(import.meta.url, configPath) | ||
export function loadEnv(configPath = '../../.env') { | ||
const fullPath = resolvePath(import.meta.url, configPath); | ||
dotenv.config({ | ||
path: fullPath | ||
}) | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import pathModule from 'path' | ||
import { fileURLToPath } from 'url' | ||
import pathModule from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
export function resolvePath(localPath: string, targetPath: string) { | ||
const __filename = fileURLToPath(localPath) | ||
const __dirname = pathModule.dirname(__filename) | ||
return pathModule.join(__dirname, targetPath) | ||
const __filename = fileURLToPath(localPath); | ||
const __dirname = pathModule.dirname(__filename); | ||
return pathModule.join(__dirname, targetPath); | ||
} |
Oops, something went wrong.