Skip to content

Commit

Permalink
Merge pull request #69 from Softawii/feature/optional-text
Browse files Browse the repository at this point in the history
fix: config modal title length
  • Loading branch information
yaansz committed Aug 15, 2022
2 parents 3d1c366 + 9d43bb5 commit 913f7a9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'com.softwaii.capivara'
version '1.0.5.0'
version '1.0.5.1'


repositories {
Expand Down
41 changes: 27 additions & 14 deletions src/main/java/com/softawii/capivara/core/DroneManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.softawii.capivara.listeners.VoiceGroup;
import com.softawii.capivara.services.VoiceDroneService;
import com.softawii.capivara.services.VoiceHiveService;
import com.softawii.capivara.utils.Utils;
import com.softawii.curupira.exceptions.MissingPermissionsException;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
Expand Down Expand Up @@ -71,11 +72,11 @@ public Modal checkConfigDrone(Guild guild, MessageChannelUnion channel, Member m

if (voiceDrone.getOwnerId().equals(member.getIdLong()) || member.hasPermission(Permission.MANAGE_CHANNEL)) {
Modal.Builder builder = Modal.create(customId, "Rename Room");
builder.setTitle("Settings of " + voice.getName() + "!")
builder.setTitle(Utils.getProperString("Settings of " + voice.getName() + "!", Modal.MAX_TITLE_LENGTH))
.addActionRow(
TextInput.create(renameDrone, "Room Name", TextInputStyle.SHORT)
.setValue(voice.getName())
.setMaxLength(256).build()
.setMaxLength(100).build()
).addActionRow(
TextInput.create(limitDrone, "Limit of Users (Number)", TextInputStyle.SHORT)
.setValue(String.valueOf(voice.getUserLimit()))
Expand Down Expand Up @@ -251,18 +252,25 @@ public void checkToCreateTemporary(VoiceChannel channel, Member member) {

// Create voice
VoiceChannel voice = hiveCategory.createVoiceChannel(droneName).complete();
TextChannel text = hiveCategory.createTextChannel(droneName).complete();
TextChannel text = hive.getCreateTextChannel() ? hiveCategory.createTextChannel(droneName).complete() : null;

// Hiding channel
Role publicRole = channel.getGuild().getPublicRole();
Guild guild = voice.getGuild();
text.upsertPermissionOverride(publicRole).deny(Permission.VIEW_CHANNEL)
.and(text.upsertPermissionOverride(member).grant(Permission.VIEW_CHANNEL))
.and(voice.upsertPermissionOverride(member).grant(Permission.VOICE_CONNECT, Permission.VIEW_CHANNEL))
.and(guild.moveVoiceMember(member, voice)).submit();

RestAction<Void> actions = guild.moveVoiceMember(member, voice)
.and(voice.upsertPermissionOverride(member).grant(Permission.VOICE_CONNECT, Permission.VIEW_CHANNEL));

if(text != null) {
actions.and(text.upsertPermissionOverride(publicRole).deny(Permission.VIEW_CHANNEL))
.and(text.upsertPermissionOverride(member).grant(Permission.VIEW_CHANNEL))
.submit();
} else {
actions.submit();
}

// Add voice to drone db
voiceDroneService.create(new VoiceDrone(voice.getIdLong(), text.getIdLong(), member.getIdLong(), null));
voiceDroneService.create(new VoiceDrone(voice.getIdLong(), text != null ? text.getIdLong() : 0L, member.getIdLong(), null));
LOGGER.debug("Creating New Channel!");
} catch (KeyNotFoundException e) {
LOGGER.debug("Key not found, ignoring...");
Expand Down Expand Up @@ -304,9 +312,9 @@ private void createControlPanel(VoiceChannel voiceChannel, GuildMessageChannel t
builder.addField("Status", drone.isPermanent() ? "Permanent" : "Temporary", true);

// Tutorials
builder.addField("Invite User", "/dynamic invite @user to invite someone to your channel", false);
builder.addField("Kick User", "/dynamic kick @user to kick someone from your channel", false);
builder.addField("Ban User", "/dynamic ban @user to ban someone from your channel", false);
builder.addField("Invite User", "/invite @user to invite someone to your channel", false);
builder.addField("Kick User", "/kick @user to kick someone from your channel", false);
builder.addField("Ban User", "/ban @user to ban someone from your channel", false);

// Other things
builder.setColor(Color.YELLOW);
Expand All @@ -332,7 +340,11 @@ private void createControlPanel(VoiceChannel voiceChannel, GuildMessageChannel t
private void sendControlPanel(GuildMessageChannel channel, VoiceDrone drone, EmbedBuilder builder, java.util.List<ActionRow> actionRows) {
// We already have a message, so we need to update it
if (drone.getControlPanel() != null) {
channel.editMessageEmbedsById(drone.getControlPanel(), builder.build()).setActionRows(actionRows).queue(q -> { /* It's ok! */}, e -> {
Member owner = channel.getGuild().getMemberById(drone.getOwnerId());
channel.editMessageById(drone.getControlPanel(), owner != null ? owner.getAsMention() : "Hello!")
.setEmbeds(builder.build())
.setActionRows(actionRows)
.queue(q -> { /* It's ok! */}, e -> {
LOGGER.error("Error updating control panel: {}", e.getMessage());
// If error, we need to create a new one
sendNewControlPanel(channel, drone, builder, actionRows);
Expand All @@ -343,7 +355,8 @@ private void sendControlPanel(GuildMessageChannel channel, VoiceDrone drone, Emb
}

private void sendNewControlPanel(GuildMessageChannel channel, VoiceDrone drone, EmbedBuilder builder, List<ActionRow> actionRows) {
channel.sendMessageEmbeds(builder.build()).setActionRows(actionRows).queue(q -> {
Member owner = channel.getGuild().getMemberById(drone.getOwnerId());
channel.sendMessage(owner != null ? owner.getAsMention() : "Hello!").setEmbeds(builder.build()).setActionRows(actionRows).queue(q -> {
drone.setControlPanel(q.getIdLong());
try {
voiceDroneService.update(drone);
Expand Down Expand Up @@ -437,7 +450,7 @@ public void checkToRemoveClaimMessage(VoiceChannel joined, Member member) {

TextChannel text = joined.getGuild().getTextChannelById(drone.getChatId());

if (member.getIdLong() == drone.getOwnerId() && (drone.getClaimMessage() != 0L && drone.getClaimMessage() != null)) {
if (member.getIdLong() == drone.getOwnerId() && (drone.getClaimMessage() != null && drone.getClaimMessage() != 0L)) {

if (text != null) text.deleteMessageById(drone.getClaimMessage()).submit();
else joined.deleteMessageById(drone.getClaimMessage()).submit();
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/softawii/capivara/core/VoiceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.softawii.capivara.exceptions.ExistingDynamicCategoryException;
import com.softawii.capivara.exceptions.KeyNotFoundException;
import com.softawii.capivara.services.VoiceHiveService;
import com.softawii.capivara.utils.Utils;
import net.dv8tion.jda.api.entities.Category;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
Expand Down Expand Up @@ -32,6 +33,8 @@ public class VoiceManager {
public static final String configModal_fieldPlaying = "set-playing";
public static final String configModal_fieldStreaming = "set-streaming";

public static final String configModal_createText = "set-text";

public VoiceManager(VoiceHiveService voiceHiveService) {
this.voiceHiveService = voiceHiveService;
}
Expand All @@ -53,7 +56,7 @@ public VoiceHive setDynamicCategory(Category category) throws ExistingDynamicCat
long guildId = category.getGuild().getIdLong();

// Returns the hive
return voiceHiveService.create(new VoiceHive(categoryId, guildId, hive.getIdLong(), configModal_idle, configModal_playing, configModal_streaming));
return voiceHiveService.create(new VoiceHive(categoryId, guildId, hive.getIdLong(), configModal_idle, configModal_playing, configModal_streaming, false));
}

public void unsetDynamicCategory(Category category) throws KeyNotFoundException {
Expand Down Expand Up @@ -90,12 +93,14 @@ public Modal getConfigModal(Category category, String id) {
try {
VoiceHive voiceHive = voiceHiveService.find(category.getIdLong());

Modal.Builder builder = Modal.create(id + ":" + category.getIdLong(), category.getName().substring(0, category.getName().length() > 45 ? 44 : category.getName().length())).addActionRow(
Modal.Builder builder = Modal.create(id + ":" + category.getIdLong(), Utils.getProperString(category.getName(), Modal.MAX_TITLE_LENGTH)).addActionRow(
TextInput.create(configModal_fieldIdle, "Channel Name when Idle", TextInputStyle.SHORT).setValue(voiceHive.getIdle().isBlank() ? null : voiceHive.getIdle()).build()
).addActionRow(
TextInput.create(configModal_fieldPlaying, "Channel Name when Playing", TextInputStyle.SHORT).setValue(voiceHive.getPlaying().isBlank() ? null : voiceHive.getPlaying()).setRequired(false).build()
).addActionRow(
TextInput.create(configModal_fieldStreaming, "Channel Name when Streaming", TextInputStyle.SHORT).setValue(voiceHive.getStreaming().isBlank() ? null : voiceHive.getStreaming()).setRequired(false).build()
).addActionRow(
TextInput.create(configModal_createText, "Create Text Channel", TextInputStyle.SHORT).setValue(voiceHive.getCreateTextChannel().toString()).setRequired(true).build()
);

return builder.build();
Expand All @@ -117,6 +122,8 @@ else if (mapping.getId().equals(configModal_fieldPlaying))
voiceHive.setPlaying(mapping.getAsString());
else if (mapping.getId().equals(configModal_fieldStreaming))
voiceHive.setStreaming(mapping.getAsString());
else if(mapping.getId().equals(configModal_createText))
voiceHive.setCreateTextChannel(mapping.getAsString().equalsIgnoreCase("true"));
}
voiceHiveService.update(voiceHive);

Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/softawii/capivara/entity/VoiceHive.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,24 @@ public class VoiceHive {
@Column
private String streaming;

@Column(nullable = false, columnDefinition = "boolean default false")
private Boolean createTextChannel;

// region Constructors

public VoiceHive() {
}

public VoiceHive(Long categoryId, Long guildId, long voiceId, String idle, String playing, String streaming) {
public VoiceHive(Long categoryId, Long guildId, long voiceId, String idle, String playing, String streaming, Boolean createTextChannel) {
this.categoryId = categoryId;
this.guildId = guildId;
this.voiceId = voiceId;
this.createTextChannel = createTextChannel;

// Temporary names
this.idle = idle;
this.playing = playing;
this.streaming = streaming;

}

// endregion
Expand Down Expand Up @@ -124,5 +127,13 @@ public void setStreaming(String streaming) {
this.streaming = streaming;
}

public Boolean getCreateTextChannel() {
return createTextChannel;
}

public void setCreateTextChannel(Boolean createTextChannel) {
this.createTextChannel = createTextChannel;
}

// endregion
}
13 changes: 13 additions & 0 deletions src/main/java/com/softawii/capivara/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,17 @@ public static MessageEmbed parseObjectToJsonError() {
"Poderia tentar novamente?", Color.ORANGE);
}

/**
* @param text Text to be parsed
* @param length Max length of the text to be returned
*
* @return Text with max length or less
*/
public static String getProperString(String text, int length) {
if (text.length() >= length) {
return text.substring(0, length - 3) + "...";
}
return text;
}

}

0 comments on commit 913f7a9

Please sign in to comment.