Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(discord-bot): Implement Discord Bot Prototype #468

Open
wants to merge 14 commits into
base: beta
Choose a base branch
from
2 changes: 1 addition & 1 deletion apps/discord-bot/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ GA4_PROPERTY_ID=
GOOGLE_APPLICATION_CREDENTIALS=./google_application_credentials.json

# Database
SQLITE_DATABASE_URL=file:./database.sqlite
DATABASE_CONNECTION_URL=database.sqlite
5 changes: 5 additions & 0 deletions apps/discord-bot/src/command/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ICommand } from './ICommand'
import { PingCommand } from './ping'
import { RegisterReportChannelCommand } from './registerReportChannel'

export const commands: ICommand[] = [PingCommand, RegisterReportChannelCommand]
6 changes: 3 additions & 3 deletions apps/discord-bot/src/command/registerReportChannel.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { ChatInputCommandInteraction, Client, TextChannel } from 'discord.js'
import { ChatInputCommandInteraction } from 'discord.js'

import { CUGetReg } from '../core/CUGetReg'
import { ICommand } from './ICommand'

export const RegisterReportChannelCommand: ICommand = {
name: 'register_report_channel',
description: 'Register Channel for Retriving Reports',
description: 'channel to receive reports',
execute: async (client: CUGetReg, interaction: ChatInputCommandInteraction): Promise<void> => {
await client.db.saveRegisterReportChannel(interaction.guildId, interaction.channelId)
console.log(await client.db.getAllReportChannels())

interaction.reply('Channel Registered!')
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ export interface Configuration {
token: string
clientID: string
}
googleAnalytic: {
googleAnalytics: {
GA4_PROPERTY_ID: string
}
database: {
connectionURL: string
}
}

export const configuration: Configuration = {
discord: {
token: process.env.DISCORD_TOKEN,
clientID: process.env.DISCORD_CLIENT_ID,
},
googleAnalytic: {
googleAnalytics: {
GA4_PROPERTY_ID: process.env.GA4_PROPERTY_ID,
},
database: {
connectionURL: process.env.DATABASE_CONNECTION_URL,
},
}
26 changes: 13 additions & 13 deletions apps/discord-bot/src/core/CUGetReg.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { AttachmentBuilder, Client, GatewayIntentBits, TextChannel } from 'discord.js'
import * as cron from 'node-cron'

import { commands } from '../command'
import { ICommand } from '../command/ICommand'
import { database } from '../database'
import { IDatabase } from '../database/IDatabase'
import { HookFunction } from '../hook/hook'
import { hooks } from '../hook'
import { HookFunction } from '../hook/type'
import { scheduler } from '../scheduler'
import { IScheduler } from '../scheduler/IScheduler'
import { CUGetRegCommands } from './CUGetRegCommands'
import { CUGetRegDatabase } from './CUGetRegDatabase'
import { CUGetRegHooks } from './CUGetRegHooks'
import { CUGetRegScheduler } from './CUGetRegScheduler'

export class CUGetReg extends Client {
private commandList = new Map<string, ICommand>()
private database: IDatabase = CUGetRegDatabase
private _commandList = new Map<string, ICommand>()
private _database: IDatabase = database

constructor(token: string) {
super({ intents: [GatewayIntentBits.Guilds] })
this.login(token)
this.addHook(CUGetRegHooks)
this.registerCommand(CUGetRegCommands)
this.registerScheduler(CUGetRegScheduler)
this.addHook(hooks)
this.registerCommand(commands)
this.registerScheduler(scheduler)
}

addHook(hookFunctions: HookFunction[]): void {
Expand All @@ -28,7 +28,7 @@ export class CUGetReg extends Client {

registerCommand(commands: ICommand[]): void {
commands.forEach((command) => {
this.commandList.set(command.name, command)
this._commandList.set(command.name, command)
})
}

Expand All @@ -52,10 +52,10 @@ export class CUGetReg extends Client {
}

get commands(): Map<string, ICommand> {
return this.commandList
return this._commandList
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i meant get commands() should match the private variable name _commands not _commandList


get db(): IDatabase {
return this.database
return this._database
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same goes here

}
}
5 changes: 0 additions & 5 deletions apps/discord-bot/src/core/CUGetRegCommands.ts

This file was deleted.

4 changes: 0 additions & 4 deletions apps/discord-bot/src/core/CUGetRegDatabase.ts

This file was deleted.

5 changes: 0 additions & 5 deletions apps/discord-bot/src/core/CUGetRegHooks.ts

This file was deleted.

4 changes: 0 additions & 4 deletions apps/discord-bot/src/core/CUGetRegScheduler.ts

This file was deleted.

4 changes: 4 additions & 0 deletions apps/discord-bot/src/database/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { IDatabase } from './IDatabase'
import { SQLiteDatabaseDriver } from './sqlite/SQLiteDatabaseDriver'

export const database: IDatabase = new SQLiteDatabaseDriver()
3 changes: 2 additions & 1 deletion apps/discord-bot/src/database/sqlite/SQLiteDatabaseDriver.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { open } from 'sqlite'
import * as sqlite3 from 'sqlite3'

import { configuration } from '../../config'
import { IDatabase } from '../IDatabase'

const DB_FILE_NAME = 'database.sqlite'
const DB_FILE_NAME = configuration.database.connectionURL

type ReportChannel = {
guild_id?: string
Expand Down
4 changes: 2 additions & 2 deletions apps/discord-bot/src/deploy-commands.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { REST, Routes, SlashCommandBuilder } from 'discord.js'

import { configuration as config } from './config/config'
import { CUGetRegCommands } from './core/CUGetRegCommands'
import { commands as CUGetRegCommands } from './command'
import { configuration as config } from './config'

const commands = CUGetRegCommands.map((command) => {
return new SlashCommandBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GoogleAnalyticData } from './google-analytic'
import { GoogleAnalyticsData } from './google-analytics'

export class GoogleAnalyticDataExtended extends GoogleAnalyticData {
export class GoogleAnalyticsDataExtended extends GoogleAnalyticsData {
async getByDate() {
return this.getMetric(['activeUsers'], ['date'])
}
Expand Down Expand Up @@ -50,4 +50,4 @@ export class GoogleAnalyticDataExtended extends GoogleAnalyticData {
}
}

export const googleAnalyticDataExtended = new GoogleAnalyticDataExtended()
export const googleAnalyticsDataExtended = new GoogleAnalyticsDataExtended()
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { BetaAnalyticsDataClient } from '@google-analytics/data'
import { google } from '@google-analytics/data/build/protos/protos'

import { configuration as config } from '../config/config'
import { googleAnalyticDimensions, googleAnalyticMetrics } from './type'
import { configuration as config } from '../config'
import { googleAnalyticsDimensions, googleAnalyticsMetrics } from './type'

export class GoogleAnalyticData {
export class GoogleAnalyticsData {
private analyticsDataClient: BetaAnalyticsDataClient
private property: string

constructor() {
this.analyticsDataClient = new BetaAnalyticsDataClient()
this.property = `properties/${config.googleAnalytic.GA4_PROPERTY_ID}`
this.property = `properties/${config.googleAnalytics.GA4_PROPERTY_ID}`
}

sortByDimentionValue(data: google.analytics.data.v1beta.IRow[]) {
Expand Down Expand Up @@ -48,8 +48,8 @@ export class GoogleAnalyticData {
}

async getMetric(
metrics: googleAnalyticMetrics[],
dimensions: googleAnalyticDimensions[],
metrics: googleAnalyticsMetrics[],
dimensions: googleAnalyticsDimensions[],
startDate = 'yesterday',
endDate = 'yesterday'
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type googleAnalyticMetrics = 'activeUsers' | 'newUsers' | 'totalUsers'
export type googleAnalyticDimensions =
export type googleAnalyticsMetrics = 'activeUsers' | 'newUsers' | 'totalUsers'
export type googleAnalyticsDimensions =
| 'city'
| 'contentType'
| 'country'
Expand Down
2 changes: 1 addition & 1 deletion apps/discord-bot/src/hook/command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChatInputCommandInteraction, Events } from 'discord.js'

import { CUGetReg } from '../core/CUGetReg'
import { HookFunction } from './hook'
import { HookFunction } from './type'

export const CommandHook: HookFunction = (client: CUGetReg): void => {
client.on(Events.InteractionCreate, async (interaction: ChatInputCommandInteraction) => {
Expand Down
5 changes: 5 additions & 0 deletions apps/discord-bot/src/hook/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CommandHook } from './command'
import { ReadyHook } from './ready'
import { HookFunction } from './type'

export const hooks: HookFunction[] = [ReadyHook, CommandHook]
2 changes: 1 addition & 1 deletion apps/discord-bot/src/hook/ready.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Events } from 'discord.js'

import { CUGetReg } from '../core/CUGetReg'
import { HookFunction } from './hook'
import { HookFunction } from './type'

export const ReadyHook: HookFunction = (client: CUGetReg): void => {
client.once(Events.ClientReady, async () => {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion apps/discord-bot/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { configuration as config } from './config/config'
import { configuration as config } from './config'
import { CUGetReg } from './core/CUGetReg'

const token = config.discord.token
Expand Down
4 changes: 4 additions & 0 deletions apps/discord-bot/src/scheduler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { IScheduler } from './IScheduler'
import { ReportChannelScheduler } from './reportChannelScheduler'

export const scheduler: IScheduler[] = [ReportChannelScheduler]
12 changes: 6 additions & 6 deletions apps/discord-bot/src/scheduler/reportChannelScheduler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CUGetReg } from '../core/CUGetReg'
import { googleAnalyticDataExtended as googleAnalytic } from '../google-analytic/google-analytic-extended'
import { googleAnalyticsDataExtended as googleAnalytics } from '../google-analytics/google-analytics-extended'
import { lineChart } from '../utilities/generateChart'
import { IScheduler } from './IScheduler'

Expand All @@ -9,9 +9,9 @@ export const ReportChannelScheduler: IScheduler = {
callbackFunction: async (client: CUGetReg) => {
try {
const allReportChannels = await client.db.getAllReportChannels()
// const data = await googleAnalytic.getByDate().then((res) => res.rows[0])
const data = googleAnalytic.sortByDimentionValue(
await googleAnalytic.getByDate7Days().then((res) => res.rows)
// const data = await googleAnalytics.getByDate().then((res) => res.rows[0])
const data = googleAnalytics.sortByDimentionValue(
await googleAnalytics.getByDate7Days().then((res) => res.rows)
)

allReportChannels.forEach(async (reportChannel) => {
Expand All @@ -22,9 +22,9 @@ export const ReportChannelScheduler: IScheduler = {
client.sendImage(
reportChannel,
await lineChart(
googleAnalytic.extractDimensionValueAsDayMonthDate(data),
googleAnalytics.extractDimensionValueAsDayMonthDate(data),
'Active Users',
googleAnalytic.extractMetricValueAsInt(data),
googleAnalytics.extractMetricValueAsInt(data),
'CU Get Reg',
'Date',
'Users'
Expand Down
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@
"styled-components": "^5.3.6",
"tslib": "^2.4.1",
"uuid": "^8.3.2",
"zustand": "^4.1.5"
"zustand": "^4.1.5",
"tough-cookie": "^4.0.0",
"winston": "^3.8.2"
},
"devDependencies": {
"@graphql-codegen/cli": "^2.16.2",
Expand All @@ -133,12 +135,6 @@
"@nestjs/testing": "^9.2.1",
"@next/bundle-analyzer": "^13.1.1",
"@next/env": "^13.1.1",
"slate-react": "^0.82.0",
"styled-components": "^5.3.5",
"tough-cookie": "^4.0.0",
"tslib": "^2.4.0",
"uuid": "^8.3.2",
"winston": "^3.8.2",
"zustand": "^4.0.0",
"@nrwl/cli": "15.0.4",
"@nrwl/cypress": "15.0.4",
Expand Down
Loading