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

Commit 64eb789

Browse files
authored
Merge pull request #57 from daniel0611/fix-twitch
Fix channel auto-formatting in TwitchChat
2 parents d5914ed + dae9757 commit 64eb789

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,18 @@ class TwitchChatConnector(override val sourceIdentifier: String) extends Connect
3030
}
3131

3232
def joinChannel(channel: String): Unit = {
33-
val formattedChan = formatChannel(channel)
34-
bot.send().joinChannel(formattedChan)
35-
channels += formattedChan
33+
bot.send().joinChannel(channel)
34+
channels += channel
3635
}
3736

3837
def sendChatMessage(channel: String, chatMessage: String): Unit = {
39-
val formattedChan = formatChannel(channel)
40-
if (!isJoined(formattedChan)) throw new IllegalArgumentException(s"you must join the '$channel' channel, before you can send messages to it")
41-
bot.send().message(formattedChan, chatMessage)
38+
if (!isJoined(channel)) throw new IllegalArgumentException(s"you must join the '$channel' channel, before you can send messages to it")
39+
bot.send().message(channel, chatMessage)
4240
}
4341

4442
override def getUniqueTypeString: String = this.getClass.getName
4543

46-
def isJoined(channel: String): Boolean = channels.contains(formatChannel(channel))
47-
48-
/**
49-
* Ensures that the channel is in following format: "#lowercasename"
50-
* @param chan the unmodified channel
51-
* @return the channel in the correct format, changes nothing if already correct
52-
*/
53-
private def formatChannel(chan: String): String = s"#${chan.stripPrefix("#").toLowerCase}"
44+
def isJoined(channel: String): Boolean = channels.contains(channel)
5445

5546
private def getConfig: Configuration = {
5647

@@ -117,4 +108,15 @@ class TwitchChatConnector(override val sourceIdentifier: String) extends Connect
117108
bot.close()
118109
true
119110
}
111+
}
112+
113+
object TwitchChatConnector {
114+
115+
/**
116+
* Ensures that the channel is in following format: "#lowercasename"
117+
*
118+
* @param chan the unmodified channel
119+
* @return the channel in the correct format, changes nothing if already correct
120+
*/
121+
private[chat] def formatChannel(chan: String): String = s"#${chan.stripPrefix("#").toLowerCase}"
120122
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.codeoverflow.chatoverflow.api.io.input.chat._
1010
import org.codeoverflow.chatoverflow.registry.Impl
1111
import org.codeoverflow.chatoverflow.requirement.InputImpl
1212
import org.codeoverflow.chatoverflow.requirement.service.twitch.chat
13+
import org.codeoverflow.chatoverflow.requirement.service.twitch.chat.TwitchChatConnector
1314
import org.pircbotx.hooks.events.{MessageEvent, UnknownEvent}
1415

1516
import scala.collection.JavaConverters._
@@ -101,7 +102,7 @@ class TwitchChatInputImpl extends InputImpl[chat.TwitchChatConnector] with Twitc
101102
override def registerPrivateMessageHandler(handler: Consumer[TwitchChatMessage]): Unit = privateMessageHandler += handler
102103

103104
override def setChannel(channel: String): Unit = {
104-
currentChannel = Some(channel.trim)
105-
if (!sourceConnector.get.isJoined(channel.trim)) sourceConnector.get.joinChannel(channel.trim)
105+
currentChannel = Some(TwitchChatConnector.formatChannel(channel.trim))
106+
if (!sourceConnector.get.isJoined(currentChannel.get)) sourceConnector.get.joinChannel(currentChannel.get)
106107
}
107108
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.codeoverflow.chatoverflow.api.io.output.chat.TwitchChatOutput
55
import org.codeoverflow.chatoverflow.registry.Impl
66
import org.codeoverflow.chatoverflow.requirement.OutputImpl
77
import org.codeoverflow.chatoverflow.requirement.service.twitch.chat
8+
import org.codeoverflow.chatoverflow.requirement.service.twitch.chat.TwitchChatConnector
89

910
/**
1011
* This is the implementation of the twitch chat output, using the twitch connector.
@@ -24,7 +25,7 @@ class TwitchChatOutputImpl extends OutputImpl[chat.TwitchChatConnector] with Twi
2425
override def start(): Boolean = true
2526

2627
override def setChannel(channel: String): Unit = {
27-
currentChannel = Some(channel.trim)
28-
if (!sourceConnector.get.isJoined(channel.trim)) sourceConnector.get.joinChannel(channel.trim)
28+
currentChannel = Some(TwitchChatConnector.formatChannel(channel.trim))
29+
if (!sourceConnector.get.isJoined(currentChannel.get)) sourceConnector.get.joinChannel(currentChannel.get)
2930
}
3031
}

0 commit comments

Comments
 (0)