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: parse experimental elevated chat payment details #663

Merged
merged 1 commit into from Oct 10, 2022
Merged
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
Expand Up @@ -7,12 +7,14 @@
import com.github.twitch4j.common.events.domain.EventChannel;
import com.github.twitch4j.common.events.domain.EventUser;
import com.github.twitch4j.common.util.ChatReply;
import com.github.twitch4j.common.util.DonationAmount;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import lombok.Value;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.Optional;
import java.util.Set;

Expand Down Expand Up @@ -104,4 +106,42 @@ public boolean isUserIntroduction() {
return "user-intro".equals(getMessageEvent().getTags().get("msg-id"));
}

/**
* @return the payment amount for the user to elevate their chat message.
* @apiNote This method is unofficial since the experiment is not officially documented in the irc guide.
* @see <a href ="https://twitter.com/TwitchSupport/status/1575603152908427272">Twitch Announcement</a>
*/
@Unofficial
public Optional<DonationAmount> getElevatedChatPayment() {
final Map<String, String> tags = getMessageEvent().getTags();

String amount = tags.get("pinned-chat-paid-amount");
if (amount == null) {
amount = tags.get("pinned-chat-paid-canonical-amount");
if (amount != null) {
amount += "00";
}
}

return Optional.ofNullable(amount)
.map(amt -> {
try {
return Long.parseLong(amt);
} catch (Exception e) {
return null;
}
})
.map(amt -> {
String currency = tags.getOrDefault("pinned-chat-paid-currency", "USD");
String exponentStr = tags.getOrDefault("pinned-chat-paid-exponent", "2");
int exponent;
try {
exponent = Integer.parseInt(exponentStr);
} catch (Exception e) {
return null;
}
return new DonationAmount(amt, exponent, currency);
});
}

}