Skip to content

Commit

Permalink
Added Rate Command
Browse files Browse the repository at this point in the history
Additionally, modified some other files to fix small bugs.
  • Loading branch information
beanbeanjuice committed Jun 30, 2024
1 parent 733fc35 commit edbdd65
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/beanbeanjuice/cafebot/CafeBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.beanbeanjuice.cafebot.commands.fun.birthday.BirthdayCommand;
import com.beanbeanjuice.cafebot.commands.fun.counting.CountingCommand;
import com.beanbeanjuice.cafebot.commands.fun.meme.MemeCommand;
import com.beanbeanjuice.cafebot.commands.fun.rate.RateCommand;
import com.beanbeanjuice.cafebot.commands.generic.PingCommand;
import com.beanbeanjuice.cafebot.commands.generic.*;
import com.beanbeanjuice.cafebot.utility.commands.CommandHandler;
Expand Down Expand Up @@ -171,7 +172,8 @@ private void setupCommands() {
new BirthdayCommand(this),
new MemeCommand(this),
new JokeCommand(this),
new CountingCommand(this)
new CountingCommand(this),
new RateCommand(this)

// new EmbedCommand(this)
);
Expand Down Expand Up @@ -273,6 +275,7 @@ public int getTotalServers() {
return this.JDA.getGuilds().size();
}

// TODO: This is only getting cached users.
public int getTotalUsers() {
return this.JDA.getUsers().size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void handleServe(final SlashCommandInteractionEvent event, final CafeUse
.exceptionallyAsync((e) -> {
event.getHook().sendMessageEmbeds(Helper.errorEmbed(
"Error Serving",
"There was an error serving: " + e.getMessage()
String.format("There was an error serving %s. Is it a real word?", word)
)).queue();
return null;
});
Expand Down Expand Up @@ -119,7 +119,7 @@ public String getName() {

@Override
public String getDescription() {
return "Serve some words to customers to earn some **bC** (beanCoins)!";
return "Serve some words to customers to earn some bC (beanCoins)!";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private int getLeaderboardPlace(final HashMap<String, CountingInformation> leade
.toList();

for (int i = 0; i < lastNumberArray.size(); i++) {
if (lastNumberArray.get(i).getValue() < information.getLastNumber()) return i;
if (lastNumberArray.get(i).getValue() < information.getLastNumber()) return i + 1;
}

return lastNumberArray.size();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.beanbeanjuice.cafebot.commands.fun.rate;

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.User;
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 RateCaffeinatedSubCommand extends Command implements ISubCommand {

public RateCaffeinatedSubCommand(CafeBot cafeBot) {
super(cafeBot);
}

@Override
public void handle(SlashCommandInteractionEvent event) {
Optional<OptionMapping> userMapping = Optional.ofNullable(event.getOption("user"));

User user = userMapping.map(OptionMapping::getAsUser).orElse(event.getUser());
int percentage = Helper.getRandomInteger(0, 101);

event.getHook().sendMessageEmbeds(Helper.successEmbed(
"☕ Caffeine Rating ☕",
String.format("%s is %d%% caffeinated! %s", user.getAsMention(), percentage, "<a:yayaya:954426079177744445>")
)).mention(user).queue();
}

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

@Override
public String getDescription() {
return "Rate how caffeinated you or someone is!";
}

@Override
public OptionData[] getOptions() {
return new OptionData[] {
new OptionData(OptionType.USER, "user", "The person who's caffeine levels you want to see.")
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.beanbeanjuice.cafebot.commands.fun.rate;

import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.utility.commands.Command;
import com.beanbeanjuice.cafebot.utility.commands.ICommand;
import com.beanbeanjuice.cafebot.utility.commands.ISubCommand;
import net.dv8tion.jda.api.Permission;

public class RateCommand extends Command implements ICommand {

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

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

@Override
public String getDescription() {
return "Rate something!";
}

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

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

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

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

@Override
public ISubCommand[] getSubCommands() {
return new ISubCommand[] {
new RateCaffeinatedSubCommand(cafeBot)
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

public class DefineCommand extends Command implements ICommand {

// TODO: Check if other languages actually work.
public DefineCommand(CafeBot cafeBot) {
super(cafeBot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public CountingListener(final CafeBot cafeBot) {

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.beanbeanjuice.cafeapi.wrapper.endpoints.cafe.CafeUsersEndpoint;
import com.beanbeanjuice.cafebot.CafeBot;
import com.beanbeanjuice.cafebot.utility.helper.Helper;
import com.beanbeanjuice.cafebot.utility.logging.LogLevel;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.IMentionable;
import net.dv8tion.jda.api.entities.MessageEmbed;
Expand All @@ -17,6 +18,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class MenuListener extends ListenerAdapter {

Expand All @@ -37,6 +39,7 @@ public void onEntitySelectInteraction(EntitySelectInteractionEvent event) {
if (event.getComponentId().startsWith("menu-entity:")) handlePurchase(event);
}

// TODO: Component sometimes disappears.
private void handleCategory(final StringSelectInteractionEvent event) {
List<String> values = event.getValues(); // the values the user selected
String value = values.getFirst();
Expand All @@ -45,11 +48,14 @@ private void handleCategory(final StringSelectInteractionEvent event) {
boolean isHome = value.equalsIgnoreCase("all");
MessageEmbed menuEmbed = isHome ? menuHandler.getAllMenuEmbed() : menuHandler.getCategoryMenuEmbed(CafeCategory.valueOf(value));

// TODO: Sometimes replaces after adding the components.
event.editMessageEmbeds(menuEmbed).setReplace(true).queue((hook) -> {
List<ActionRow> rows = new ArrayList<>();
rows.add(ActionRow.of(menuHandler.getAllStringSelectMenu()));
if (!isHome) rows.add(ActionRow.of(menuHandler.getItemStringSelectMenu(CafeCategory.valueOf(value))));
hook.editOriginalComponents(rows).queue();
hook.editOriginalComponents(rows).delay(1, TimeUnit.SECONDS).queue((ignored) -> { }, (e) -> { // TODO: Check this.
cafeBot.getLogger().log(MenuListener.class, LogLevel.ERROR, "Error Adding Components: " + e, e);
});
});
}

Expand All @@ -65,7 +71,9 @@ private void handleItem(final StringSelectInteractionEvent event) {
List<ActionRow> rows = new ArrayList<>();
rows.add(ActionRow.of(menuHandler.getAllStringSelectMenu()));
rows.add(ActionRow.of(menuHandler.getItemEntitySelectMenu(itemString)));
hook.editOriginalComponents(rows).queue();
hook.editOriginalComponents(rows).delay(1, TimeUnit.SECONDS).queue((ignored) -> { }, (e) -> { // TODO: Check this.
cafeBot.getLogger().log(MenuListener.class, LogLevel.ERROR, "Error Adding Components: " + e, e);
});
});
});
}
Expand Down

0 comments on commit edbdd65

Please sign in to comment.