diff --git a/README.md b/README.md new file mode 100644 index 0000000..cbc5b26 --- /dev/null +++ b/README.md @@ -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 手動授權後即可取得 \ No newline at end of file diff --git a/index.js b/index.js index a373acf..ce76e98 100644 --- a/index.js +++ b/index.js @@ -2,3 +2,5 @@ require('dotenv').config() // load server require('./app') +// load chatbot +require('./libs/botClient') \ No newline at end of file diff --git a/libs/botClient.js b/libs/botClient.js index 9fc2478..6bca69c 100644 --- a/libs/botClient.js +++ b/libs/botClient.js @@ -7,7 +7,29 @@ const { msgSplit } = require('./twitchParser') 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 body = null @@ -32,14 +54,14 @@ ws.on('open', async () => { reject(error) return } - console.log(response.statusCode) + if (typeof body === 'string') body = JSON.parse(body) resolve(body) }) }) } catch (err) { console.log(err) } - console.log(typeof body) + if (body === null || !('access_token' in body)) throw new Error('access token not found') 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 param = [true] let result = await db.query(query, param) + await db.end() if (result !== null && result.rows.length > 0) { for (let row of result.rows) { 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 diff --git a/libs/twitchParser.js b/libs/twitchParser.js index ffc6115..1637808 100644 --- a/libs/twitchParser.js +++ b/libs/twitchParser.js @@ -52,12 +52,13 @@ const parseCMD = async function (user, channel, msg) { ($1, $2, $3) on conflict ("command", "channel") do update set "message" = $3, - "active" = true` + "active" = true, + "mtime" = now()` let param = [cmd, ch, tmpmsg] await db.query(query, param) } else if (/^!-/.test(txt[0])) { 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] await db.query(query, param) m = `command "${cmd}" has removed`