Skip to content

Commit

Permalink
feat: The big #$!? update
Browse files Browse the repository at this point in the history
  • Loading branch information
KararTY committed Aug 10, 2020
1 parent 8d29038 commit 41d620c
Show file tree
Hide file tree
Showing 28 changed files with 1,639 additions and 39 deletions.
14 changes: 14 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,17 @@ HOST=

# development, production
NODE_ENV=

# Logger level, defaults to info. Set this to debug to get more information.
LOG_LEVEL=

# Command prefix. Default is '@@'. Make sure it's set or there will be errors.
COMMAND_PREFIX=@@

# PROGRAM WILL ERROR IF NOT SET.
TWITCH_CLIENT_TOKEN=
TWITCH_CLIENT_SECRET=
TWITCH_BOT_ACCESS_TOKEN=
TWITCH_BOT_REFRESH_TOKEN=
TWITCH_BOT_NAME=
TWITCH_BOT_ID=
59 changes: 51 additions & 8 deletions Index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,58 @@
// Load env variables
import { env } from '@adonisjs/env/build/standalone'
import { readFileSync } from 'fs'
import { Logger } from '@adonisjs/logger/build/standalone'
import { TwitchAuth } from 'befriendlier-shared'
import { readdirSync, readFileSync } from 'fs'
import path from 'path'
import TwitchConfig from './config/Twitch'
import WSConfig from './config/Ws'
import packageJSON from './package.json'
import Twitch from './src/Twitch'
import Ws from './src/Ws'

env.process(readFileSync(path.join(__dirname, '.env'), 'utf-8'))

// Start Ws client.
;(() => new Ws(
`ws://${env.getOrFail('HOST') as string}:${env.getOrFail('PORT') as string}`,
{
'user-agent': `BEFRIENDLIER-BOT-${Date.now()}`,
},
))()
const logger = new Logger({
name: 'befriendlier-bot',
enabled: true,
level: typeof env.get('LOG_LEVEL') === 'string' ? String(env.get('LOG_LEVEL')) : 'info',
prettyPrint: env.get('NODE_ENV') === 'development',
})

// Initialize config values for WS.
const wsConfig = new WSConfig(env)
const server = new Ws(wsConfig, logger)

// Initialize config values for Twitch.
const apiConfig = new TwitchConfig(env)
const api = new TwitchAuth(apiConfig, logger.level)

// Start Twitch client.
const twitch = new Twitch(apiConfig, server, api, packageJSON, logger)

// Add command handlers
const commandDirectory = path.join(__dirname, 'src', 'Handlers')
const commandFiles = readdirSync(commandDirectory, 'utf-8')

// eslint-disable-next-line no-void
void (async function loadHandlers (): Promise<void> {
let currentFileDir: string = ''

try {
for (let index = 0; index < commandFiles.length; index++) {
currentFileDir = commandFiles[index]
const fullFileName = path.join(commandDirectory.toString(), commandFiles[index])

// Import
const Command = (await import(fullFileName)).default
twitch.handlers.push(new Command(twitch, server, logger))
}

server.connect()
} catch (error) {
logger.error({ err: error }, `Index.ts: Something went wrong while importing ${String(currentFileDir)}.`)
setTimeout(() => {
process.exit(0)
}, 10000)
}
})()
55 changes: 55 additions & 0 deletions config/Twitch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Env } from '@adonisjs/env/build/src/Env'

export default class TwitchConfig {
public clientToken: string
public clientSecret: string
public superSecret: string
public refreshToken: string
public redirectURI: string
public user: { name: string, id: string }
public scope: string[]
public headers: { 'user-agent': string }
public commandPrefix: string

constructor (env: Env) {
/**
* Twitch client ID token.
*/
this.clientToken = env.getOrFail('TWITCH_CLIENT_TOKEN') as string

/**
* Twitch client secret token.
*/
this.clientSecret = env.getOrFail('TWITCH_CLIENT_SECRET') as string

/**
* Redirect URI.
*/
this.redirectURI = ''

/**
* Twitch username.
*/
this.user = {
name: env.getOrFail('TWITCH_BOT_NAME') as string,
id: env.getOrFail('TWITCH_BOT_ID') as string,
}

/**
* Scopes to ask for.
*/
this.scope = ['chat:read', 'chat:edit', 'whispers:read', 'whispers:edit']

/**
* HTTP request headers.
*/
this.headers = {
'user-agent': 'befriendlierapp (https://github.com/kararty/befriendlier-web)',
}

/**
* Command prefix.
*/
this.commandPrefix = env.getOrFail('COMMAND_PREFIX') as string
}
}
20 changes: 20 additions & 0 deletions config/Ws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Env } from '@adonisjs/env/build/src/Env'

export default class WSConfig {
public url: string
public headers: { 'user-agent': string }

constructor (env: Env) {
/**
* Server url to connect to.
*/
this.url = `ws://${env.getOrFail('HOST') as string}:${env.getOrFail('PORT') as string}`

/**
* HTTP request headers.
*/
this.headers = {
'user-agent': `BEFRIENDLIER-BOT-${process.platform}-${process.pid}`,
}
}
}
17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "befriendlier-bot",
"version": "0.0.0",
"description": "",
"description": "Twitch bot serving commands to the befriendlier.app service.",
"private": true,
"scripts": {
"build-bot": "tsc && copyfiles .env build/",
"start": "node build/Index.js --inspect",
"start-test": "ts-node-dev --respawn --clear --log-error --inspect=\"9230\" -- Index.ts",
"start": "node build/Index.js",
"start-test": "ts-node-dev --respawn --clear --log-error --inspect=\"0\" -- Index.ts",
"release": "standard-version && git push --follow-tags origin master"
},
"repository": {
Expand All @@ -32,13 +32,22 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"pino-pretty": "^4.1.0",
"standard-version": "^8.0.2",
"ts-node-dev": "^1.0.0-pre.56",
"typescript": "^3.9.7"
},
"dependencies": {
"@adonisjs/env": "^1.0.18",
"befriendlier-shared": "github:kararty/BeFriendlier-Shared#semver:^4.1.0",
"@adonisjs/logger": "^2.1.0",
"@adonisjs/validator": "^7.4.0",
"befriendlier-shared": "github:kararty/BeFriendlier-Shared#semver:5.4.1",
"dank-twitch-irc": "github:kararty/dank-twitch-irc",
"p-queue": "^6.6.0",
"ws": "^7.3.1"
},
"optionalDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": "^5.0.2"
}
}
Loading

0 comments on commit 41d620c

Please sign in to comment.