-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYourMom.scala
110 lines (96 loc) · 3.89 KB
/
YourMom.scala
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package blazeit.yourmom
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.Future
import scala.io.Source
import scala.util.{Success, Failure}
import akka.actor.{ActorSystem, Cancellable, ActorRef}
import slack.api.SlackApiClient
import slack.models.{User, Im, Message}
import slack.rtm.SlackRtmClient
object YourMom {
implicit val system = ActorSystem("slack")
val challengeTimeout = 2 minutes
var nextReset: Option[Cancellable] = None
def main(args: Array[String]) = {
refreshAndRun()
}
def refreshAndRun(): Unit = {
val botToken = Source.fromFile("bottoken.txt").mkString.replace("\n", "")
val userToken = Source.fromFile("usertoken.txt").mkString.replace("\n", "")
val apiClient = SlackApiClient(botToken)
val userApiClient = SlackApiClient(userToken)
val resolvedSlackInfo: Future[(Seq[User], Seq[Im])] = for {
users <- apiClient.listUsers()
ims <- apiClient.listIms()
} yield (users, ims)
resolvedSlackInfo.onComplete {
case Success((users, ims)) => {
val usersMap = users.map { user =>
(user.id, user)
}.toMap
val channelIdsMap = (for {
imChannel <- ims
user <- usersMap.get(imChannel.user)
} yield (imChannel.id, user)).toMap
listenForYourMom(userApiClient, SlackRtmClient(botToken), usersMap, channelIdsMap)
}
case Failure(err) => {
println("[LOG] startup failed")
println(err)
}
}
}
/**
* We want the bot to time out if expected input is not received after a period of time. This method will clear any existing timeout (if exists),
* and schedule another timeout with the anonymous function passed to it. This function will be executed after a period of time, if updateReset is
* not called again.
*/
def updateReset(onUpdate: () => Unit) = {
nextReset.map(_.cancel())
nextReset = Some(system.scheduler.scheduleOnce(challengeTimeout) {
onUpdate()
})
}
/**
* Given an rtmClient and a message received with this client, check for command strings, and respond as necessary.
* Options:
* - restart: close the rtmClient and start the bot anew
* - status: print the current status of the bot, as passed to this method
* - help: print out info about the bot
*/
def checkForCommands(rtmClient: SlackRtmClient, message: Message, status: String, challengingUserOpt: Option[User], challengedUserOpt: Option[User]): Boolean = {
val sendMessage = (msg: String) => rtmClient.sendMessage(message.channel, msg)
val messageContains = (substring: String) => message.text.toLowerCase().contains(substring): Boolean
if (!message.text.contains(s"<@${rtmClient.state.self.id}>")) {
return false
}
// Restart bot
if (messageContains("restart")) {
sendMessage("Restarting...")
rtmClient.close()
refreshAndRun()
return true
}
return false
}
/**
* Listen for your mom and kick the offender
*/
def listenForYourMom(apiClient: SlackApiClient, rtmClient: SlackRtmClient, uidsToUsers: Map[String, User], channelIdToUsers: Map[String, User]): ActorRef = {
val usernamesToUsers = uidsToUsers.map { case (id, user) =>
(user.name, user)
}.toMap
val usernameRegex = uidsToUsers.values.map(_.name).mkString("|").r
val userIdRegex = uidsToUsers.keys.mkString("|").r
lazy val listener: ActorRef = rtmClient.onMessage { message =>
val sendMessage = (msg: String) => rtmClient.sendMessage(message.channel, msg)
val messageContains = (substring: String) => message.text.toLowerCase().contains(substring): Boolean
if (message.user != rtmClient.state.self.id && !checkForCommands(rtmClient, message, "", None, None) && messageContains("ur mom")) {
apiClient.kickFromGroup(message.channel, "UALEH6UMQ")
println(message.user)
}
}
listener
}
}