[AB-64] adding voice context channel feature

This commit is contained in:
Sheldan
2021-07-12 00:56:15 +02:00
parent 7e7591a4b3
commit c7514a6bad
40 changed files with 2327 additions and 1 deletions

View File

@@ -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;
}
}

View File

@@ -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");
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}