Skip to content

Commit

Permalink
Added support for Stage Channels on recordings. And moved back to 2.1…
Browse files Browse the repository at this point in the history
….5-SQL because 2.1.6 is still not finished and buggy.
  • Loading branch information
DxsSucuk committed Dec 20, 2023
1 parent e1c5f02 commit 62cdc59
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
<dependency>
<groupId>de.ree6</groupId>
<artifactId>Ree6-SQL</artifactId>
<version>2.1.6</version>
<version>2.1.5</version>
</dependency>

<!-- Logging -->
Expand Down
70 changes: 41 additions & 29 deletions src/main/java/de/presti/ree6/audio/AudioPlayerReceiveHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.google.gson.Gson;
import com.google.gson.JsonParser;
import de.presti.ree6.bot.BotConfig;
import de.presti.ree6.language.LanguageService;
import de.presti.ree6.sql.SQLSession;
import de.presti.ree6.sql.entities.Recording;
import de.presti.ree6.utils.data.AudioUtil;
import de.presti.ree6.bot.BotConfig;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
Expand All @@ -15,7 +15,8 @@
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.unions.AudioChannelUnion;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
Expand All @@ -40,7 +41,7 @@
public class AudioPlayerReceiveHandler implements AudioReceiveHandler {

/**
* Queue of audio to be sent afterwards.
* Queue of audio to be sent afterward.
*/
private final Queue<byte[]> queue = new ConcurrentLinkedQueue<>();

Expand All @@ -57,7 +58,7 @@ public class AudioPlayerReceiveHandler implements AudioReceiveHandler {
/**
* The voice channel this handler is currently handling.
*/
private final VoiceChannel voiceChannel;
private final AudioChannelUnion audioChannelUnion;

/**
* A list with all IDs of users who where in the talk while recording.
Expand All @@ -73,18 +74,23 @@ public class AudioPlayerReceiveHandler implements AudioReceiveHandler {
* Constructor.
*
* @param member The member who started the recording.
* @param voiceChannel The voice channel this handler should handle.
* @param audioChannelUnion The voice channel this handler should handle.
*/
public AudioPlayerReceiveHandler(Member member, VoiceChannel voiceChannel) {
public AudioPlayerReceiveHandler(Member member, AudioChannelUnion audioChannelUnion) {
this.creatorId = member.getId();
this.voiceChannel = voiceChannel;
if (voiceChannel.getGuild().getSelfMember().hasPermission(Permission.NICKNAME_CHANGE)) {
voiceChannel.getGuild().getSelfMember().modifyNickname(LanguageService.getByGuild(member.getGuild(), "label.recording.name")).reason(LanguageService.getByGuild(member.getGuild(), "message.recording.startReason", member.getUser().getName())).onErrorMap(throwable -> {
if (voiceChannel.canTalk()) voiceChannel.sendMessage(LanguageService.getByGuild(member.getGuild(), "message.default.nameChangeFailed")).queue();
this.audioChannelUnion = audioChannelUnion;
if (audioChannelUnion.getGuild().getSelfMember().hasPermission(Permission.NICKNAME_CHANGE)) {
audioChannelUnion.getGuild().getSelfMember().modifyNickname(LanguageService.getByGuild(member.getGuild(), "label.recording.name")).reason(LanguageService.getByGuild(member.getGuild(), "message.recording.startReason", member.getUser().getName())).onErrorMap(throwable -> {

boolean canTalk = audioChannelUnion.getType() == ChannelType.STAGE ?
audioChannelUnion.asStageChannel().canTalk() :
audioChannelUnion.asVoiceChannel().canTalk();

if (canTalk) audioChannelUnion.asGuildMessageChannel().sendMessage(LanguageService.getByGuild(member.getGuild(), "message.default.nameChangeFailed")).queue();
return null;
}).queue();
}
message = voiceChannel.sendMessageEmbeds(new EmbedBuilder()
message = audioChannelUnion.asGuildMessageChannel().sendMessageEmbeds(new EmbedBuilder()
.setDescription(LanguageService.getByGuild(member.getGuild(), "message.recording.started"))
.setColor(Color.YELLOW)
.setFooter("Requested by " + member.getEffectiveName() + " - " + BotConfig.getAdvertisement(), member.getEffectiveAvatarUrl())
Expand Down Expand Up @@ -123,13 +129,13 @@ public void handleCombinedAudio(@NotNull CombinedAudio combinedAudio) {
}

if (combinedAudio.getUsers().isEmpty()) {
if (voiceChannel.getMembers().size() == 1) {
if (audioChannelUnion.getMembers().size() == 1) {
endReceiving();
}
return;
}

if (voiceChannel.getMembers().size() == 1) {
if (audioChannelUnion.getMembers().size() == 1) {
endReceiving();
return;
}
Expand All @@ -155,9 +161,14 @@ public void endReceiving() {

finished = true;

if (voiceChannel.getGuild().getSelfMember().hasPermission(Permission.NICKNAME_CHANGE)) {
voiceChannel.getGuild().getSelfMember().modifyNickname(voiceChannel.getGuild().getSelfMember().getUser().getName()).reason(LanguageService.getByGuild(voiceChannel.getGuild(), "message.recording.stopReason")).onErrorMap(throwable -> {
if (voiceChannel.canTalk()) voiceChannel.sendMessage(LanguageService.getByGuild(voiceChannel.getGuild(), "message.default.nameChangeFailed")).queue();
boolean canTalk = audioChannelUnion.getType() == ChannelType.STAGE ?
audioChannelUnion.asStageChannel().canTalk() :
audioChannelUnion.asVoiceChannel().canTalk();

if (audioChannelUnion.getGuild().getSelfMember().hasPermission(Permission.NICKNAME_CHANGE)) {
audioChannelUnion.getGuild().getSelfMember().modifyNickname(audioChannelUnion.getGuild().getSelfMember().getUser().getName()).reason(LanguageService.getByGuild(audioChannelUnion.getGuild(), "message.recording.stopReason")).onErrorMap(throwable -> {

if (canTalk) audioChannelUnion.asGuildMessageChannel().sendMessage(LanguageService.getByGuild(audioChannelUnion.getGuild(), "message.default.nameChangeFailed")).queue();
return null;
}).queue();
}
Expand All @@ -169,7 +180,7 @@ public void endReceiving() {
byteBuffer.put(data);
}

Recording recording = new Recording(voiceChannel.getGuild().getId(), voiceChannel.getId(), creatorId, AudioUtil.convertPCMtoWAV(byteBuffer),
Recording recording = new Recording(audioChannelUnion.getGuild().getId(), audioChannelUnion.getId(), creatorId, AudioUtil.convertPCMtoWAV(byteBuffer),
JsonParser.parseString(new Gson().toJson(participants)).getAsJsonArray());

boolean failedToUpload = false;
Expand All @@ -180,38 +191,39 @@ public void endReceiving() {
failedToUpload = true;
}

if (voiceChannel.canTalk()) {
if (canTalk) {
message.editMessageEmbeds(new EmbedBuilder()
.setDescription(LanguageService.getByGuild(voiceChannel.getGuild(), "message.recording.stopped"))
.setDescription(LanguageService.getByGuild(audioChannelUnion.getGuild(), "message.recording.stopped"))
.setColor(Color.GREEN)
.setFooter(BotConfig.getAdvertisement(), voiceChannel.getGuild().getIconUrl())
.setTitle(LanguageService.getByGuild(voiceChannel.getGuild(), "label.recording.finished"))
.setFooter(BotConfig.getAdvertisement(), audioChannelUnion.getGuild().getIconUrl())
.setTitle(LanguageService.getByGuild(audioChannelUnion.getGuild(), "label.recording.finished"))
.build())
.setActionRow(
new ButtonImpl("ree6RedirectButton", LanguageService.getByGuild(voiceChannel.getGuild(), "label.download"), ButtonStyle.LINK,
new ButtonImpl("ree6RedirectButton", LanguageService.getByGuild(audioChannelUnion.getGuild(), "label.download"), ButtonStyle.LINK,
BotConfig.getRecordingUrl() + "?id=" + recording.getIdentifier(), failedToUpload, Emoji.fromCustom("shiba", 941219375535509504L, true)),
Button.primary("r_recordingDownload:" + recording.getIdentifier(), Emoji.fromCustom("sip", 1011956355810209852L, false))
.withLabel(LanguageService.getByGuild(voiceChannel.getGuild(), "label.sendToChat")).withDisabled(!BotConfig.allowRecordingInChat() || failedToUpload)).complete();
.withLabel(LanguageService.getByGuild(audioChannelUnion.getGuild(), "label.sendToChat")).withDisabled(!BotConfig.allowRecordingInChat() || failedToUpload)).complete();

if (failedToUpload) {
voiceChannel.sendMessage(LanguageService.getByGuild(voiceChannel.getGuild(), "message.recording.error", "Upload failed")).setFiles(FileUpload.fromData(recording.getRecording(), "recording.wav"));
audioChannelUnion.asStageChannel().sendMessage(LanguageService.getByGuild(audioChannelUnion.getGuild(), "message.recording.error", "Upload failed")).setFiles(FileUpload.fromData(recording.getRecording(), "recording.wav"));
}
}
// Find a way to still notify that the bot couldn't send the audio.
} catch (Exception ex) {
if (voiceChannel.canTalk()) {

if (canTalk) {
message.editMessageEmbeds(new EmbedBuilder()
.setDescription(LanguageService.getByGuild(voiceChannel.getGuild(), "message.recording.error", ex.getMessage()))
.setDescription(LanguageService.getByGuild(audioChannelUnion.getGuild(), "message.recording.error", ex.getMessage()))
.setColor(Color.RED)
.setFooter(BotConfig.getAdvertisement(), voiceChannel.getGuild().getIconUrl())
.setTitle(LanguageService.getByGuild(voiceChannel.getGuild(), "label.error"))
.setFooter(BotConfig.getAdvertisement(), audioChannelUnion.getGuild().getIconUrl())
.setTitle(LanguageService.getByGuild(audioChannelUnion.getGuild(), "label.error"))
.build()).complete();
}

log.error("Something went wrong while converting a recording!", ex);
}

voiceChannel.getGuild().getAudioManager().closeAudioConnection();
audioChannelUnion.getGuild().getAudioManager().closeAudioConnection();
queue.clear();
}
}
12 changes: 8 additions & 4 deletions src/main/java/de/presti/ree6/commands/impl/fun/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.dv8tion.jda.api.entities.GuildVoiceState;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
import net.dv8tion.jda.api.entities.channel.unions.AudioChannelUnion;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.managers.AudioManager;

Expand Down Expand Up @@ -54,12 +55,15 @@ public void connectAndRecord(CommandEvent commandEvent) {
if (voiceState != null &&
voiceState.inAudioChannel() &&
voiceState.getChannel() != null &&
voiceState.getChannel().getType() == ChannelType.VOICE &&
voiceState.getChannel() instanceof VoiceChannel voiceChannel) {
(voiceState.getChannel().getType() == ChannelType.VOICE ||
voiceState.getChannel().getType() == ChannelType.STAGE)) {

AudioChannelUnion audioChannelUnion = voiceState.getChannel();

AudioManager audioManager = commandEvent.getGuild().getAudioManager();
audioManager.openAudioConnection(voiceChannel);
audioManager.openAudioConnection(audioChannelUnion);

AudioPlayerReceiveHandler handler = new AudioPlayerReceiveHandler(commandEvent.getMember(), voiceChannel);
AudioPlayerReceiveHandler handler = new AudioPlayerReceiveHandler(commandEvent.getMember(), audioChannelUnion);

audioManager.setReceivingHandler(handler);

Expand Down

0 comments on commit 62cdc59

Please sign in to comment.