Skip to content

Commit

Permalink
Added the "AI" Listener
Browse files Browse the repository at this point in the history
  • Loading branch information
beanbeanjuice committed Jul 13, 2024
1 parent 31d21dd commit e5e4502
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/java/com/beanbeanjuice/cafebot/CafeBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.beanbeanjuice.cafebot.commands.twitch.TwitchCommand;
import com.beanbeanjuice.cafebot.utility.commands.CommandHandler;
import com.beanbeanjuice.cafebot.utility.helper.Helper;
import com.beanbeanjuice.cafebot.utility.listeners.AIResponseListener;
import com.beanbeanjuice.cafebot.utility.listeners.BotAddListener;
import com.beanbeanjuice.cafebot.utility.listeners.BotRemoveListener;
import com.beanbeanjuice.cafebot.utility.listeners.CountingListener;
Expand Down Expand Up @@ -75,6 +76,9 @@ public class CafeBot {
// Internal Items
@Getter private final LogManager logger;

// Listeners
@Getter private AIResponseListener aiResponseListener;

// Handlers
private CommandHandler commandHandler;
@Getter private MenuHandler menuHandler;
Expand Down Expand Up @@ -182,6 +186,7 @@ private void setupCommands() {

// Fun
new AvatarCommand(this),
new AICommand(this),
new BannerCommand(this),
new BirthdayCommand(this),
new MemeCommand(this),
Expand Down Expand Up @@ -255,12 +260,14 @@ private void setupCommands() {
}

private void setupListeners() {
this.aiResponseListener = new AIResponseListener(this);
this.JDA.addEventListener(
new BotAddListener(this),
new BotRemoveListener(this),
new CountingListener(this),
new HelpListener(commandHandler, helpHandler),
new TicTacToeListener(cafeAPI.getWinStreaksEndpoint())
new TicTacToeListener(cafeAPI.getWinStreaksEndpoint()),
aiResponseListener
);
}

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

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.CommandCategory;
import com.beanbeanjuice.cafebot.utility.commands.ICommand;
import com.beanbeanjuice.cafebot.utility.helper.Helper;
import net.dv8tion.jda.api.Permission;
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 AICommand extends Command implements ICommand {

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

@Override
public void handle(SlashCommandInteractionEvent event) {
String guildID = event.getGuild().getId();
boolean status = event.getOption("status").getAsBoolean();

String statusString = (status) ? "Enabled ✅" : "Disabled ❌";

cafeBot.getCafeAPI().getGuildsEndpoint().updateGuildInformation(guildID, GuildInformationType.AI_RESPONSE, status)
.thenAcceptAsync((ignored) -> {
event.getHook().sendMessageEmbeds(Helper.successEmbed(
"AI Response Changed",
String.format("Status: %s", statusString)
)).queue();
})
.exceptionallyAsync((e) -> {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Changing AI Response",
String.format("There was an error changing the AI response: %s", e.getMessage())
)).queue();
return null;
});
}

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

@Override
public String getDescription() {
return "Enable or disable the \"AI\".";
}

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

@Override
public OptionData[] getOptions() {
return new OptionData[] {
new OptionData(OptionType.BOOLEAN, "status", "Whether to enable or disable the \"AI\".", true)
};
}

@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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public void handle(SlashCommandInteractionEvent event) {
return;
}

if (event.getUser().getId().equals("690927484199370753") && word.equalsIgnoreCase("ai")) {
cafeBot.getAiResponseListener().refreshMaps();
event.getHook().sendMessage("Successfully refreshed the AI!").queue();
return;
}

event.getHook().sendMessage(word).queue();
});
numberOptionMapping.map(OptionMapping::getAsInt).ifPresent((number) -> event.getHook().sendMessage(String.valueOf(number)).queue());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.beanbeanjuice.cafebot.utility.listeners;

import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformationType;
import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.utility.helper.Helper;
import com.beanbeanjuice.cafebot.utility.logging.LogLevel;
import com.fasterxml.jackson.databind.JsonNode;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AIResponseListener extends ListenerAdapter {

private final CafeBot cafeBot;
private final HashMap<List<String>, List<String>> messageMap;

public AIResponseListener(final CafeBot cafeBot) {
this.cafeBot = cafeBot;
this.messageMap = new HashMap<>();

refreshMaps();
}

public void refreshMaps() {
this.messageMap.clear();

try {
createMaps();
} catch (IOException e) {
cafeBot.getLogger().log(AIResponseListener.class, LogLevel.ERROR, "Unable to refresh AI maps...", true, true, e);
}
}

private void createMaps() throws IOException {
for (JsonNode type : Helper.parseJson("ai.json")) {
List<String> triggers = new ArrayList<>();
List<String> responses = new ArrayList<>();

for (JsonNode trigger : type.get("triggers")) triggers.add(trigger.asText());
for (JsonNode response : type.get("responses")) responses.add(response.asText());

messageMap.put(triggers, responses);
}
}

@Override
public void onMessageReceived(final MessageReceivedEvent event) {
if (!event.isFromGuild()) return;
if (event.getAuthor().isBot()) return;

String guildID = event.getGuild().getId();

cafeBot.getCafeAPI().getGuildsEndpoint().getGuildInformation(guildID).thenAcceptAsync((information) -> {
boolean useAI = Boolean.parseBoolean(information.getSetting(GuildInformationType.AI_RESPONSE));
if (!useAI) return;

String message = event.getMessage().getContentRaw().replaceAll("[^\\sa-zA-Z0-9]", "");

messageMap.forEach((commandTerms, commandResponses) -> {
if (!commandTerms.contains(message)) return;

event.getMessage().reply(parseMessage(
commandResponses.get(Helper.getRandomInteger(0, commandResponses.size())),
event.getAuthor()
)).queue();
cafeBot.increaseCommandsRun();
});
});
}

private String parseMessage(final String message, final User user) {
return message.replace("{user}", user.getAsMention());
}


}

0 comments on commit e5e4502

Please sign in to comment.