-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
1,639 additions
and
39 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
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) | ||
} | ||
})() |
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 |
---|---|---|
@@ -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 | ||
} | ||
} |
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 |
---|---|---|
@@ -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}`, | ||
} | ||
} | ||
} |
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
Oops, something went wrong.