-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31d21dd
commit e5e4502
Showing
4 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/java/com/beanbeanjuice/cafebot/commands/fun/AICommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/com/beanbeanjuice/cafebot/utility/listeners/AIResponseListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
|
||
} |