From: Jordan Duabe <1442368+j4ckofalltrades@users.noreply.github.com> Date: Sat, 13 Mar 2021 12:52:57 +0530 Subject: Implement Twitch whisper action --- Implement Twitch whisper action --- --- 'a/README.md' +++ b/README.md @@ -34,7 +34,7 @@ In the Stream-Pi Server's Plugin page en - Set username color - Normal users can choose between Blue, Coral, DodgerBlue, SpringGreen, YellowGreen, Green, OrangeRed, Red, GoldenRod, HotPink, CadetBlue, SeaGreen, Chocolate, BlueViolet, and Firebrick. Twitch Turbo users can use any Hex value (i.e: #000000). - Send channel message -- Whisper (send user message, TBA) +- Whisper (send user message) #### Broadcaster and Mods --- 'a/build.sh' +++ b/build.sh @@ -99,6 +99,9 @@ twitchchat() { cd ../set-color && mvn clean install package mv target/twitch-set-color-1.0.0.jar ../$FOLD/twitch-set-color.jar + + cd ../whisper && mvn clean install package + mv target/twitch-whisper-1.0.0.jar ../$FOLD/twitch-whisper.jar popd || exit } --- /dev/null +++ b/twitch/whisper/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + com.stream-pi + twitch-whisper + 1.0.0 + + + 11 + 11 + 1.0.0 + 1.0.0 + 1.0.0 + 0.6.3 + + + + + jitpack.io + https://jitpack.io + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.0 + + + test-jar + package + + test-jar + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 11 + + + + + + + + com.stream-pi + util + ${streamPiUtilVersion} + + + + com.stream-pi + action-api + ${streamPiActionApiVersion} + + + + com.github.gikkman + Java-Twirk + ${javaTwirkVersion} + + + + com.stream-pi + twitch-chat-connect + ${streamPiTwitchChatConnectVersion} + + + + --- /dev/null +++ b/twitch/whisper/src/main/java/module-info.java @@ -0,0 +1,9 @@ +module com.stream_pi.twitch.whisperaction { + requires com.stream_pi.util; + requires com.stream_pi.action_api; + + requires com.stream_pi.twitchchatconnectaction; + requires Java.Twirk; + + provides com.stream_pi.action_api.normalaction.NormalAction with whisper.WhisperAction; +} --- /dev/null +++ b/twitch/whisper/src/main/java/whisper/WhisperAction.java @@ -0,0 +1,88 @@ +package whisper; + +import com.gikk.twirk.Twirk; +import com.gikk.twirk.TwirkBuilder; +import com.stream_pi.action_api.actionproperty.property.Property; +import com.stream_pi.action_api.actionproperty.property.Type; +import com.stream_pi.action_api.normalaction.NormalAction; +import com.stream_pi.util.exception.StreamPiException; +import com.stream_pi.util.version.Version; +import connect.chat.TwitchChatCredentials; + +public class WhisperAction extends NormalAction +{ + + private final String usernameKey = "username_wa"; + private final String messageKey = "message_wa"; + + private Twirk twirk; + + public WhisperAction() + { + setName("Whisper"); + setCategory("Twitch Chat"); + setVisibilityInServerSettingsPane(false); + setAuthor("j4ckofalltrades"); + setVersion(new Version(1, 0, 0)); + setHelpLink("https://github.com/stream-pi/essentialactions#twitch-chat-integration"); + } + + @Override + public void initProperties() throws Exception + { + Property usernameProp = new Property(usernameKey, Type.STRING); + usernameProp.setDisplayName("Twitch Username"); + usernameProp.setDefaultValueStr("username"); + usernameProp.setCanBeBlank(false); + + Property messageProp = new Property(messageKey, Type.STRING); + messageProp.setDisplayName("Message"); + messageProp.setDefaultValueStr("message"); + messageProp.setCanBeBlank(false); + + addClientProperties(usernameProp, messageProp); + } + + @Override + public void initAction() throws Exception + { + + } + + @Override + public void onActionClicked() throws Exception + { + final TwitchChatCredentials.ChatCredentials credentials = TwitchChatCredentials.getCredentials(); + credentials.ensureCredentialsInitialized(); + + final String username = getClientProperties().getSingleProperty(usernameKey).getStringValue(); + final String message = getClientProperties().getSingleProperty(messageKey).getStringValue(); + + try + { + twirk = new TwirkBuilder(username, credentials.getNickname(), credentials.getOauthToken()).build(); + twirk.connect(); + twirk.whisper(username, message); + } catch (Exception ex) + { + throw new StreamPiException( + "Failed to send message to user", + String.format("Could not send message '%s' to user '%s', please try again.", + username, message) + ); + } + } + + @Override + public void onShutDown() throws Exception + { + if (twirk != null) { + try + { + twirk.disconnect(); + } catch (Exception ex) { + throw new StreamPiException("Twitch connection error", "Please try again."); + } + } + } +}