essential-actions
Clone or download
Modified Files
package hostchannel;
package hostchannel;
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 HostChannelAction extends NormalAction
public class HostChannelAction extends NormalAction
{
{
private final String channelKey = "channel_hc";
private final String channelKey = "channel_hc";
private final String channelToHostKey = "channel_to_host_hc";
private final String channelToHostKey = "channel_to_host_hc";
private Twirk twirk;
private Twirk twirk;
public HostChannelAction()
public HostChannelAction()
{
{
setName("Host Channel");
setName("Host Channel");
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 initProperties() throws Exception
public void initProperties() throws MinorException
{
{
Property channel = new Property(channelKey, Type.STRING);
Property channel = new Property(channelKey, Type.STRING);
channel.setDisplayName("Channel");
channel.setDisplayName("Channel");
channel.setDefaultValueStr("channel");
channel.setDefaultValueStr("channel");
channel.setCanBeBlank(false);
channel.setCanBeBlank(false);
Property channelToHost = new Property(channelToHostKey, Type.STRING);
Property channelToHost = new Property(channelToHostKey, Type.STRING);
channelToHost.setDisplayName("Channel to host");
channelToHost.setDisplayName("Channel to host");
channelToHost.setDefaultValueStr("channel_to_host");
channelToHost.setDefaultValueStr("channel_to_host");
channelToHost.setCanBeBlank(false);
channelToHost.setCanBeBlank(false);
addClientProperties(channel, channelToHost);
addClientProperties(channel, channelToHost);
}
}
@Override
@Override
public void initAction() throws Exception
public void onActionClicked() throws MinorException
{
}
@Override
public void onActionClicked() throws Exception
{
{
final TwitchChatCredentials.ChatCredentials credentials = TwitchChatCredentials.getCredentials();
final TwitchChatCredentials.ChatCredentials credentials = TwitchChatCredentials.getCredentials();
credentials.ensureCredentialsInitialized();
credentials.ensureCredentialsInitialized();
final String channel = getClientProperties().getSingleProperty(channelKey).getStringValue();
final String channel = getClientProperties().getSingleProperty(channelKey).getStringValue();
final String channelToHost = getClientProperties().getSingleProperty(channelToHostKey).getStringValue();
final String channelToHost = getClientProperties().getSingleProperty(channelToHostKey).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(String.format("/host %s", channelToHost));
twirk.channelMessage(String.format("/host %s", channelToHost));
} catch (Exception ex)
} catch (Exception ex)
{
{
throw new StreamPiException(
throw new MinorException(
"Failed to host channel",
"Failed to host channel",
String.format("Could not host channel '%s', please try again.", channelToHost));
String.format("Could not host channel '%s', please try again.", channelToHost));
}
}
}
}
@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.");
}
}
}
}
}
}
}
}