mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-15 12:10:21 +00:00
[AB-64] adding voice context channel feature
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package dev.sheldan.abstracto.core.command.handler;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.Command;
|
||||
import dev.sheldan.abstracto.core.command.CommandConstants;
|
||||
import dev.sheldan.abstracto.core.command.config.Parameter;
|
||||
import dev.sheldan.abstracto.core.command.exception.AbstractoTemplatedException;
|
||||
import dev.sheldan.abstracto.core.command.execution.ParameterPieceType;
|
||||
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
|
||||
import dev.sheldan.abstracto.core.command.handler.provided.VoiceChannelParameterHandler;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.VoiceChannel;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
@Component
|
||||
public class VoiceChannelParameterHandlerImpl implements VoiceChannelParameterHandler {
|
||||
@Override
|
||||
public boolean handles(Class clazz, UnparsedCommandParameterPiece value) {
|
||||
return clazz.equals(VoiceChannel.class) && value.getType().equals(ParameterPieceType.STRING);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object handle(UnparsedCommandParameterPiece input, CommandParameterIterators iterators, Parameter param, Message context, Command command) {
|
||||
String inputString = ((String) input.getValue()).trim();
|
||||
Matcher matcher = Message.MentionType.CHANNEL.getPattern().matcher(inputString);
|
||||
if(matcher.matches()) {
|
||||
long channelId = Long.parseLong(matcher.group(1));
|
||||
return context.getGuild().getVoiceChannelById(channelId);
|
||||
} else {
|
||||
if(NumberUtils.isParsable(inputString)) {
|
||||
long channelId = Long.parseLong(inputString);
|
||||
return context.getGuild().getVoiceChannelById(channelId);
|
||||
} else {
|
||||
List<VoiceChannel> possibleVoiceChannels = context.getGuild().getVoiceChannelsByName(inputString, true);
|
||||
if(possibleVoiceChannels.isEmpty()) {
|
||||
throw new AbstractoTemplatedException("No channel found with name.", "no_channel_found_by_name_exception");
|
||||
}
|
||||
if(possibleVoiceChannels.size() > 1) {
|
||||
throw new AbstractoTemplatedException("Multiple channels found with name.", "multiple_channels_found_by_name_exception");
|
||||
}
|
||||
return possibleVoiceChannels.get(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPriority() {
|
||||
return CommandConstants.CORE_HANDLER_PRIORITY;
|
||||
}
|
||||
}
|
||||
@@ -147,4 +147,14 @@ public class ListenerExecutorConfig {
|
||||
return executorService.setupExecutorFor("userBannedListener");
|
||||
}
|
||||
|
||||
@Bean(name = "voiceChatJoinedExecutor")
|
||||
public TaskExecutor voiceChatJoinedExecutor() {
|
||||
return executorService.setupExecutorFor("voiceChatJoinedListener");
|
||||
}
|
||||
|
||||
@Bean(name = "voiceChatLeftExecutor")
|
||||
public TaskExecutor voiceChatLeftExecutor() {
|
||||
return executorService.setupExecutorFor("voiceChatLeftListener");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package dev.sheldan.abstracto.core.listener.async.jda;
|
||||
|
||||
import dev.sheldan.abstracto.core.listener.ListenerService;
|
||||
import dev.sheldan.abstracto.core.models.listener.VoiceChannelJoinedModel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AsyncVoiceChannelJoinedListenerBean extends ListenerAdapter {
|
||||
|
||||
@Autowired(required = false)
|
||||
private List<AsyncVoiceChannelJoinedListener> listenerList;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("voiceChatJoinedExecutor")
|
||||
private TaskExecutor voiceChatJoinedExecutor;
|
||||
|
||||
@Autowired
|
||||
private ListenerService listenerService;
|
||||
|
||||
@Override
|
||||
public void onGuildVoiceJoin(@NotNull GuildVoiceJoinEvent event) {
|
||||
if(listenerList == null) return;
|
||||
VoiceChannelJoinedModel model = getModel(event);
|
||||
listenerList.forEach(leaveListener -> listenerService.executeFeatureAwareListener(leaveListener, model, voiceChatJoinedExecutor));
|
||||
}
|
||||
|
||||
private VoiceChannelJoinedModel getModel(GuildVoiceJoinEvent event) {
|
||||
return VoiceChannelJoinedModel
|
||||
.builder()
|
||||
.channel(event.getChannelJoined())
|
||||
.member(event.getMember())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package dev.sheldan.abstracto.core.listener.async.jda;
|
||||
|
||||
import dev.sheldan.abstracto.core.listener.ListenerService;
|
||||
import dev.sheldan.abstracto.core.models.listener.VoiceChannelLeftModel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AsyncVoiceChannelLeftListenerBean extends ListenerAdapter {
|
||||
|
||||
@Autowired
|
||||
private List<AsyncVoiceChannelLeftListener> listenerList;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("voiceChatLeftExecutor")
|
||||
private TaskExecutor voiceChatLeaveExecutor;
|
||||
|
||||
@Autowired
|
||||
private ListenerService listenerService;
|
||||
|
||||
@Override
|
||||
public void onGuildVoiceLeave(@NotNull GuildVoiceLeaveEvent event) {
|
||||
if(listenerList == null) return;
|
||||
VoiceChannelLeftModel model = getModel(event);
|
||||
listenerList.forEach(leaveListener -> listenerService.executeFeatureAwareListener(leaveListener, model, voiceChatLeaveExecutor));
|
||||
}
|
||||
|
||||
private VoiceChannelLeftModel getModel(GuildVoiceLeaveEvent event) {
|
||||
return VoiceChannelLeftModel
|
||||
.builder()
|
||||
.channel(event.getChannelLeft())
|
||||
.member(event.getMember())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user