diff --git a/discord/bot.ts b/discord/bot.ts index e7a199f..c0313a4 100644 --- a/discord/bot.ts +++ b/discord/bot.ts @@ -13,10 +13,9 @@ import { TextChannel, User } from 'discord.js'; +import { logError, logInfo } from '../logging'; import { db, - logInfo, - logError, openDb, reactionEmojis, recordReaction, diff --git a/discord/sync.ts b/discord/sync.ts index 7dee347..809ae53 100644 --- a/discord/sync.ts +++ b/discord/sync.ts @@ -5,7 +5,8 @@ */ import { Client, Events, GatewayIntentBits, IntentsBitField, Partials } from 'discord.js'; -import { db, logInfo, openDb, reactionEmojis, sync } from './util'; +import { logInfo } from '../logging'; +import { db, openDb, reactionEmojis, sync } from './util'; const client = new Client({ intents: [GatewayIntentBits.MessageContent, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMessages], diff --git a/discord/util.ts b/discord/util.ts index f791d17..69f4316 100644 --- a/discord/util.ts +++ b/discord/util.ts @@ -9,6 +9,7 @@ import { get as httpGet } from 'https'; import { Database, open } from 'sqlite'; import { Database as Database3 } from 'sqlite3'; import 'dotenv/config'; +import { logError, logInfo, logWarn } from '../logging'; import { ScoreboardMessageRow } from '../models'; @@ -16,22 +17,6 @@ const reactionEmojis: string[] = process.env.REACTIONS.split(','); let db: Database = null; -function curDateStr() { - return new Date().toJSON().replace('T', ' ').replace('Z', ''); -} - -function logInfo(...data) { - console.log(`[${curDateStr()}] ${data.join(' ')}`); -} - -function logWarn(...data) { - console.warn(`[${curDateStr()}] ${data.join(' ')}`); -} - -function logError(...data) { - console.error(`[${curDateStr()}] ${data.join(' ')}`); -} - async function openDb() { db = await open({ filename: 'db.sqlite', @@ -178,4 +163,4 @@ async function sync(guilds: GuildManager) { } } -export { db, clearDb, logError, logInfo, logWarn, openDb, reactionEmojis, recordReaction, sync }; +export { db, clearDb, openDb, reactionEmojis, recordReaction, sync }; diff --git a/logging.ts b/logging.ts new file mode 100644 index 0000000..471c550 --- /dev/null +++ b/logging.ts @@ -0,0 +1,22 @@ +/** + * logging.ts + * Standardized logging helper functions + */ + +function curDateStr() { + return new Date().toJSON().replace('T', ' ').replace('Z', ''); +} + +function logInfo(...data) { + console.log(`[${curDateStr()}] ${data.join(' ')}`); +} + +function logWarn(...data) { + console.warn(`[${curDateStr()}] ${data.join(' ')}`); +} + +function logError(...data) { + console.error(`[${curDateStr()}] ${data.join(' ')}`); +} + +export { logInfo, logWarn, logError }; diff --git a/server.ts b/server.ts index a773354..159056e 100644 --- a/server.ts +++ b/server.ts @@ -7,8 +7,8 @@ import { Database as Database3 } from 'sqlite3'; import { Database, open } from 'sqlite'; import express = require('express'); import 'dotenv/config'; +import { logInfo } from './logging'; import { ScoreboardMessageRow, ScoreboardUserRow } from './models'; -import { logInfo } from './discord/util'; const app = express(); app.use(express.static('public'));