Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support new mod announcement command #556

Merged
merged 2 commits into from Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions chat/src/main/java/com/github/twitch4j/chat/ITwitchChat.java
Expand Up @@ -251,4 +251,17 @@ default boolean unban(String channel, String user) {
return this.sendMessage(channel, String.format("/unban %s", user));
}

/**
* Send a mod announcement (accented message)
*
* @param channel the name of the channel to send the announcement in.
* @param message the message to be announced.
* @return whether the command was added to the queue
* @see <a href="https://twitter.com/TwitchSupport/status/1509634525982302208">Official Announcement</a>
*/
@Unofficial
default boolean sendAnnouncement(String channel, String message) {
return this.sendMessage(channel, String.format("/announce %s", message));
}

}
@@ -0,0 +1,27 @@
package com.github.twitch4j.chat.enums;

import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
import com.github.twitch4j.common.annotation.Unofficial;

@Unofficial
public enum AnnouncementColor {

@JsonEnumDefaultValue
PRIMARY,
BLUE,
GREEN,
ORANGE,
PURPLE;

private static final AnnouncementColor[] COLORS = values();

@Unofficial
public static AnnouncementColor parseColor(String colorStr) {
for (AnnouncementColor color : COLORS) {
if (color.toString().equalsIgnoreCase(colorStr))
return color;
}
return PRIMARY;
}

}
Expand Up @@ -2,9 +2,11 @@

import com.github.philippheuer.events4j.core.EventManager;
import com.github.twitch4j.chat.TwitchChat;
import com.github.twitch4j.chat.enums.AnnouncementColor;
import com.github.twitch4j.chat.enums.NoticeTag;
import com.github.twitch4j.chat.events.channel.*;
import com.github.twitch4j.chat.events.roomstate.*;
import com.github.twitch4j.common.annotation.Unofficial;
import com.github.twitch4j.common.enums.SubscriptionPlan;
import com.github.twitch4j.common.events.domain.EventChannel;
import com.github.twitch4j.common.events.domain.EventUser;
Expand Down Expand Up @@ -57,6 +59,7 @@ public IRCEventHandler(TwitchChat twitchChat) {
// register event handlers
eventManager.onEvent("twitch4j-chat-message-trigger", IRCMessageEvent.class, this::onChannelMessage);
eventManager.onEvent("twitch4j-chat-whisper-trigger", IRCMessageEvent.class, this::onWhisper);
eventManager.onEvent("twitch4j-chat-announcement-trigger", IRCMessageEvent.class, this::onAnnouncement);
eventManager.onEvent("twitch4j-chat-bits-badge-trigger", IRCMessageEvent.class, this::onBitsBadgeTier);
eventManager.onEvent("twitch4j-chat-cheer-trigger", IRCMessageEvent.class, this::onChannelCheer);
eventManager.onEvent("twitch4j-chat-sub-trigger", IRCMessageEvent.class, this::onChannelSubscription);
Expand All @@ -82,6 +85,20 @@ public IRCEventHandler(TwitchChat twitchChat) {
eventManager.onEvent("twitch4j-chat-globaluserstate-trigger", IRCMessageEvent.class, this::onGlobalUserState);
}

@Unofficial
public void onAnnouncement(IRCMessageEvent event) {
if ("USERNOTICE".equals(event.getCommandType()) && "announcement".equalsIgnoreCase(event.getTags().get("msg-id"))) {
// Load Info
EventChannel channel = event.getChannel();
EventUser user = event.getUser();
String message = event.getMessage().orElse("");
String color = event.getTagValue("msg-param-color").orElse(AnnouncementColor.PRIMARY.toString());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// Dispatch Event
eventManager.publish(new ModAnnouncementEvent(event, channel, user, message, AnnouncementColor.parseColor(color)));
}
}

/**
* ChatChannel Message Event
* @param event IRCMessageEvent
Expand Down
@@ -0,0 +1,63 @@
package com.github.twitch4j.chat.events.channel;

import com.github.twitch4j.chat.enums.AnnouncementColor;
import com.github.twitch4j.chat.events.AbstractChannelEvent;
import com.github.twitch4j.common.annotation.Unofficial;
import com.github.twitch4j.common.events.domain.EventChannel;
import com.github.twitch4j.common.events.domain.EventUser;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.Value;

/**
* Fired when the broadcaster or a moderator publishes an accented message via {@code /announce}.
* <p>
* Since this event is not officially documented in the IRC guide, it could change or stop working at any time.
*
* @see <a href="https://twitter.com/TwitchSupport/status/1509634525982302208">Official Announcement</a>
*/
@Value
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Unofficial
public class ModAnnouncementEvent extends AbstractChannelEvent {

/**
* The raw message event.
*/
@ToString.Exclude
IRCMessageEvent messageEvent;

/**
* The user that made the announcement.
*/
EventUser announcer;

/**
* The message being announced.
*/
String message;

/**
* The color accent for the announcement.
*/
AnnouncementColor color;

/**
* Event Constructor
*
* @param messageEvent The raw message event.
* @param channel The channel that this event originates from.
* @param announcer The user that made the announcement.
* @param message The message being announced.
* @param color The color accent for the announcement.
*/
public ModAnnouncementEvent(IRCMessageEvent messageEvent, EventChannel channel, EventUser announcer, String message, AnnouncementColor color) {
super(channel);
this.messageEvent = messageEvent;
this.announcer = announcer;
this.message = message;
this.color = color;
}

}