essential-actions

Clone or download

Twitch - clear-chat - Updated to use new Action API

Modified Files

package clearchat;
package clearchat;
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.externalplugin.NormalAction;
import com.stream_pi.action_api.externalplugin.NormalAction;
import com.stream_pi.util.exception.MinorException;
import com.stream_pi.util.exception.StreamPiException;
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 ClearChatAction extends NormalAction
public class ClearChatAction extends NormalAction
{
{
private final String channelNameKey = "channel_name_cc";
private final String channelNameKey = "channel_name_cc";
private Twirk twirk;
private Twirk twirk;
@Override
@Override
public void initProperties() throws Exception
public void initProperties() throws MinorException
{
{
setName("Clear Chat");
setName("Clear Chat");
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#twitch-chat-integration");
setHelpLink("https://github.com/stream-pi/essentialactions#twitch-chat-integration");
}
}
@Override
@Override
public void initAction() throws Exception
public void initAction() throws MinorException
{
{
Property channelName = new Property(channelNameKey, Type.STRING);
Property channelName = new Property(channelNameKey, Type.STRING);
channelName.setDisplayName("Channel Name");
channelName.setDisplayName("Channel Name");
channelName.setDefaultValueStr("channel_name");
channelName.setDefaultValueStr("channel_name");
channelName.setCanBeBlank(false);
channelName.setCanBeBlank(false);
addClientProperties(channelName);
addClientProperties(channelName);
}
}
@Override
@Override
public void onActionClicked() throws Exception
public void onActionClicked() throws MinorException
{
{
final TwitchChatCredentials.ChatCredentials credentials = TwitchChatCredentials.getCredentials();
final TwitchChatCredentials.ChatCredentials credentials = TwitchChatCredentials.getCredentials();
credentials.ensureCredentialsInitialized();
credentials.ensureCredentialsInitialized();
final String channel = getClientProperties().getSingleProperty(channelNameKey).getStringValue();
final String channel = getClientProperties().getSingleProperty(channelNameKey).getStringValue();
try
try
{
{
twirk = new TwirkBuilder(channel, credentials.getNickname(), credentials.getOauthToken()).build();
twirk = new TwirkBuilder(channel, credentials.getNickname(), credentials.getOauthToken()).build();
twirk.connect();
twirk.connect();
twirk.channelMessage("/clear");
twirk.channelMessage("/clear");
} catch (Exception ex)
} catch (Exception ex)
{
{
throw new StreamPiException(
throw new MinorException(
"Failed to clear channel chat",
String.format("Could not clear chat for '%s' channel, please try again.", channel)
String.format("Could not clear chat for '%s' channel, please try again.", channel)
);
);
}
}
}
}
@Override
@Override
public void onShutDown() throws Exception
public void onShutDown() throws MinorException
{
{
if (twirk != null)
if (twirk != null)
{
{
try
try
{
{
twirk.disconnect();
twirk.disconnect();
} catch (Exception ex)
} catch (Exception ex)
{
{
throw new StreamPiException("Twitch Connection error", "Please try again.");
throw new MinorException("Twitch Connection error - Please try again.");
}
}
}
}
}
}
}
}