update botClient

This commit is contained in:
Jay 2018-02-13 13:16:30 +08:00
parent c4f4b861c3
commit 725791dd4e
4 changed files with 57 additions and 24 deletions

25
README.md Normal file
View File

@ -0,0 +1,25 @@
# MTFoS Bot
## setup
安裝依賴套件
```
$ npm install
```
啟動伺服器
```
$ DEBUG=* node index.js
```
## usage
到 https://dev.twitch.tv/ 申請 Twitch APP
在專案根目錄建立 .env 設定檔案
```
CLIENT_ID=
SECRET=
REDIRECT_URI=
REFRESH_TOKEN=
```
依序填入設定內容後啟動伺服器機器人就可以運作
取得refresh_token
啟動伺服器後進入 http://localhost:10230/chat_login 手動授權後即可取得

View File

@ -2,3 +2,5 @@
require('dotenv').config() require('dotenv').config()
// load server // load server
require('./app') require('./app')
// load chatbot
require('./libs/botClient')

View File

@ -7,7 +7,29 @@ const { msgSplit } = require('./twitchParser')
const ws = new WebSocket(`wss://${config.twitch.chat_host}:443`, 'irc') const ws = new WebSocket(`wss://${config.twitch.chat_host}:443`, 'irc')
ws.on('open', async () => { ws.on('open', handleOpen)
ws.on('message', handleMessage)
ws.on('error', (err) => {
console.error(err)
})
ws.on('close', (code, reason) => {
console.log('exit', code, reason)
})
function handleMessage (data) {
let d = data.toString()
let darr = d.split(/\n/).filter(t => t).map(t => t.replace(/\r$/, ''))
for (let i in darr) {
log(darr[i])
msgSplit(ws, darr[i]).then(() => { })
}
}
async function handleOpen () {
let db = await dbPool.connect() let db = await dbPool.connect()
let body = null let body = null
@ -32,14 +54,14 @@ ws.on('open', async () => {
reject(error) reject(error)
return return
} }
console.log(response.statusCode) if (typeof body === 'string') body = JSON.parse(body)
resolve(body) resolve(body)
}) })
}) })
} catch (err) { } catch (err) {
console.log(err) console.log(err)
} }
console.log(typeof body)
if (body === null || !('access_token' in body)) throw new Error('access token not found') if (body === null || !('access_token' in body)) throw new Error('access token not found')
ws.send('PASS oauth:' + body.access_token) ws.send('PASS oauth:' + body.access_token)
@ -49,6 +71,7 @@ ws.on('open', async () => {
let query = `select "name", "join" from "public"."channels" where "join" = $1` let query = `select "name", "join" from "public"."channels" where "join" = $1`
let param = [true] let param = [true]
let result = await db.query(query, param) let result = await db.query(query, param)
await db.end()
if (result !== null && result.rows.length > 0) { if (result !== null && result.rows.length > 0) {
for (let row of result.rows) { for (let row of result.rows) {
if ('name' in row) { if ('name' in row) {
@ -56,24 +79,6 @@ ws.on('open', async () => {
} }
} }
} }
})
ws.on('message', (data) => {
let d = data.toString()
let darr = d.split(/\n/).filter(t => t).map(t => t.replace(/\r$/, ''))
for (let i in darr) {
log(darr[i])
msgSplit(ws, darr[i]).then(() => { })
} }
})
ws.on('error', (err) => {
console.error(err)
})
ws.on('close', (code, reason) => {
console.log('exit', code, reason)
})
module.exports = ws module.exports = ws

View File

@ -52,12 +52,13 @@ const parseCMD = async function (user, channel, msg) {
($1, $2, $3) ($1, $2, $3)
on conflict ("command", "channel") do update set on conflict ("command", "channel") do update set
"message" = $3, "message" = $3,
"active" = true` "active" = true,
"mtime" = now()`
let param = [cmd, ch, tmpmsg] let param = [cmd, ch, tmpmsg]
await db.query(query, param) await db.query(query, param)
} else if (/^!-/.test(txt[0])) { } else if (/^!-/.test(txt[0])) {
let cmd = txt[0].substr(2) let cmd = txt[0].substr(2)
let query = `update "public"."commands" set "active" = $1 where "command" = $2 and "channel" = $3` let query = `update "public"."commands" set "active" = $1, "mtime" = now() where "command" = $2 and "channel" = $3`
let param = [false, cmd, ch] let param = [false, cmd, ch]
await db.query(query, param) await db.query(query, param)
m = `command "${cmd}" has removed` m = `command "${cmd}" has removed`