essential-actions

Clone or download

Small optimisations to twitch plugin

Modified Files

<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<modelVersion>4.0.0</modelVersion>
<groupId>com.stream-pi</groupId>
<groupId>com.stream-pi</groupId>
<artifactId>twitch-send-channel-msg</artifactId>
<artifactId>twitch-send-channel-msg</artifactId>
<version>1.0.0</version>
<version>1.0.0</version>
<properties>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.target>11</maven.compiler.target>
<streamPiActionApiVersion>1.0.0</streamPiActionApiVersion>
<streamPiActionApiVersion>1.0.0-SNAPSHOT</streamPiActionApiVersion>
<streamPiUtilVersion>1.0.0</streamPiUtilVersion>
<streamPiUtilVersion>1.0.0-SNAPSHOT</streamPiUtilVersion>
<streamPiTwitchChatConnectVersion>1.0.0</streamPiTwitchChatConnectVersion>
<streamPiTwitchChatConnectVersion>1.0.0</streamPiTwitchChatConnectVersion>
<javaTwirkVersion>0.6.3</javaTwirkVersion>
<javaTwirkVersion>0.6.3</javaTwirkVersion>
</properties>
</properties>
<repositories>
<repositories>
<repository>
<repository>
<id>jitpack.io</id>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
<url>https://jitpack.io</url>
</repository>
</repository>
</repositories>
</repositories>
<build>
<build>
<plugins>
<plugins>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<version>3.1.0</version>
<executions>
<executions>
<execution>
<execution>
<id>test-jar</id>
<id>test-jar</id>
<phase>package</phase>
<phase>package</phase>
<goals>
<goals>
<goal>test-jar</goal>
<goal>test-jar</goal>
</goals>
</goals>
</execution>
</execution>
</executions>
</executions>
</plugin>
</plugin>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.8.1</version>
<configuration>
<configuration>
<release>11</release>
<release>11</release>
</configuration>
</configuration>
</plugin>
</plugin>
</plugins>
</plugins>
</build>
</build>
<dependencies>
<dependencies>
<dependency>
<dependency>
<groupId>com.stream-pi</groupId>
<groupId>com.stream-pi</groupId>
<artifactId>util</artifactId>
<artifactId>util</artifactId>
<version>${streamPiUtilVersion}</version>
<version>${streamPiUtilVersion}</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>com.stream-pi</groupId>
<groupId>com.stream-pi</groupId>
<artifactId>action-api</artifactId>
<artifactId>action-api</artifactId>
<version>${streamPiActionApiVersion}</version>
<version>${streamPiActionApiVersion}</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>com.github.gikkman</groupId>
<groupId>com.github.gikkman</groupId>
<artifactId>Java-Twirk</artifactId>
<artifactId>Java-Twirk</artifactId>
<version>${javaTwirkVersion}</version>
<version>${javaTwirkVersion}</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>com.stream-pi</groupId>
<groupId>com.stream-pi</groupId>
<artifactId>twitch-chat-connect</artifactId>
<artifactId>twitch-chat-connect</artifactId>
<version>${streamPiTwitchChatConnectVersion}</version>
<version>${streamPiTwitchChatConnectVersion}</version>
</dependency>
</dependency>
</dependencies>
</dependencies>
</project>
</project>
package sendchannelmsg;
package sendchannelmsg;
import com.gikk.twirk.Twirk;
import com.gikk.twirk.Twirk;
import com.gikk.twirk.TwirkBuilder;
import com.gikk.twirk.TwirkBuilder;
import com.stream_pi.action_api.actionproperty.property.Property;
import com.stream_pi.action_api.actionproperty.property.Property;
import com.stream_pi.action_api.actionproperty.property.Type;
import com.stream_pi.action_api.actionproperty.property.Type;
import com.stream_pi.action_api.normalaction.NormalAction;
import com.stream_pi.action_api.normalaction.NormalAction;
import com.stream_pi.util.alert.StreamPiAlert;
import com.stream_pi.util.alert.StreamPiAlert;
import com.stream_pi.util.alert.StreamPiAlertType;
import com.stream_pi.util.alert.StreamPiAlertType;
import com.stream_pi.util.exception.StreamPiException;
import com.stream_pi.util.version.Version;
import com.stream_pi.util.version.Version;
import connect.chat.TwitchChatCredentials;
import connect.chat.TwitchChatCredentials;
public class SendChannelMessageAction extends NormalAction
public class SendChannelMessageAction extends NormalAction
{
{
private static final String TWITCH_CHANNEL_NAME_KEY = "twitch_channel_name";
private static final String TWITCH_CHANNEL_NAME_KEY = "twitch_channel_name";
private static final String TWITCH_CHANNEL_MSG_KEY = "twitch_channel_msg";
private static final String TWITCH_CHANNEL_MSG_KEY = "twitch_channel_msg";
private Twirk twirk;
private Twirk twirk;
public SendChannelMessageAction()
public SendChannelMessageAction()
{
{
setName("Send Channel Message");
setName("Send Channel Message");
setCategory("Twitch Chat");
setCategory("Twitch Chat");
setVisibilityInServerSettingsPane(false);
setVisibilityInServerSettingsPane(false);
setAuthor("j4ckofalltrades");
setAuthor("j4ckofalltrades");
setVersion(new Version(1, 0, 0));
setVersion(new Version(1, 0, 0));
setHelpLink("https://github.com/stream-pi/essentialactions");
setHelpLink("https://github.com/stream-pi/essentialactions");
}
}
@Override
@Override
public void initProperties() throws Exception
public void initProperties() throws Exception
{
{
Property channelName = new Property(TWITCH_CHANNEL_NAME_KEY, Type.STRING);
Property channelName = new Property(TWITCH_CHANNEL_NAME_KEY, Type.STRING);
channelName.setDisplayName("Channel Name");
channelName.setDisplayName("Channel Name");
channelName.setDefaultValueStr("channel_name");
channelName.setDefaultValueStr("channel_name");
channelName.setCanBeBlank(false);
channelName.setCanBeBlank(false);
Property channelMessage = new Property(TWITCH_CHANNEL_MSG_KEY, Type.STRING);
Property channelMessage = new Property(TWITCH_CHANNEL_MSG_KEY, Type.STRING);
channelMessage.setDisplayName("Message");
channelMessage.setDisplayName("Message");
channelMessage.setDefaultValueStr("channel_msg");
channelMessage.setDefaultValueStr("channel_msg");
channelMessage.setCanBeBlank(false);
channelMessage.setCanBeBlank(false);
addClientProperties(channelName, channelMessage);
addClientProperties(channelName, channelMessage);
}
}
@Override
@Override
public void initAction() throws Exception
public void initAction() throws Exception
{
{
}
}
@Override
@Override
public void onActionClicked() throws Exception
public void onActionClicked() throws Exception
{
{
TwitchChatCredentials.ChatCredentials credentials = TwitchChatCredentials.getCredentials();
TwitchChatCredentials.ChatCredentials credentials = TwitchChatCredentials.getCredentials();
if (!isChatCredentialsInitialized(credentials))
if (!isChatCredentialsInitialized(credentials))
{
{
new StreamPiAlert(
throw new StreamPiException("Twitch Chat uninitialized.","Please check that the Twitch Chat plugin configuration is correct.");
"Twitch Chat plugin configuration uninitialized",
"Please check that the Twitch Chat plugin configuration is correct.",
StreamPiAlertType.ERROR
).show();
return;
}
}
final String channel = getClientProperties().getSingleProperty(TWITCH_CHANNEL_NAME_KEY).getStringValue();
final String channel = getClientProperties().getSingleProperty(TWITCH_CHANNEL_NAME_KEY).getStringValue();
final String message = getClientProperties().getSingleProperty(TWITCH_CHANNEL_MSG_KEY).getStringValue();
final String message = getClientProperties().getSingleProperty(TWITCH_CHANNEL_MSG_KEY).getStringValue();
try
try
{
{
twirk = new TwirkBuilder(
twirk = new TwirkBuilder(
getClientProperties().getSingleProperty(TWITCH_CHANNEL_NAME_KEY).getStringValue(),
getClientProperties().getSingleProperty(TWITCH_CHANNEL_NAME_KEY).getStringValue(),
credentials.getNickname(),
credentials.getNickname(),
credentials.getOauthToken())
credentials.getOauthToken())
.build();
.build();
twirk.connect();
twirk.connect();
twirk.channelMessage(message);
twirk.channelMessage(message);
} catch (Exception ex)
} catch (Exception ex)
{
{
new StreamPiAlert(
throw new StreamPiException(
"Failed to send channel message",
"Failed to send channel message",
String.format("Could not send message '%s' to '%s' channel, please try again.",
String.format("Could not send message '%s' to '%s' channel, please try again.",
channel, message),
channel, message)
StreamPiAlertType.ERROR
);
).show();
}
}
}
}
private boolean isChatCredentialsInitialized(TwitchChatCredentials.ChatCredentials credentials)
private boolean isChatCredentialsInitialized(TwitchChatCredentials.ChatCredentials credentials)
{
{
if (credentials == null)
if (credentials == null)
{
{
return false;
return false;
}
}
final String twitchNickname = credentials.getNickname();
final String twitchNickname = credentials.getNickname();
boolean isNicknameInitialized = twitchNickname != null &&
boolean isNicknameInitialized = twitchNickname != null &&
!twitchNickname.isEmpty() &&
!twitchNickname.isEmpty() &&
!twitchNickname.equalsIgnoreCase("twitch_nickname");
!twitchNickname.equalsIgnoreCase("twitch_nickname");
final String twitchChatOauthToken = credentials.getOauthToken();
final String twitchChatOauthToken = credentials.getOauthToken();
boolean isTokenInitialized = twitchChatOauthToken != null &&
boolean isTokenInitialized = twitchChatOauthToken != null &&
!twitchChatOauthToken.isEmpty() &&
!twitchChatOauthToken.isEmpty() &&
!twitchChatOauthToken.equalsIgnoreCase("twitch_oauth_token");
!twitchChatOauthToken.equalsIgnoreCase("twitch_oauth_token");
return isNicknameInitialized && isTokenInitialized;
return isNicknameInitialized && isTokenInitialized;
}
}
@Override
@Override
public void onShutDown() throws Exception
public void onShutDown() throws Exception
{
{
twirk.close();
twirk.close();
}
}
}
}