Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #686 from Timmi6790/bugs/slash-fixes
Browse files Browse the repository at this point in the history
Slash command fixes
  • Loading branch information
Timmi6790 committed Nov 9, 2022
2 parents d880bcc + 00fc638 commit aa318b3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
public class ChannelDb {
private final long discordId;
private final GuildDb guildDb;
private boolean privateChannel = false;
private boolean disabled;

@EqualsAndHashCode.Exclude
Expand All @@ -39,12 +40,17 @@ public ChannelDb(final GuildDb guildDb,
this.discord = discord;
}

ChannelDb setPrivateChannel(final boolean privateChannel) {
this.privateChannel = privateChannel;
return this;
}

/**
* Gets the corresponding discord channel for this instance. This might be retrieved if not cached
*
* @return the discord channel
*/
public MessageChannel getChannel() {
return this.discord.getTextChannelById(this.discordId);
return this.privateChannel ? this.discord.getPrivateChannelById(this.discordId) : this.discord.getTextChannelById(this.discordId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public ChannelDb getOrCreate(final long discordChannelId, final long discordGuil
* @return the channel db instance
*/
public ChannelDb getOrCreatePrivateMessage(final long discordChannelId) {
return this.getOrCreate(discordChannelId, GuildDbModule.getPrivateMessageGuildId());
return this.getOrCreate(discordChannelId, GuildDbModule.getPrivateMessageGuildId())
.setPrivateChannel(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import de.timmi6790.discord_framework.module.modules.slashcommand.parameters.action.CommandRestAction;
import de.timmi6790.discord_framework.module.modules.slashcommand.parameters.action.FileRestAction;
import de.timmi6790.discord_framework.module.modules.slashcommand.parameters.action.UpdateRestAction;
import de.timmi6790.discord_framework.module.modules.slashcommand.parameters.options.DiscordOption;
import de.timmi6790.discord_framework.module.modules.user.UserDb;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
Expand All @@ -14,6 +15,7 @@

import java.io.InputStream;
import java.util.Collection;
import java.util.Map;

public class EventSlashCommandParameters extends SlashCommandParameters {
private final SlashCommandInteractionEvent event;
Expand All @@ -32,6 +34,12 @@ public EventSlashCommandParameters(final SlashCommandInteractionEvent event, fin
this.event = event;
}

private EventSlashCommandParameters(final EventSlashCommandParameters slashCommandParameters, final Map<String, DiscordOption> options) {
super(slashCommandParameters, options);

this.event = slashCommandParameters.event;
}

@Override
public boolean isGuildCommand() {
return this.event.isGuildCommand();
Expand All @@ -58,6 +66,11 @@ public CommandRestAction createFileAction(final InputStream stream, final String
return new FileRestAction(this.getHook().sendFiles(fileUpload));
}

@Override
public SlashCommandParameters clone(final Map<String, DiscordOption> newOptions) {
return new EventSlashCommandParameters(this, newOptions);
}

public InteractionHook getHook() {
return this.event.getHook();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import de.timmi6790.discord_framework.module.modules.user.UserDb;
import de.timmi6790.discord_framework.utilities.MultiEmbedBuilder;
import de.timmi6790.discord_framework.utilities.discord.DiscordMessagesUtilities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.SneakyThrows;
import net.dv8tion.jda.api.JDA;
Expand All @@ -27,6 +28,7 @@
import java.util.concurrent.TimeUnit;

@Data
@AllArgsConstructor
public abstract class SlashCommandParameters implements Cloneable {
public static Map<String, DiscordOption> formatEventOptions(final List<OptionMapping> options) {
final Map<String, DiscordOption> formatOptions = new HashMap<>(options.size());
Expand All @@ -52,6 +54,16 @@ public static Map<String, DiscordOption> formatOptions(final List<DiscordOption>
private final Map<String, DiscordOption> options;
private final String subCommandName;

protected SlashCommandParameters(final SlashCommandParameters slashCommandParameters, final Map<String, DiscordOption> options) {
this.jda = slashCommandParameters.jda;
this.commandCause = slashCommandParameters.commandCause;
this.commandModule = slashCommandParameters.commandModule;
this.channelDb = slashCommandParameters.channelDb;
this.userDb = slashCommandParameters.userDb;
this.options = options;
this.subCommandName = slashCommandParameters.subCommandName;
}

public GuildDb getGuildDb() {
return this.channelDb.getGuildDb();
}
Expand All @@ -70,6 +82,8 @@ public Member getGuildMember() {

public abstract CommandRestAction createFileAction(final InputStream stream, final String name);

public abstract SlashCommandParameters clone(final Map<String, DiscordOption> newOptions);

public CommandRestAction createMessageUpdateAction(final MultiEmbedBuilder builder) {
return this.createMessageUpdateAction(builder.build());
}
Expand Down Expand Up @@ -150,14 +164,4 @@ public void sendPrivateMessage(final MultiEmbedBuilder builder) {
builder
);
}

@SneakyThrows
public SlashCommandParameters clone(final Map<String, DiscordOption> newOptions) {
final SlashCommandParameters commandParameters = (SlashCommandParameters) super.clone();

commandParameters.options.clear();
commandParameters.options.putAll(newOptions);

return commandParameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public StoredSlashCommandParameters(final JDA jda, final CommandCause commandCau
this.guildCommand = guildCommand;
}

private StoredSlashCommandParameters(final StoredSlashCommandParameters slashCommandParameters, final Map<String, DiscordOption> options) {
super(slashCommandParameters, options);

this.guildCommand = slashCommandParameters.guildCommand;
}

@Override
public boolean isGuildCommand() {
return this.guildCommand;
Expand All @@ -60,4 +66,9 @@ public CommandRestAction createFileAction(final InputStream stream, final String
final FileUpload fileUpload = FileUpload.fromData(stream, name);
return new CreateRestAction(this.getChannelDb().getChannel().sendFiles(fileUpload));
}

@Override
public SlashCommandParameters clone(final Map<String, DiscordOption> newOptions) {
return new StoredSlashCommandParameters(this, newOptions);
}
}

0 comments on commit aa318b3

Please sign in to comment.