Skip to content

Commit

Permalink
✨ add/remove/get all vips
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny committed Mar 31, 2024
1 parent a7ad4ac commit 051a4e4
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 0 deletions.
@@ -0,0 +1,82 @@
package com.trason.skitch.elements.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import com.github.twitch4j.helix.domain.User;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import com.trason.skitch.elements.events.bukkit.BridgeEventChat;
import com.trason.skitch.elements.events.custom.CommandEvent;
import com.trason.skitch.util.ConsoleMessages.console;
import org.apache.commons.lang3.exception.ContextedRuntimeException;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

import static com.trason.skitch.elements.effects.EffLoginTwitchBot.client;

@Name("Twitch Add Channel VIP")
@Description("Adds a VIP to the broadcaster’s chat room.\n" +
"The bot needs broadcaster permission!!! (So only selfbots work here!)")
@Examples("add twitch channel vip \"username\"")
@Since("1.4.1")
public class EffAddChannelVip extends Effect {
static {
// This is the registration of the effect
Skript.registerEffect(EffAddChannelVip.class, "add [twitch] channel vip %string%");
}

private Expression<String> exprUserName;
@Override
protected void execute(@NotNull Event event) {
try {
if (event instanceof BridgeEventChat) {
String broadcasterID = ((BridgeEventChat) event).getEvent().getChannel().getId();
List<User> userName = client.getHelix().getUsers(null, null, Collections.singletonList(exprUserName.getSingle(event))).execute().getUsers();
String userId = userName.get(0).getId();
client.getHelix().addChannelVip(null, broadcasterID,userId).execute();
}
else if (event instanceof CommandEvent) {
String broadcasterID = ((CommandEvent) event).getEvent().getChannel().getId();
List<User> userName = client.getHelix().getUsers(null, null, Collections.singletonList(exprUserName.getSingle(event))).execute().getUsers();
String userId = userName.get(0).getId();
client.getHelix().addChannelVip(null, broadcasterID,userId).execute();
}
} catch (HystrixRuntimeException e) {
Throwable cause = e.getCause();
if (cause instanceof ContextedRuntimeException) {
ContextedRuntimeException cre = (ContextedRuntimeException) cause;
Object error = cre.getFirstContextValue("errorMessage");
if (error == null) {
console.error("Error:[AddChannelVip] You don't have the broadcaster permission to add a vip.");
}
else {
console.error("Error:[AddChannelVip] " + error);
}
}

}


}

@Override
public @NotNull String toString(Event event, boolean debug) {
return "add [twitch] channel vip " + exprUserName.toString(event, debug);
}

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] expressions, int i, @NotNull Kleenean kleenean, SkriptParser.@NotNull ParseResult parseResult) {
this.exprUserName = (Expression<String>) expressions[0];
return true;
}
}
@@ -0,0 +1,83 @@
package com.trason.skitch.elements.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import com.github.twitch4j.helix.domain.User;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import com.trason.skitch.elements.events.bukkit.BridgeEventChat;
import com.trason.skitch.elements.events.custom.CommandEvent;
import com.trason.skitch.util.ConsoleMessages.console;
import jdk.jfr.Name;
import org.apache.commons.lang3.exception.ContextedRuntimeException;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

import static com.trason.skitch.elements.effects.EffLoginTwitchBot.client;

@Name("Twitch Remove Channel VIP")
@Description("Removes a VIP from the broadcaster’s chat room.\n" +
"The bot needs broadcaster permission!!! (So only selfbots work here!)")
@Examples("remove twitch channel vip \"username\"")
@Since("1.4.1")
public class EffRemoveChannelVip extends Effect {
static {
// This is the registration of the effect
Skript.registerEffect(EffRemoveChannelVip.class, "remove [twitch] channel vip %string%");
}

private Expression<String> exprUserName;
@Override
protected void execute(@NotNull Event event) {
try {
if (event instanceof BridgeEventChat) {
String broadcasterID = ((BridgeEventChat) event).getEvent().getChannel().getId();
List<User> userName = client.getHelix().getUsers(null, null, Collections.singletonList(exprUserName.getSingle(event))).execute().getUsers();
String userId = userName.get(0).getId();
client.getHelix().removeChannelVip(null, broadcasterID,userId).execute();
}
else if (event instanceof CommandEvent) {
String broadcasterID = ((CommandEvent) event).getEvent().getChannel().getId();
List<User> userName = client.getHelix().getUsers(null, null, Collections.singletonList(exprUserName.getSingle(event))).execute().getUsers();
String userId = userName.get(0).getId();
client.getHelix().removeChannelVip(null, broadcasterID,userId).execute();
}
} catch (HystrixRuntimeException e) {
Throwable cause = e.getCause();
if (cause instanceof ContextedRuntimeException) {
ContextedRuntimeException cre = (ContextedRuntimeException) cause;
Object error = cre.getFirstContextValue("errorMessage");
if (error == null) {
console.error("Error:[RemoveChannelVip] You don't have the broadcaster permission to add a vip.");
}
else {
console.error("Error:[RemoveChannelVip] " + error);
}
}

}


}

@Override
public @NotNull String toString(Event event, boolean debug) {
return "add [twitch] channel vip " + exprUserName.toString(event, debug);
}

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] expressions, int i, @NotNull Kleenean kleenean, SkriptParser.@NotNull ParseResult parseResult) {
this.exprUserName = (Expression<String>) expressions[0];
return true;
}
}

@@ -0,0 +1,71 @@
package com.trason.skitch.elements.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import com.github.twitch4j.helix.domain.ChannelVip;
import com.github.twitch4j.helix.domain.ChannelVipList;
import com.trason.skitch.elements.events.bukkit.BridgeEventChat;
import com.trason.skitch.elements.events.custom.CommandEvent;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;


import static com.trason.skitch.elements.effects.EffLoginTwitchBot.client;

@Name("Channel Vips")
@Description("Gets a list of the channel’s VIPs.")
@Examples("set {_vips::*} to channel vips")
@Since("1.4.1")
public class ExprChannelVips extends SimpleExpression<String> {

static {
// This is the registration of the expression
Skript.registerExpression(ExprChannelVips.class, String.class, ExpressionType.SIMPLE, "[event-]channel vips");
}

@Override
protected String @NotNull [] get(@NotNull Event event) {
if (event instanceof BridgeEventChat) {
String channelId = ((BridgeEventChat) event).getEvent().getChannel().getId();
ChannelVipList vipList = client.getHelix().getChannelVips(null, channelId, null, 100, null).execute();
String[] vips = vipList.getData().stream().map(ChannelVip::getUserName).toArray(String[]::new);
return vips;
}
else if (event instanceof CommandEvent) {
String channelId = ((CommandEvent) event).getEvent().getChannel().getId();
ChannelVipList vipList = client.getHelix().getChannelVips(null, channelId, null, 100, null).execute();
String[] vips = vipList.getData().stream().map(ChannelVip::getUserName).toArray(String[]::new);
return vips;
}
else
return new String[0];
}

@Override
public boolean isSingle() {
return true;
}

@Override
public Class<? extends String> getReturnType() {
return String.class;
}

@Override
public @NotNull String toString(Event event, boolean b) {
return "channel vips";
}

@Override
public boolean init(Expression<?>[] expressions, int i, @NotNull Kleenean kleenean, SkriptParser.@NotNull ParseResult parseResult) {
return true;
}
}

0 comments on commit 051a4e4

Please sign in to comment.