From: Debayan Sutradhar Date: Thu, 24 Jun 2021 20:51:56 +0530 Subject: Merge pull request #58 from j4ckofalltrades/issue-57 --- Merge pull request #58 from j4ckofalltrades/issue-57 Followers-only toggle action --- --- 'a/README.md' +++ b/README.md @@ -10,6 +10,8 @@ Set of trusted, essential actions and in ## Actions Help Guide - [Hotkey](hotkeyaction/README.md) +- [Media Keys](mediakeyaction/README.md) +- [OBS](obssuite/README.md) - [Run Command](runcommandaction/README.md) - [Twitch Chat](twitch/README.md) @@ -20,7 +22,7 @@ Plugins directory (`$HOME/Stream-Pi/Serv ### Linux/Mac -Build all actions by executing `make build-all` from the command line or specific actions i.e. `make twitch`. +Build all actions by executing `make build-all` from the command line or specific actions i.e. `make twitch-talk`. ### Windows --- 'a/build.sh' +++ b/build.sh @@ -129,6 +129,9 @@ twitch() { cd ../emote-only && mvn clean -Dmaven.test.skip package mv target/twitch-emote-only-*.jar ../../$FOLD/twitch-emote-only.jar + cd ../followers-only && mvn clean -Dmaven.test.skip package + mv target/twitch-followers-only-*.jar ../../$FOLD/twitch-followers-only.jar + popd || exit } --- 'a/twitch/README.md' +++ b/twitch/README.md @@ -28,11 +28,8 @@ You should then be able to use the pre-b - Add stream marker - Toggle slow mode - Toggle subs-only mode - -### TODO - - Toggle followers-only mode ## Running locally -Copy the `Java-Twirk` from the `Dependencies` directory and the `twitch-xxx-action` jar files from the `PreBuiltPlugins` directory to your Stream-Pi server plugins' directory. \ No newline at end of file +Copy the `twitch-api-java` from the `Dependencies` directory and the `twitch-xxx-action` jar files from the `PreBuiltPlugins` directory to your Stream-Pi server plugins' directory. \ No newline at end of file --- /dev/null +++ b/twitch/followers-only/pom.xml @@ -0,0 +1,85 @@ + + + 4.0.0 + + com.stream-pi + twitch-followers-only + 1.0.0 + + + 11 + 11 + 1.0.0-SNAPSHOT + 1.0.0 + 0.7.1 + + + + + jitpack.io + https://jitpack.io + + + sonatype-snapshots + https://oss.sonatype.org/content/repositories/snapshots/ + + + + + + + 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 + action-api + ${streamPiActionApiVersion} + + + + com.github.j4ckofalltrades + twitch-api-java + ${twitchApiVersion} + + + + com.stream-pi + twitch-chat-connect + ${streamPiTwitchChatConnectVersion} + + + + + + sonatype-snapshots + https://oss.sonatype.org/content/repositories/snapshots/ + + + + --- /dev/null +++ b/twitch/followers-only/src/main/java/followers_only/FollowersOnlyAction.java @@ -0,0 +1,154 @@ +package followers_only; + +import com.gikk.twirk.Twirk; +import com.gikk.twirk.TwirkBuilder; +import com.gikk.twirk.events.TwirkListener; +import com.gikk.twirk.types.roomstate.Roomstate; +import com.stream_pi.action_api.actionproperty.property.ListValue; +import com.stream_pi.action_api.actionproperty.property.Property; +import com.stream_pi.action_api.actionproperty.property.Type; +import com.stream_pi.action_api.externalplugin.ToggleAction; +import com.stream_pi.util.exception.MinorException; +import com.stream_pi.util.version.Version; +import connect.chat.TwitchChatCredentials; + +import java.util.ArrayList; +import java.util.Arrays; + +public class FollowersOnlyAction extends ToggleAction +{ + + private final String channelNameKey = "channel_name_fo"; + private final String durationKey = "duration_fo"; + private final ArrayList durations; + + private Twirk twirk; + + public FollowersOnlyAction() + { + setName("Toggle Followers-Only Chat"); + setCategory("Twitch Chat"); + setVisibilityInServerSettingsPane(false); + setAuthor("j4ckofalltrades"); + setVersion(new Version(1, 0, 0)); + setHelpLink("https://github.com/stream-pi/essentialactions#twitch-chat-integration"); + + durations = new ArrayList<>(); + durations.addAll(Arrays.asList( + new ListValue("all"), + new ListValue("30m"), + new ListValue("2h"), + new ListValue("2d"), + new ListValue("1w"), + new ListValue("3mo") + )); + } + + + @Override + public void initProperties() throws MinorException + { + Property channelName = new Property(channelNameKey, Type.STRING); + channelName.setDisplayName("Channel Name"); + channelName.setDefaultValueStr("channel_name"); + channelName.setCanBeBlank(false); + + Property duration = new Property(durationKey, Type.LIST); + duration.setDisplayName("Follow Duration"); + duration.setListValue(durations); + + addClientProperties(channelName, duration); + } + + @Override + public void onToggleOn() throws MinorException + { + try + { + connectToChannel(); + final String followerDuration = durations.get(getClientProperties().getSingleProperty(durationKey) + .getSelectedIndex()).getName().toString(); + + if (followerDuration.equals("all")) + { + twirk.channelMessage("/followers"); + } else { + twirk.channelMessage(String.format("/followers %s", followerDuration)); + } + } catch (Exception ex) + { + setCurrentStatus(false); + throw new MinorException("Failed to enable followers only mode.", "Please try again."); + } + } + + @Override + public void onToggleOff() throws MinorException + { + try + { + connectToChannel(); + twirk.channelMessage("/followersoff"); + } catch (Exception ex) + { + setCurrentStatus(true); + throw new MinorException("Failed to disable followers only mode.", "Please try again."); + } + } + + private void connectToChannel() throws Exception + { + if (twirk != null) + { + if (!twirk.isConnected()) + { + twirk.connect(); + } + + return; + } + + final TwitchChatCredentials.ChatCredentials credentials = TwitchChatCredentials.getCredentials(); + credentials.ensureCredentialsInitialized(); + final String channel = getClientProperties().getSingleProperty(channelNameKey).getStringValue(); + + try + { + twirk = new TwirkBuilder(channel, credentials.getNickname(), credentials.getOauthToken()).build(); + twirk.addIrcListener(new TwirkListener() + { + @Override + public void onRoomstate(Roomstate roomstate) + { + try + { + setCurrentStatus(roomstate.getFollowersMode() == 1); + } catch (MinorException e) + { + throw new RuntimeException(e); + } + } + }); + twirk.connect(); + } catch (Exception ex) + { + throw new MinorException("Failed to connect to Twitch", + String.format("Could not connect to '%s' channel.", channel)); + } + } + + @Override + public void onShutDown() throws MinorException + { + if (twirk != null) + { + try + { + twirk.disconnect(); + } catch (Exception ex) + { + throw new MinorException("Twitch connection error", "Please try again."); + } + } + } +} --- /dev/null +++ b/twitch/followers-only/src/main/java/module-info.java @@ -0,0 +1,9 @@ +module com.stream_pi.twitch.followersonlyaction { + requires com.stream_pi.util; + requires com.stream_pi.action_api; + + requires com.stream_pi.twitchchatconnectaction; + requires twitch.api.java; + + provides com.stream_pi.action_api.externalplugin.ExternalPlugin with followers_only.FollowersOnlyAction; +}