Skip to content

Commit

Permalink
Added Update Checker and Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
beanbeanjuice committed Jul 16, 2024
1 parent 1695cf9 commit 2b3f5e6
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.beanbeanjuice"
version = "v4.0.0"
version = "4.0.0"

allprojects {
group = "com.beanbeanjuice"
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/beanbeanjuice/cafebot/CafeBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
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.update.UpdateCommand;
import com.beanbeanjuice.cafebot.commands.settings.welcome.WelcomeCommand;
import com.beanbeanjuice.cafebot.commands.social.MemberCountCommand;
import com.beanbeanjuice.cafebot.commands.social.vent.VentCommand;
import com.beanbeanjuice.cafebot.commands.twitch.TwitchCommand;
import com.beanbeanjuice.cafebot.utility.api.GitHubVersionEndpointWrapper;
import com.beanbeanjuice.cafebot.utility.commands.CommandHandler;
import com.beanbeanjuice.cafebot.utility.helper.Helper;
import com.beanbeanjuice.cafebot.utility.helper.UpdateCheckHelper;
import com.beanbeanjuice.cafebot.utility.listeners.*;
import com.beanbeanjuice.cafebot.utility.logging.LogLevel;
import com.beanbeanjuice.cafebot.utility.logging.LogManager;
Expand Down Expand Up @@ -254,14 +257,18 @@ private void setupCommands() {
// Settings
new AICommand(this),
new WelcomeCommand(this),
new GoodbyeCommand(this)
new GoodbyeCommand(this),
new UpdateCommand(this)

// new EmbedCommand(this)
);

this.JDA.addEventListener(commandHandler);
this.helpHandler = new HelpHandler(commandHandler);
this.twitchHandler = new TwitchHandler(System.getenv("CAFEBOT_TWITCH_ACCESS_TOKEN"), this);

UpdateCheckHelper updateCheckHelper = new UpdateCheckHelper(this);
updateCheckHelper.checkUpdate();
}

public void addEventListener(final ListenerAdapter listener) {
Expand Down Expand Up @@ -289,7 +296,7 @@ private String getVersion() {
try (InputStream resourceStream = loader.getResourceAsStream(resourceName)) { props.load(resourceStream); }
catch (IOException e) { throw new RuntimeException(e); }

return props.get("version").toString();
return "v" + props.get("version").toString();
}

private void startUpdateTimer() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.beanbeanjuice.cafebot.commands.settings.update;

import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.commands.settings.update.channel.UpdateChannelRemoveSubCommand;
import com.beanbeanjuice.cafebot.commands.settings.update.channel.UpdateChannelSetSubCommand;
import com.beanbeanjuice.cafebot.utility.commands.*;
import net.dv8tion.jda.api.Permission;

public class UpdateCommand extends Command implements ICommand {

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

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

@Override
public String getDescription() {
return "Edit the bot update 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 ISubCommand[] getSubCommands() {
return new ISubCommand[] {
new UpdateStatusSubCommand(cafeBot)
};
}

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

return new SubCommandGroup[]{ channelGroup };
}

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

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;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;

public class UpdateStatusSubCommand extends Command implements ISubCommand {

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

@Override
public void handle(SlashCommandInteractionEvent event) {
boolean status = event.getOption("enabled").getAsBoolean();

cafeBot.getCafeAPI().getGuildsEndpoint().updateGuildInformation(event.getGuild().getId(), GuildInformationType.NOTIFY_ON_UPDATE, status)
.thenAcceptAsync((ignored) -> {
event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Bot Update Notification Status Changed",
"The bot update notification status has been successfully changed."
)).queue();
})
.exceptionallyAsync((e) -> {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Changing Bot Notification Status",
String.format("There was an error changing the bot notification status: %s", e.getMessage())
)).queue();
return null;
});
}

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

@Override
public String getDescription() {
return "Enable or disable bot update notifications.";
}

@Override
public OptionData[] getOptions() {
return new OptionData[] {
new OptionData(OptionType.BOOLEAN, "enabled", "Enable or disable bot update notifications.", true)
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.beanbeanjuice.cafebot.commands.settings.update.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 UpdateChannelRemoveSubCommand extends Command implements ISubCommand {

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

@Override
public void handle(SlashCommandInteractionEvent event) {
cafeBot.getCafeAPI().getGuildsEndpoint().updateGuildInformation(event.getGuild().getId(), GuildInformationType.UPDATE_CHANNEL_ID, "0")
.thenAcceptAsync((ignored) -> {
event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Update Channel Removed",
"The update channel has been successfully removed. Don't forget to disable update notifications too!"
)).queue();
})
.exceptionallyAsync((e) -> {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Removing Update Channel",
String.format("There was an error removing the update channel: %s", e.getMessage())
)).queue();
return null;
});
}

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

@Override
public String getDescription() {
return "Remove the update channel.";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.beanbeanjuice.cafebot.commands.settings.update.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 UpdateChannelSetSubCommand extends Command implements ISubCommand {

public UpdateChannelSetSubCommand(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.UPDATE_CHANNEL_ID, channel.getId())
.thenAcceptAsync((ignored) -> {
event.getHook().sendMessageEmbeds(Helper.successEmbed(
"Update Channel Set",
String.format("The update channel has been successfully set to %s. Don't forget to enable bot updates!", channel.getAsMention())
)).queue();
})
.exceptionallyAsync((e) -> {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Setting Update Channel",
String.format("There was an error setting the update channel: %s", e.getMessage())
)).queue();
return null;
});
}

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

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,27 @@ public CompletableFuture<MessageEmbed> getVersion(final String versionTag) {
}
}

private String parse(String responseBody) {
public CompletableFuture<String> getLatestVersionString() {
try (HttpClient client = HttpClient.newHttpClient()) {
HttpRequest request = HttpRequest.newBuilder().setHeader("User-Agent", cafeBot.getBotUserAgent()).uri(URI.create("https://api.github.com/repos/beanbeanjuice/cafeBot/tags")).build();

return client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(this::versionNumberFromResponse);
}
}

private String versionNumberFromResponse(final String responseBody) {
ObjectMapper defaultObjectMapper = new ObjectMapper();
try {
JsonNode node = defaultObjectMapper.readTree(responseBody);
return node.get(0).get("name").textValue();
} catch (JsonProcessingException e) {
throw new CompletionException(e);
}
}

private String parse(final String responseBody) {
ObjectMapper defaultObjectMapper = new ObjectMapper();
try {
JsonNode node = defaultObjectMapper.readTree(responseBody);
Expand Down
Loading

0 comments on commit 2b3f5e6

Please sign in to comment.