Skip to content

Commit

Permalink
Added the Goodbye Command
Browse files Browse the repository at this point in the history
Additionally, there is now a goodbye listener.
  • Loading branch information
beanbeanjuice committed Jul 16, 2024
1 parent 100d912 commit 1695cf9
Show file tree
Hide file tree
Showing 11 changed files with 427 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/main/java/com/beanbeanjuice/cafebot/CafeBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.beanbeanjuice.cafebot.commands.interaction.*;
import com.beanbeanjuice.cafebot.commands.moderation.ClearChatCommand;
import com.beanbeanjuice.cafebot.commands.settings.AICommand;
import com.beanbeanjuice.cafebot.commands.settings.goodbye.GoodbyeCommand;
import com.beanbeanjuice.cafebot.commands.settings.welcome.WelcomeCommand;
import com.beanbeanjuice.cafebot.commands.social.MemberCountCommand;
import com.beanbeanjuice.cafebot.commands.social.vent.VentCommand;
Expand Down Expand Up @@ -252,7 +253,8 @@ private void setupCommands() {

// Settings
new AICommand(this),
new WelcomeCommand(this)
new WelcomeCommand(this),
new GoodbyeCommand(this)

// new EmbedCommand(this)
);
Expand All @@ -275,7 +277,8 @@ private void setupListeners() {
new HelpListener(commandHandler, helpHandler),
new TicTacToeListener(cafeAPI.getWinStreaksEndpoint()),
aiResponseListener,
new WelcomeListener(this)
new WelcomeListener(this),
new GoodbyeListener(this)
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.beanbeanjuice.cafebot.commands.settings.goodbye;

import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.commands.settings.goodbye.channel.GoodbyeChannelRemoveSubCommand;
import com.beanbeanjuice.cafebot.commands.settings.goodbye.channel.GoodbyeChannelSetSubCommand;
import com.beanbeanjuice.cafebot.commands.settings.goodbye.message.GoodbyeMessageRemoveSubCommand;
import com.beanbeanjuice.cafebot.commands.settings.goodbye.message.GoodbyeMessageSetSubCommand;
import com.beanbeanjuice.cafebot.utility.commands.*;
import net.dv8tion.jda.api.Permission;

public class GoodbyeCommand extends Command implements ICommand {

public GoodbyeCommand(final CafeBot cafeBot) {
super(cafeBot);
}

@Override
public String getName() {
return "goodbye";
}

@Override
public String getDescription() {
return "Edit the goodbye settings!";
}

@Override
public CommandCategory getCategory() {
return CommandCategory.SETTINGS;
}

@Override
public Permission[] getPermissions() {
return new Permission[] {
Permission.MANAGE_SERVER
};
}

@Override
public boolean isEphemeral() {
return true;
}

@Override
public boolean isNSFW() {
return false;
}

@Override
public boolean allowDM() {
return false;
}

@Override
public SubCommandGroup[] getSubCommandGroups() {
SubCommandGroup channelGroup = new SubCommandGroup("channel", "Edit the goodbye channel.");
channelGroup.addSubCommands(new ISubCommand[] {
new GoodbyeChannelSetSubCommand(cafeBot),
new GoodbyeChannelRemoveSubCommand(cafeBot)
});

SubCommandGroup messageGroup = new SubCommandGroup("message", "Edit the goodbye message.");
messageGroup.addSubCommands(new ISubCommand[] {
new GoodbyeMessageSetSubCommand(cafeBot),
new GoodbyeMessageRemoveSubCommand(cafeBot)
});

return new SubCommandGroup[] { channelGroup, messageGroup };
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.beanbeanjuice.cafebot.commands.settings.goodbye.channel;

import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformationType;
import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.utility.commands.Command;
import com.beanbeanjuice.cafebot.utility.commands.ISubCommand;
import com.beanbeanjuice.cafebot.utility.helper.Helper;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;

public class GoodbyeChannelRemoveSubCommand extends Command implements ISubCommand {

public GoodbyeChannelRemoveSubCommand(final CafeBot cafeBot) {
super(cafeBot);
}

@Override
public void handle(SlashCommandInteractionEvent event) {
cafeBot.getCafeAPI().getGuildsEndpoint().updateGuildInformation(event.getGuild().getId(), GuildInformationType.GOODBYE_CHANNEL_ID, "0")
.thenAcceptAsync((ignored) -> {
event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Goodbye Channel Removed",
"The goodbye channel has been removed!"
)).queue();
})
.exceptionallyAsync((e) -> {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Removing Goodbye Channel",
String.format("There was an error removing the goodbye channel: %s", e.getMessage())
)).queue();
return null;
});
}

@Override
public String getName() {
return "remove";
}

@Override
public String getDescription() {
return "Remove the goodbye channel!";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.beanbeanjuice.cafebot.commands.settings.goodbye.channel;

import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformationType;
import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.utility.commands.Command;
import com.beanbeanjuice.cafebot.utility.commands.ISubCommand;
import com.beanbeanjuice.cafebot.utility.helper.Helper;
import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;

import java.util.Optional;

public class GoodbyeChannelSetSubCommand extends Command implements ISubCommand {

public GoodbyeChannelSetSubCommand(final CafeBot cafeBot) {
super(cafeBot);
}

@Override
public void handle(SlashCommandInteractionEvent event) {
Optional<OptionMapping> channelMapping = Optional.ofNullable(event.getOption("channel"));
GuildChannelUnion channel = channelMapping.map(OptionMapping::getAsChannel).orElse((GuildChannelUnion) event.getChannel());

cafeBot.getCafeAPI().getGuildsEndpoint().updateGuildInformation(event.getGuild().getId(), GuildInformationType.GOODBYE_CHANNEL_ID, channel.getId())
.thenAcceptAsync((ignored) -> {
event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Goodbye Channel Set",
String.format("The goodbye channel has been set to %s. Make sure you set the goodbye message!", channel.getAsMention())
)).queue();
})
.exceptionallyAsync((e) -> {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Setting Goodbye Channel",
String.format("There was an error setting the goodbye channel: %s", e.getMessage())
)).queue();
return null;
});
}

@Override
public String getName() {
return "set";
}

@Override
public String getDescription() {
return "Set the goodbye channel!";
}

@Override
public OptionData[] getOptions() {
return new OptionData[] {
new OptionData(OptionType.CHANNEL, "channel", "The channel to set the goodbye channel to.", false)
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.beanbeanjuice.cafebot.commands.settings.goodbye.message;

import com.beanbeanjuice.cafeapi.wrapper.endpoints.goodbyes.GoodbyesEndpoint;
import com.beanbeanjuice.cafeapi.wrapper.endpoints.goodbyes.GuildGoodbye;
import com.beanbeanjuice.cafebot.utility.helper.Helper;
import com.beanbeanjuice.cafebot.utility.listeners.GoodbyeListener;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.util.Map;

public class GoodbyeMessageModalListener extends ListenerAdapter {

private final GoodbyesEndpoint goodbyesEndpoint;

public GoodbyeMessageModalListener(final GoodbyesEndpoint goodbyesEndpoint) {
this.goodbyesEndpoint = goodbyesEndpoint;
}

@Override
public void onModalInteraction(ModalInteractionEvent event) {
if (!event.getModalId().startsWith("cafeBot:modal:goodbye:message:")) return;

Map<String, String> values = Helper.modalValuesToMap(event.getValues());

GuildGoodbye guildGoodbye = new GuildGoodbye(
event.getGuild().getId(),
values.get("description"),
values.get("thumbnail-url"),
values.get("image-url"),
values.get("message")
);

goodbyesEndpoint.updateGuildGoodbye(guildGoodbye)
.exceptionallyComposeAsync((e) -> goodbyesEndpoint.createGuildGoodbye(guildGoodbye))
.thenAcceptAsync((ignored) -> {
guildGoodbye.getMessage().ifPresentOrElse(
(message) -> event.reply(message.replace("{user}", event.getUser().getAsMention())).addEmbeds(GoodbyeListener.getGoodbyeEmbed(guildGoodbye, event.getUser())).setEphemeral(true).queue(),
() -> event.replyEmbeds(GoodbyeListener.getGoodbyeEmbed(guildGoodbye, event.getUser())).setEphemeral(true).queue()
);
})
.exceptionallyAsync((e) -> {
event.replyEmbeds(Helper.errorEmbed(
"Error Setting Goodbye Message",
String.format("There was an error setting the goodbye message: %s", e.getMessage())
)).queue();
return null;
});

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.beanbeanjuice.cafebot.commands.settings.goodbye.message;

import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.utility.commands.Command;
import com.beanbeanjuice.cafebot.utility.commands.ISubCommand;
import com.beanbeanjuice.cafebot.utility.helper.Helper;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;

public class GoodbyeMessageRemoveSubCommand extends Command implements ISubCommand {

public GoodbyeMessageRemoveSubCommand(final CafeBot cafeBot) {
super(cafeBot);
}

@Override
public void handle(SlashCommandInteractionEvent event) {
cafeBot.getCafeAPI().getGoodbyesEndpoint().deleteGuildGoodbye(event.getGuild().getId())
.thenAcceptAsync((ignored) -> {
event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Goodbye Message Removed",
"The goodbye message has been successfully removed."
)).queue();
})
.exceptionallyAsync((e) -> {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Removing Goodbye Message",
String.format("There was an error removing the goodbye message: %s", e.getMessage())
)).queue();
return null;
});
}

@Override
public String getName() {
return "remove";
}

@Override
public String getDescription() {
return "Remove the goodbye message!";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.beanbeanjuice.cafebot.commands.settings.goodbye.message;

import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.utility.commands.Command;
import com.beanbeanjuice.cafebot.utility.commands.ISubCommand;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.text.TextInput;
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
import net.dv8tion.jda.api.interactions.modals.Modal;

public class GoodbyeMessageSetSubCommand extends Command implements ISubCommand {

public GoodbyeMessageSetSubCommand(final CafeBot cafeBot) {
super(cafeBot);

cafeBot.addEventListener(new GoodbyeMessageModalListener(cafeBot.getCafeAPI().getGoodbyesEndpoint()));
}

@Override
public void handle(SlashCommandInteractionEvent event) {
TextInput title = TextInput.create("title", "Title", TextInputStyle.SHORT)
.setPlaceholder("Goodbye... {user}!")
.setRequired(false)
.build();

TextInput description = TextInput.create("description", "Description", TextInputStyle.PARAGRAPH)
.setPlaceholder("{user} left... sad...")
.setRequired(false)
.build();

TextInput message = TextInput.create("message", "Message", TextInputStyle.PARAGRAPH)
.setPlaceholder("@SomeRole, {user} just left...")
.setRequired(false)
.build();

TextInput largeImage = TextInput.create("image-url", "Large Image", TextInputStyle.SHORT)
.setPlaceholder("https://some.image.url")
.setRequired(false)
.build();

TextInput smallImage = TextInput.create("thumbnail-url", "Small Image", TextInputStyle.SHORT)
.setPlaceholder("https://some.image.url")
.setRequired(false)
.build();

Modal modal = Modal.create("cafeBot:modal:goodbye:message:" + this.getName(), "Edit Goodbye Message Content")
.addComponents(
ActionRow.of(title),
ActionRow.of(description),
ActionRow.of(message),
ActionRow.of(largeImage),
ActionRow.of(smallImage)
)
.build();

event.replyModal(modal).queue();
}

@Override
public String getName() {
return "set";
}

@Override
public String getDescription() {
return "Set the goodbye message!";
}

@Override
public boolean isModal() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ public boolean allowDM() {
return false;
}

@Override
public ISubCommand[] getSubCommands() {
return ICommand.super.getSubCommands();
}

@Override
public SubCommandGroup[] getSubCommandGroups() {
SubCommandGroup channelGroup = new SubCommandGroup("channel", "Edit the welcome channel.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void handle(SlashCommandInteractionEvent event) {
.exceptionallyAsync((e) -> {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Removing Welcome Channel",
String.format("There was an error setting the welcome channel: %s", e.getMessage())
String.format("There was an error removing the welcome channel: %s", e.getMessage())
)).queue();
return null;
});
Expand Down
Loading

0 comments on commit 1695cf9

Please sign in to comment.