add comment
This commit is contained in:
parent
79eed290d4
commit
efbbed07ed
@ -8,7 +8,8 @@ module.exports = {
|
|||||||
redirect_uri: process.env.REDIRECT_URI || '',
|
redirect_uri: process.env.REDIRECT_URI || '',
|
||||||
client_id: process.env.CLIENT_ID || '',
|
client_id: process.env.CLIENT_ID || '',
|
||||||
secret: process.env.SECRET || '',
|
secret: process.env.SECRET || '',
|
||||||
refresh_token: process.env.REFRESH_TOKEN || ''
|
refresh_token: process.env.REFRESH_TOKEN || '',
|
||||||
|
bot_account: process.env.BOT_ACCOUNT || ''
|
||||||
},
|
},
|
||||||
database: {
|
database: {
|
||||||
host: process.env.DB_HOST || 'localhost',
|
host: process.env.DB_HOST || 'localhost',
|
||||||
|
@ -10,7 +10,9 @@ const {
|
|||||||
/** @type {WebSocket} */
|
/** @type {WebSocket} */
|
||||||
var ws = null
|
var ws = null
|
||||||
|
|
||||||
|
if (config.twitch.refresh_token && typeof config.twitch.refresh_token === 'string' && config.twitch.refresh_token.length > 0) {
|
||||||
runBot()
|
runBot()
|
||||||
|
}
|
||||||
|
|
||||||
function runBot() {
|
function runBot() {
|
||||||
ws = new WebSocket(`wss://${config.twitch.chat_host}:443`, 'irc')
|
ws = new WebSocket(`wss://${config.twitch.chat_host}:443`, 'irc')
|
||||||
@ -38,16 +40,25 @@ function handleExit(code) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleMessage(data) {
|
function handleMessage(data) {
|
||||||
|
// socket message convert to string
|
||||||
let d = data.toString()
|
let d = data.toString()
|
||||||
|
// split message with \n
|
||||||
|
// filter array remove empty element
|
||||||
|
// replace \r to empty
|
||||||
let darr = d.split(/\n/).filter(t => t).map(t => t.replace(/\r$/, ''))
|
let darr = d.split(/\n/).filter(t => t).map(t => t.replace(/\r$/, ''))
|
||||||
|
|
||||||
for (let i in darr) {
|
for (let i in darr) {
|
||||||
log(darr[i])
|
log(darr[i])
|
||||||
msgSplit(ws, darr[i]).then(() => {})
|
// parse message
|
||||||
|
msgSplit(ws, darr[i]).then(() => { /* pass */ })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WebSocket Open Action
|
||||||
|
*/
|
||||||
async function handleOpen() {
|
async function handleOpen() {
|
||||||
|
// from pool get db connection
|
||||||
let db = await dbPool.connect()
|
let db = await dbPool.connect()
|
||||||
|
|
||||||
let body = null
|
let body = null
|
||||||
@ -67,6 +78,7 @@ async function handleOpen() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
body = await new Promise((resolve, reject) => {
|
body = await new Promise((resolve, reject) => {
|
||||||
|
// get new token with refresh_token
|
||||||
request(options, function (error, response, body) {
|
request(options, function (error, response, body) {
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error)
|
reject(error)
|
||||||
@ -82,14 +94,19 @@ async function handleOpen() {
|
|||||||
|
|
||||||
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')
|
||||||
|
|
||||||
|
// login to twitch irc
|
||||||
ws.send('PASS oauth:' + body.access_token)
|
ws.send('PASS oauth:' + body.access_token)
|
||||||
ws.send('NICK mtfosbot')
|
ws.send('NICK mtfosbot')
|
||||||
ws.send('CAP REQ :twitch.tv/membership :twitch.tv/commands')
|
ws.send('CAP REQ :twitch.tv/membership :twitch.tv/commands')
|
||||||
|
|
||||||
|
// 取得要加入的頻道列表
|
||||||
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)
|
||||||
|
|
||||||
|
// release pool connection
|
||||||
await db.end()
|
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) {
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
|
|
||||||
class MessageQueue {
|
|
||||||
constructor () {
|
|
||||||
this.queue = {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = new MessageQueue()
|
|
@ -1,4 +1,5 @@
|
|||||||
const dbPool = require('./database')
|
const dbPool = require('./database')
|
||||||
|
const config = require('../config')
|
||||||
/**
|
/**
|
||||||
* @param {WebSocket} ws
|
* @param {WebSocket} ws
|
||||||
* @param {string} msg
|
* @param {string} msg
|
||||||
@ -8,16 +9,23 @@ const msgSplit = async function (ws, msg) {
|
|||||||
let txtarr = msg.split(' ')
|
let txtarr = msg.split(' ')
|
||||||
|
|
||||||
if (txtarr.length > 2) {
|
if (txtarr.length > 2) {
|
||||||
|
// server heartbeat message
|
||||||
if (/^ping$/i.test(txtarr[0])) {
|
if (/^ping$/i.test(txtarr[0])) {
|
||||||
|
// reply server heartbeat
|
||||||
ws.send(`PONG ${txtarr[1]}`)
|
ws.send(`PONG ${txtarr[1]}`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// chat message
|
// chat message
|
||||||
if (/^privmsg$/i.test(txtarr[1])) {
|
if (/^privmsg$/i.test(txtarr[1])) {
|
||||||
|
// message format
|
||||||
|
// :user!user@user.tmi.twitch.tv PRIVMSG #channel :message
|
||||||
let user = txtarr[0].split('!')[0].substr(1)
|
let user = txtarr[0].split('!')[0].substr(1)
|
||||||
if (user === 'mtfosbot') return
|
if (user === config.twitch.bot_account) return
|
||||||
let channel = txtarr[2]
|
let channel = txtarr[2]
|
||||||
|
|
||||||
|
// get message
|
||||||
txtarr = txtarr.slice(3, txtarr.length)
|
txtarr = txtarr.slice(3, txtarr.length)
|
||||||
|
// remove message first :
|
||||||
let m = txtarr.join(' ').substr(1)
|
let m = txtarr.join(' ').substr(1)
|
||||||
|
|
||||||
let result = await parseCMD(user, channel, m)
|
let result = await parseCMD(user, channel, m)
|
||||||
@ -39,6 +47,7 @@ const parseCMD = async function (user, channel, msg) {
|
|||||||
let m = null
|
let m = null
|
||||||
try {
|
try {
|
||||||
let txt = msg.trim().split(' ')
|
let txt = msg.trim().split(' ')
|
||||||
|
// get channel name without hashtag
|
||||||
let ch = channel[0] === '#' ? channel.substr(1) : channel
|
let ch = channel[0] === '#' ? channel.substr(1) : channel
|
||||||
if (txt.length < 1) {
|
if (txt.length < 1) {
|
||||||
await db.end()
|
await db.end()
|
||||||
@ -56,12 +65,15 @@ const parseCMD = async function (user, channel, msg) {
|
|||||||
"mtime" = now()`
|
"mtime" = now()`
|
||||||
let param = [cmd, ch, tmpmsg]
|
let param = [cmd, ch, tmpmsg]
|
||||||
await db.query(query, param)
|
await db.query(query, param)
|
||||||
|
m = `command "${cmd}" has added`
|
||||||
|
// remove cmd
|
||||||
} 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, "mtime" = now() 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`
|
||||||
|
// run cmd
|
||||||
} else if (/^!/.test(txt[0])) {
|
} else if (/^!/.test(txt[0])) {
|
||||||
let cmd = txt[0].substr(1)
|
let cmd = txt[0].substr(1)
|
||||||
let query = `select "message", "command" from "public"."commands" where "command" = $1 and "channel" = $2 and "active" = $3 limit 1`
|
let query = `select "message", "command" from "public"."commands" where "command" = $1 and "channel" = $2 and "active" = $3 limit 1`
|
||||||
|
Loading…
Reference in New Issue
Block a user