Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit d9b137b

Browse files
authored
Merge pull request #86 from daniel0611/fix/80-twitch-wait-for-login
Wait for authentication in twitch bot and report invalid credentials
2 parents b92e2c8 + 1223ff9 commit d9b137b

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.codeoverflow.chatoverflow.requirement.service.twitch.chat
2+
3+
import org.pircbotx.hooks.events.{ConnectAttemptFailedEvent, ConnectEvent, NoticeEvent}
4+
import org.pircbotx.hooks.{Event, ListenerAdapter}
5+
6+
/**
7+
* Handles connection events for the TwitchChatConnector.
8+
* Calls the callback function once the bot connected and reports connection errors.
9+
* @param fn the callback which will be called once suitable event has been received.
10+
* The first param informs whether the connection could be established successfully
11+
* and the second param includes a error description if something has gone wrong.
12+
*/
13+
class TwitchChatConnectListener(fn: (Boolean, String) => Unit) extends ListenerAdapter {
14+
override def onEvent(event: Event): Unit = {
15+
event match {
16+
case _: ConnectEvent => fn(true, "")
17+
case e: ConnectAttemptFailedEvent => fn(false, "couldn't connect to irc chat server")
18+
case e: NoticeEvent =>
19+
if (e.getNotice.contains("authentication failed")) {
20+
fn(false, "authentication failed")
21+
}
22+
case _ =>
23+
}
24+
}
25+
}

src/main/scala/org/codeoverflow/chatoverflow/requirement/service/twitch/chat/TwitchChatConnector.scala

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import scala.collection.mutable.ListBuffer
1515
*/
1616
class TwitchChatConnector(override val sourceIdentifier: String) extends Connector(sourceIdentifier) with WithLogger {
1717
private val twitchChatListener = new TwitchChatListener
18+
private val connectionListener = new TwitchChatConnectListener(onConnect)
1819
private val oauthKey = "oauth"
1920
override protected var requiredCredentialKeys: List[String] = List(oauthKey)
2021
override protected var optionalCredentialKeys: List[String] = List()
2122
private var bot: PircBotX = _
23+
private var status: Option[(Boolean, String)] = None
2224
private val channels = ListBuffer[String]()
2325

2426
def addMessageEventListener(listener: MessageEvent => Unit): Unit = {
@@ -63,6 +65,7 @@ class TwitchChatConnector(override val sourceIdentifier: String) extends Connect
6365
.setName(credentials.get.credentialsIdentifier)
6466
.setServerPassword(password.getOrElse(""))
6567
.addListener(twitchChatListener)
68+
.addListener(connectionListener)
6669
.buildConfiguration()
6770
} else {
6871
logger error "No credentials set!"
@@ -71,33 +74,47 @@ class TwitchChatConnector(override val sourceIdentifier: String) extends Connect
7174

7275
}
7376

77+
/**
78+
* Gets called by the TwitchChatConnectListener when the bot has connected.
79+
* Saves the passed information into the status variable.
80+
*/
81+
private def onConnect(success: Boolean, msg: String): Unit = {
82+
status.synchronized {
83+
// tell the thread which starts the connector that the status has been reported
84+
status.notify()
85+
status = Some((success, msg))
86+
}
87+
}
88+
7489
/**
7590
* Starts the connector, e.g. creates a connection with its platform.
7691
*/
7792
override def start(): Boolean = {
7893
bot = new PircBotX(getConfig)
7994
startBot()
80-
true
8195
}
8296

83-
private def startBot(): Unit = {
84-
85-
var errorCount = 0
86-
97+
private def startBot(): Boolean = {
8798
new Thread(() => {
8899
bot.startBot()
89100
}).start()
90101

91-
while (bot.getState != PircBotX.State.CONNECTED && errorCount < 30) {
92-
logger info "Waiting while the bot is connecting..."
93-
Thread.sleep(100)
94-
errorCount += 1
102+
logger info "Waiting while the bot is connecting and logging in..."
103+
status.synchronized {
104+
status.wait(10000)
105+
}
106+
107+
if (status.isEmpty) {
108+
logger error "Bot couldn't connect within timeout of 10 seconds."
109+
return false
95110
}
96111

97-
if (errorCount >= 30) {
98-
logger error "Fatal. Unable to start bot."
112+
val (success, msg) = status.get
113+
if (!success) {
114+
logger error s"Bot couldn't connect. Reason: $msg."
99115
}
100116

117+
success
101118
}
102119

103120
/**
@@ -106,6 +123,8 @@ class TwitchChatConnector(override val sourceIdentifier: String) extends Connect
106123
override def stop(): Boolean = {
107124
bot.sendIRC().quitServer()
108125
bot.close()
126+
status = None
127+
channels.clear()
109128
true
110129
}
111130
}

0 commit comments

Comments
 (0)