~tomleb/juliendeathbot

2bbb281f364887b454632aee47599bd9603ea680 — Tom Lebreux 9 months ago 77f80ab v0.2.0
Add command !jdb criss
6 files changed, 55 insertions(+), 3 deletions(-)

M index.js
M lib/bot.js
M lib/commands.js
M lib/commands.test.js
M lib/greeter.js
M lib/utils.js
M index.js => index.js +2 -1
@@ 92,7 92,8 @@ const main = async () => {

    const bot = new Bot(logger, twitchClient, deathCounts,
            config.getDeathPhrases(), config.getMotivationPhrases(),
            config.getInsultPhrases(), greeter, delay_seconds)
            config.getInsultPhrases(), greeter, delay_seconds,
            config.getLorembarnak())
    bot.initialize()

    bot.run()

M lib/bot.js => lib/bot.js +6 -1
@@ 1,5 1,6 @@
const {
    commandCount,
    commandCurse,
    commandHelp,
    commandInsult,
    commandMotivate,


@@ 10,7 11,8 @@ const {

class Bot {
    constructor(logger, twitchClient, deathCounts, deathPhrases,
            motivationPhrases, insultPhrases, greeter, delay_seconds) {
            motivationPhrases, insultPhrases, greeter, delay_seconds,
            lorembarnak) {
        this.logger = logger
        this.deathPhrases = deathPhrases
        this.motivationPhrases = motivationPhrases


@@ 20,6 22,7 @@ class Bot {
        this.greeter = greeter
        this.join_delay_done = false
        this.join_delay = delay_seconds * 1000
        this.lorembarnak = lorembarnak
    }

    initialize() {


@@ 140,6 143,8 @@ class Bot {
            await commandMotivate(actionLogger, client, this.motivationPhrases, channel)
        } else if (action === 'insult') {
            await commandInsult(actionLogger, client, this.insultPhrases, channel)
        } else if (action === 'criss' && this.lorembarnak.enabled) {
            await commandCurse(actionLogger, client, channel, this.lorembarnak.length)
        }
    }
}

M lib/commands.js => lib/commands.js +18 -1
@@ 1,9 1,25 @@
const { selectRandom } = require('./utils')
const { selectRandom, randomBetween } = require('./utils')

const lorembarnak = require('lorembarnak')

const parseCommand = (message) => {
    return message.split(/(\s+)/).filter((e) => e.trim().length > 0)
}

const commandCurse = async (logger, client, channel, minMax) => {
    const minimum = minMax.minimum
    const maximum = minMax.maximum
    const length = randomBetween(minimum, maximum)
    const text = lorembarnak.getText(length)
    logger.withLabels({
        minimum: minimum,
        maximum: maximum,
        length: length,
        phrase: text,
    }).info(`sending curse phrase`)
    await client.say(channel, text)
}

// commandCount sends the total death count to the chat
const commandTotal = async (logger, client, deathCounts, channel) => {
    try {


@@ 91,6 107,7 @@ const commandRecord = async (logger, client, deathPhrases, deathCounts, channel)

module.exports = {
    commandCount,
    commandCurse,
    commandHelp,
    commandInsult,
    commandMotivate,

M lib/commands.test.js => lib/commands.test.js +23 -0
@@ 1,6 1,7 @@
const { Logger } = require('./logger')
const {
    commandCount,
    commandCurse,
    commandHelp,
    commandInsult,
    commandMotivate,


@@ 10,8 11,11 @@ const {
} = require('./commands')
const { DeathCounts } = require('./deathcount')

const lorembarnak = require('lorembarnak')
const sqlite3 = require('sqlite3').verbose();

jest.mock('lorembarnak')

const makeDeathCount = () => {
    const db = new sqlite3.Database(':memory:',
        sqlite3.OPEN_READWRITE|sqlite3.OPEN_CREATE, (err) => {


@@ 230,3 234,22 @@ describe('help command', () => {
        expect(client.say).toHaveBeenCalledTimes(8)
    })
})

describe('curse command', () => {
    it('sends curse', async () => {
        const logger = new Logger('error')
        const channel = '#mychannel'
        const minMax = {
            minimum: 4,
            maximum: 6,
        }
        const client = {
            say: jest.fn()
        }

        lorembarnak.getText.mockImplementation(length => 'hi')
        await commandCurse(logger, client, channel, minMax)

        expect(client.say).toHaveBeenCalledWith(channel, 'hi')
    })
})

M lib/greeter.js => lib/greeter.js +1 -0
@@ 1,3 1,4 @@
// TODO: Per channel greets
class Greeter {
    constructor(logger, greeters) {
        this.greeters = greeters

M lib/utils.js => lib/utils.js +5 -0
@@ 7,6 7,10 @@ const selectRandom = (items) => {
    return items[index]
}

const randomBetween = (minimum, maximum) => {
    return (Math.random() * (maximum - minimum)) + minimum
}

// toISOStringWithTimezone comes from
// https://www.30secondsofcode.org/js/s/to-iso-string-with-timezone and is
// licensed CC0-1.0 License as mentioned here:


@@ 27,6 31,7 @@ const toISOStringWithTimezone = (date) => {
};

module.exports = {
    randomBetween,
    selectRandom,
    toISOStringWithTimezone,
}