repl.it
@cmarmar/

GrotesqueNonstopAddress

Nodejs

No description

fork
loading
Files
  • index.js
  • assets
  • originalAssets.js
  • originalIndex.js
  • originalTestRunner.js
  • package-lock.json
  • package.json
  • Prompt.MD
  • TestRunner.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// C: Please check out originalIndex by running originalIndex.js instead of index.js. I made some comments there that explain why I ended up doing what I'm doing now with the code. At the very least if this turns out to be a horrendous mess, originalIndex.js is the cleanest version of the receive function and its tests so it'll be the easiest to read through and understand.

// Below is the more recent version where I decided to have receive work with a channels object that holds all the necessary channels and their respective messages, bots, info together. 

const _ = require('underscore')
const {channels, toggleBot, givePoint, setAway, startHangout, botCheck} = require('./assets')
const TestRunner = require('./TestRunner')

const testInstance = new TestRunner(channels)

const receive = (sender, message, channelId) => {
  const matches = /\/(\w)+/i.exec(message)
  let messages = channels[channelId].messages
  messages.push(sender + ': ' + message)
  if (channels.aways.length){ //if anyone has set themselves away
    channels.aways.forEach((awayObj) => {
      const awayChannel = awayObj.awayFrom
      if (awayChannel == channelId) channels[awayChannel].messages.push(`${awayObj.sender} (Autoreply): ${awayObj.message}`)
    })
  } 
  if (matches) {
    switch (matches[0]) {
      case '/hangout':
        if (botCheck('HangoutBot', channelId, channels)) { // I added this botCheck function for almost each bot in order to use the bot objects in each channel effectively. If there's no bots assigned to the channel, the user is alerted.
          startHangout(channelId, channels)
        }
        break
      case '/givepoint':
        if (botCheck('PointBot', channelId, channels)) {
          givePoint(message, channelId, channels)
        }
        break
      case '/setaway':
        if (botCheck('AwayBot', channelId, channels)) {
          setAway(sender, message, channelId, channels)
        }
        break
      case '/togglebot': //C: Special bot I added that allows admin Jordan to toggle the bots on or off in each channel. Pretty much hardcoded to only work for them, though.
        const botName = message.slice(11)
        if (sender === 'Jordan') {
          toggleBot(botName, channelId, channels)
        } else {
          channels[channelId].messages.push(`-SuperBot: YOU DO NOT HAVE PERMISSION TO TURN ${channels[channelId].bots[botName] ? 'OFF' : 'ON'} ${botName}.`)
        }
    }
  }
}

testInstance.run(receive)
node v10.16.0