Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F improve twitch #3

Merged
merged 4 commits into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"module": true,
"process": true,
"Exception": true,
"console": true,
"setInterval": true
},
"extends": "eslint:recommended",
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
auth.json
.idea/
.idea/
*.log
78 changes: 49 additions & 29 deletions bot.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,55 @@
const Discord = require('discord.js')
let twitch = require('./twitch.js')
let fs = require('fs')
const auth = require('./auth.js')()
let config = require('./config.json')
const Discord = require('discord.js'),
twitch2 = require('./twitch.js'),
fs = require('fs'),
auth = require('./auth.js')(),
config = require('./config.json'),
logger = require('./logger.js')(),
cache = require('./cache.js')(100, 10)

const SUPPORTED_STREAMERS = config.streamers
const {STREAMER_NOT_LIVE} = require('./constants')

let live = {
pedropcruz: 0,
drakzOfficial: 0,
ExilePT: 0
}
// once key is expired set it to be 0 (not live)
cache.on('expired', function(key, value) {
if (value !== STREAMER_NOT_LIVE) {
cache.set(key, STREAMER_NOT_LIVE)
}
})

config.streamers.forEach(streamer => {
cache.set(streamer, 0)
})

let bot = new Discord.Client()
bot.commands = new Discord.Collection()
bot.aliases = new Discord.Collection()

fs.readdir('./commands/', (err, files) => {
if (err) console.error(err)
if (err) {
logger.error(err)
return
}

let jsfiles = files.filter(
f => f.split('.').pop() === 'js'
)

logger.debug(`${jsfiles.length} commands loaded`)
if (jsfiles.length <= 0) {
return console.log('0 commands loaded.')
} else {
console.log(jsfiles.length + ' commands loaded.')
return
}

jsfiles.forEach(f => {
let cmds = require(`./commands/${f}`)
console.log(`Command ${f} loaded.`)
logger.debug(`Command ${f} loaded.`)
bot.commands.set(cmds.config.command, cmds)
bot.aliases.set(cmds.config.alias, cmds)
})
})

bot.login(auth.token)

bot.on('ready', () => {
console.log(`Connected!\nLogged in as ${bot.user.tag}!`)
logger.debug(`Logged in as ${bot.user.tag}!`)
bot.user.setGame('www.drakz.pt')

const twitchOnline =
Expand All @@ -48,36 +59,45 @@ bot.on('ready', () => {
'id',
config.channel_announces
)
setInterval(() => {
live = twitch.checkTwitchStreams(
SUPPORTED_STREAMERS,
announce_channel,
live,
auth.twitch_clientId
)
}, config.twitch_checktime * 1000)

// assign new function with already bound parameters
let renderLiveStreams = twitch2.renderLiveStreams.bind(
null,
auth.twitch_clientId,
announce_channel,
cache,
config.streamers
)

setInterval(
renderLiveStreams,
config.twitch_checktime * 1000
)
}
})

const prefix = config.command_prefix

bot.on('message', message => {
if (message.author.bot) return

let prefix = config.command_prefix
// let sender = message.author
let msg = message.content.toLowerCase()
let cont = message.content.slice(prefix.length).split(' ')
let args = cont.slice(1)

if (msg === 'poop') {
message.channel.send(':poop:')
return
}

if (msg.startsWith('hey drakzbot')) {
message.reply('hey!')
return
}

if (!msg.startsWith(prefix)) return

let cont = message.content.slice(prefix.length).split(' ')
let args = cont.slice(1)

let cmd
if (bot.commands.has(cont[0]))
cmd = bot.commands.get(cont[0])
Expand Down
8 changes: 8 additions & 0 deletions cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const NodeCache = require('node-cache')

module.exports = function(ttl = 100, checkPeriod = 120) {
return new NodeCache({
stdTTL: ttl,
checkperiod: checkPeriod
})
}
4 changes: 4 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable */

const STREAMER_NOT_LIVE = 0
const STREAMER_LIVE = 1
39 changes: 39 additions & 0 deletions logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const {
createLogger,
format,
transports
} = require('winston')
const {combine, timestamp, printf} = format
const customFormat = printf(info => {
return `${info.timestamp} ${info.level}: ${info.message}`
})

module.exports = () => {
const logger = createLogger({
level: 'error',
format: combine(
format.splat(),
timestamp(),
customFormat
),
transports: [
new transports.File({
filename: 'error.log',
level: 'error'
})
],
exceptionHandlers: [
new transports.File({filename: 'exceptions.log'})
]
})

if (process.env.NODE_ENV !== 'production') {
logger.add(
new transports.Console({
level: 'debug'
})
)
}

return logger
}
Loading