use of net.kodehawa.mantarobot.commands.currency.profile.ProfileComponent in project MantaroBot by Mantaro.
the class ProfileCmd method profile.
@Subscribe
public void profile(CommandRegistry cr) {
final var rateLimiter = new IncreasingRateLimiter.Builder().limit(// twice every 10m
2).spamTolerance(2).cooldown(10, TimeUnit.MINUTES).cooldownPenaltyIncrease(10, TimeUnit.SECONDS).maxCooldown(15, TimeUnit.MINUTES).pool(MantaroData.getDefaultJedisPool()).prefix("profile").build();
final var config = MantaroData.config().get();
List<ProfileComponent> defaultOrder;
if (config.isPremiumBot() || config.isSelfHost()) {
defaultOrder = createLinkedList(HEADER, CREDITS, LEVEL, REPUTATION, BIRTHDAY, MARRIAGE, INVENTORY, BADGES, PET);
} else {
defaultOrder = createLinkedList(HEADER, CREDITS, OLD_CREDITS, LEVEL, REPUTATION, BIRTHDAY, MARRIAGE, INVENTORY, BADGES, PET);
}
List<ProfileComponent> noOldOrder = createLinkedList(HEADER, CREDITS, LEVEL, REPUTATION, BIRTHDAY, MARRIAGE, INVENTORY, BADGES, PET);
TreeCommand profileCommand = cr.register("profile", new TreeCommand(CommandCategory.CURRENCY) {
@Override
public Command defaultTrigger(Context ctx, String mainCommand, String commandName) {
return new SubCommand() {
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
var optionalArguments = ctx.getOptionalArguments();
content = Utils.replaceArguments(optionalArguments, content, "season", "s").trim();
var isSeasonal = ctx.isSeasonal();
var finalContent = content;
ctx.findMember(content, members -> {
SeasonPlayer seasonalPlayer = null;
var userLooked = ctx.getAuthor();
var memberLooked = ctx.getMember();
if (!finalContent.isEmpty()) {
var found = CustomFinderUtil.findMember(finalContent, members, ctx);
if (found == null) {
return;
}
userLooked = found.getUser();
memberLooked = found;
}
if (userLooked.isBot()) {
ctx.sendLocalized("commands.profile.bot_notice", EmoteReference.ERROR);
return;
}
var player = ctx.getPlayer(userLooked);
var dbUser = ctx.getDBUser(userLooked);
var playerData = player.getData();
var userData = dbUser.getData();
var inv = player.getInventory();
// Cache waifu value.
playerData.setWaifuCachedValue(WaifuCmd.calculateWaifuValue(player, userLooked).getFinalValue());
// start of badge assigning
var mh = MantaroBot.getInstance().getShardManager().getGuildById("213468583252983809");
var mhMember = mh == null ? null : ctx.retrieveMemberById(memberLooked.getUser().getId(), false);
Badge.assignBadges(player, player.getStats(), dbUser);
var christmasBadgeAssign = inv.asList().stream().map(ItemStack::getItem).anyMatch(it -> it.equals(ItemReference.CHRISTMAS_TREE_SPECIAL) || it.equals(ItemReference.BELL_SPECIAL));
// Manual badges
if (config.isOwner(userLooked)) {
playerData.addBadgeIfAbsent(Badge.DEVELOPER);
}
if (christmasBadgeAssign) {
playerData.addBadgeIfAbsent(Badge.CHRISTMAS);
}
// Requires a valid Member in Mantaro Hub.
if (mhMember != null) {
// Admin
if (containsRole(mhMember, 315910951994130432L, 642089477828902912L)) {
playerData.addBadgeIfAbsent(Badge.COMMUNITY_ADMIN);
}
// Patron - Donator
if (containsRole(mhMember, 290902183300431872L, 290257037072531466L)) {
playerData.addBadgeIfAbsent(Badge.DONATOR_2);
}
// Translator
if (containsRole(mhMember, 407156441812828162L)) {
playerData.addBadgeIfAbsent(Badge.TRANSLATOR);
}
}
// end of badge assigning
var badges = playerData.getBadges();
Collections.sort(badges);
if (isSeasonal) {
seasonalPlayer = ctx.getSeasonPlayer(userLooked);
}
var marriage = ctx.getMarriage(userData);
var ringHolder = player.getInventory().containsItem(ItemReference.RING) && marriage != null;
var holder = new ProfileComponent.Holder(userLooked, player, seasonalPlayer, dbUser, marriage, badges);
var profileBuilder = new EmbedBuilder();
var description = languageContext.get("commands.profile.no_desc");
if (playerData.getDescription() != null) {
description = player.getData().getDescription();
}
profileBuilder.setAuthor((ringHolder ? EmoteReference.RING : "") + String.format(languageContext.get("commands.profile.header"), memberLooked.getEffectiveName()), null, userLooked.getEffectiveAvatarUrl()).setDescription(description).setThumbnail(userLooked.getEffectiveAvatarUrl()).setColor(ctx.getMemberColor(memberLooked)).setFooter(ProfileComponent.FOOTER.getContent().apply(holder, languageContext), ctx.getAuthor().getEffectiveAvatarUrl());
var hasCustomOrder = dbUser.isPremium() && !playerData.getProfileComponents().isEmpty();
var usedOrder = hasCustomOrder ? playerData.getProfileComponents() : defaultOrder;
if ((!config.isPremiumBot() && player.getOldMoney() < 5000 && !hasCustomOrder) || (playerData.isHiddenLegacy() && !hasCustomOrder)) {
usedOrder = noOldOrder;
}
for (var component : usedOrder) {
profileBuilder.addField(component.getTitle(languageContext), component.getContent().apply(holder, languageContext), component.isInline());
}
ctx.send(profileBuilder.build());
// We don't need to update stats if someone else views your profile
if (player.getUserId().equals(ctx.getAuthor().getId())) {
player.saveUpdating();
}
});
}
};
}
// If you wonder why is this so short compared to before, subcommand descriptions will do the trick on telling me what they do.
@Override
public HelpContent help() {
return new HelpContent.Builder().setDescription("Retrieves your current user profile.").setUsage("To retrieve your profile use `~>profile`. You can also use `~>profile @mention`\n" + "*The profile command only shows the 5 most important badges.* Use `~>badges` to get a complete list!").addParameter("@mention", "A user mention (ping)").setSeasonal(true).build();
}
});
profileCommand.setPredicate(ctx -> {
if (!ctx.getSelfMember().hasPermission(ctx.getChannel(), Permission.MESSAGE_EMBED_LINKS)) {
ctx.sendLocalized("general.missing_embed_permissions");
return false;
}
return true;
});
profileCommand.addSubCommand("toggleaction", new SubCommand() {
@Override
public String description() {
return "Toggles the ability to do action commands to you.";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
final var dbUser = ctx.getDBUser();
final var userData = dbUser.getData();
final var isDisabled = userData.isActionsDisabled();
if (isDisabled) {
userData.setActionsDisabled(false);
ctx.sendLocalized("commands.profile.toggleaction.enabled", EmoteReference.CORRECT);
} else {
userData.setActionsDisabled(true);
ctx.sendLocalized("commands.profile.toggleaction.disabled", EmoteReference.CORRECT);
}
dbUser.save();
}
});
profileCommand.addSubCommand("claimlock", new SubCommand() {
@Override
public String description() {
return "Locks you from being claimed. Use `remove` to remove it.";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
final var player = ctx.getPlayer();
final var playerData = player.getData();
if (content.equals("remove")) {
playerData.setClaimLocked(false);
ctx.sendLocalized("commands.profile.claimlock.removed", EmoteReference.CORRECT);
player.saveUpdating();
return;
}
if (playerData.isClaimLocked()) {
ctx.sendLocalized("commands.profile.claimlock.already_locked", EmoteReference.CORRECT);
return;
}
var inventory = player.getInventory();
if (!inventory.containsItem(ItemReference.CLAIM_KEY)) {
ctx.sendLocalized("commands.profile.claimlock.no_key", EmoteReference.ERROR);
return;
}
playerData.setClaimLocked(true);
ctx.sendLocalized("commands.profile.claimlock.success", EmoteReference.CORRECT);
inventory.process(new ItemStack(ItemReference.CLAIM_KEY, -1));
player.saveUpdating();
}
});
if (!config.isPremiumBot()) {
profileCommand.addSubCommand("togglelegacy", new SubCommand() {
@Override
public String description() {
return "Toggles legacy credit display.";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
final var player = ctx.getPlayer();
final var data = player.getData();
var toSet = !data.isHiddenLegacy();
data.setHiddenLegacy(toSet);
player.saveUpdating();
ctx.sendLocalized("commands.profile.hidelegacy", EmoteReference.CORRECT, data.isHiddenLegacy());
}
});
}
profileCommand.addSubCommand("inventorysort", new SubCommand() {
@Override
public String description() {
return "Sort your inventory. Possible values: `VALUE, VALUE_TOTAL, AMOUNT, TYPE, RANDOM`.";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
final var type = Utils.lookupEnumString(content, InventorySortType.class);
if (type == null) {
ctx.sendLocalized("commands.profile.inventorysort.not_valid", EmoteReference.ERROR, Arrays.stream(InventorySortType.values()).map(b1 -> b1.toString().toLowerCase()).collect(Collectors.joining(", ")));
return;
}
final var player = ctx.getPlayer();
final var playerData = player.getData();
playerData.setInventorySortType(type);
player.saveUpdating();
ctx.sendLocalized("commands.profile.inventorysort.success", EmoteReference.CORRECT, type.toString().toLowerCase());
}
});
profileCommand.addSubCommand("autoequip", new SubCommand() {
@Override
public String description() {
return "Toggles auto-equipping a new tool on break. Use `disable` to disable it.";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
var user = ctx.getDBUser();
var data = user.getData();
if (content.equals("disable")) {
data.setAutoEquip(false);
ctx.sendLocalized("commands.profile.autoequip.disable", EmoteReference.CORRECT);
user.saveUpdating();
return;
}
data.setAutoEquip(true);
ctx.sendLocalized("commands.profile.autoequip.success", EmoteReference.CORRECT);
user.saveUpdating();
}
});
// Hide tags from profile/waifu list.
profileCommand.addSubCommand("hidetag", new SubCommand() {
@Override
public String description() {
return "Hide or show the member id/tag from profile/waifu ls.";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
var user = ctx.getDBUser();
var data = user.getData();
data.setPrivateTag(!data.isPrivateTag());
user.saveUpdating();
ctx.sendLocalized("commands.profile.hide_tag.success", EmoteReference.POPPER, data.isPrivateTag());
}
});
profileCommand.addSubCommand("timezone", new SubCommand() {
@Override
public String description() {
return "Set your profile timezone.";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
var dbUser = ctx.getDBUser();
var args = ctx.getArguments();
if (args.length < 1) {
ctx.sendLocalized("commands.profile.timezone.not_specified", EmoteReference.ERROR);
return;
}
var timezone = content;
if (offsetRegex.matcher(timezone).matches()) {
timezone = content.toUpperCase().replace("UTC", "GMT");
}
// EST, EDT, etc...
if (timezone.length() == 3) {
timezone = timezone.toUpperCase();
}
if (timezone.equalsIgnoreCase("reset")) {
dbUser.getData().setTimezone(null);
dbUser.saveAsync();
ctx.sendLocalized("commands.profile.timezone.reset_success", EmoteReference.CORRECT);
return;
}
if (!Utils.isValidTimeZone(timezone)) {
ctx.sendLocalized("commands.profile.timezone.invalid", EmoteReference.ERROR);
return;
}
try {
Utils.formatDate(LocalDateTime.now(Utils.timezoneToZoneID(timezone)), dbUser.getData().getLang());
} catch (DateTimeException e) {
ctx.sendLocalized("commands.profile.timezone.invalid", EmoteReference.ERROR);
return;
}
var player = ctx.getPlayer();
if (player.getData().addBadgeIfAbsent(Badge.CALENDAR)) {
player.saveUpdating();
}
dbUser.getData().setTimezone(timezone);
dbUser.saveUpdating();
ctx.sendLocalized("commands.profile.timezone.success", EmoteReference.CORRECT, timezone);
}
});
profileCommand.addSubCommand("description", new SubCommand() {
@Override
public String description() {
return "Set your profile description. Use `reset` to reset it.";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
if (!RatelimitUtils.ratelimit(rateLimiter, ctx)) {
return;
}
var args = ctx.getArguments();
var player = ctx.getPlayer();
var dbUser = ctx.getDBUser();
if (args.length == 0) {
ctx.sendLocalized("commands.profile.description.no_argument", EmoteReference.ERROR);
return;
}
if (args[0].equals("clear") || args[0].equals("remove") || args[0].equals("reset")) {
player.getData().setDescription(null);
ctx.sendLocalized("commands.profile.description.clear_success", EmoteReference.CORRECT);
player.saveUpdating();
return;
}
var split = SPLIT_PATTERN.split(content, 2);
var desc = content;
var old = false;
if (split[0].equals("set")) {
desc = content.replaceFirst("set ", "");
old = true;
}
var MAX_LENGTH = 300;
if (dbUser.isPremium()) {
MAX_LENGTH = 500;
}
if (args.length < (old ? 2 : 1)) {
ctx.sendLocalized("commands.profile.description.no_content", EmoteReference.ERROR);
return;
}
if (desc.length() > MAX_LENGTH) {
ctx.sendLocalized("commands.profile.description.too_long", EmoteReference.ERROR);
return;
}
desc = Utils.DISCORD_INVITE.matcher(desc).replaceAll("-discord invite link-");
desc = Utils.DISCORD_INVITE_2.matcher(desc).replaceAll("-discord invite link-");
player.getData().setDescription(desc);
ctx.sendStrippedLocalized("commands.profile.description.success", EmoteReference.POPPER);
player.getData().addBadgeIfAbsent(Badge.WRITER);
player.saveUpdating();
}
});
profileCommand.addSubCommand("displaybadge", new SubCommand() {
@Override
public String description() {
return "Set your profile badge. Use `reset` to reset and `none` to show no badge.";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
var args = ctx.getArguments();
if (args.length == 0) {
ctx.sendLocalized("commands.profile.displaybadge.not_specified", EmoteReference.ERROR);
return;
}
var player = ctx.getPlayer();
var data = player.getData();
var arg = args[0];
if (arg.equalsIgnoreCase("none")) {
data.setShowBadge(false);
ctx.sendLocalized("commands.profile.displaybadge.reset_success", EmoteReference.CORRECT);
player.saveUpdating();
return;
}
if (arg.equalsIgnoreCase("reset")) {
data.setMainBadge(null);
data.setShowBadge(true);
ctx.sendLocalized("commands.profile.displaybadge.important_success", EmoteReference.CORRECT);
player.saveUpdating();
return;
}
var badge = Badge.lookupFromString(content);
if (badge == null) {
ctx.sendLocalized("commands.profile.displaybadge.no_such_badge", EmoteReference.ERROR, player.getData().getBadges().stream().map(Badge::getDisplay).collect(Collectors.joining(", ")));
return;
}
if (!data.getBadges().contains(badge)) {
ctx.sendLocalized("commands.profile.displaybadge.player_missing_badge", EmoteReference.ERROR, player.getData().getBadges().stream().map(Badge::getDisplay).collect(Collectors.joining(", ")));
return;
}
data.setShowBadge(true);
data.setMainBadge(badge);
player.saveUpdating();
ctx.sendLocalized("commands.profile.displaybadge.success", EmoteReference.CORRECT, badge.display);
}
});
profileCommand.addSubCommand("language", new SubCommand() {
@Override
public String description() {
return "Set your profile language. Available langs: `~>lang`";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
if (content.isEmpty()) {
ctx.sendLocalized("commands.profile.lang.nothing_specified", EmoteReference.ERROR);
return;
}
var dbUser = ctx.getDBUser();
if (content.equalsIgnoreCase("reset")) {
dbUser.getData().setLang(null);
dbUser.saveUpdating();
ctx.sendLocalized("commands.profile.lang.reset_success", EmoteReference.CORRECT);
return;
}
if (I18n.isValidLanguage(content)) {
dbUser.getData().setLang(content);
// Create new I18n context based on the new language choice.
var newContext = new I18nContext(ctx.getDBGuild().getData(), dbUser.getData());
dbUser.saveUpdating();
ctx.getChannel().sendMessageFormat(newContext.get("commands.profile.lang.success"), EmoteReference.CORRECT, content).queue();
} else {
ctx.sendLocalized("commands.profile.lang.invalid", EmoteReference.ERROR);
}
}
}).createSubCommandAlias("language", "lang");
profileCommand.addSubCommand("stats", new SubCommand() {
@Override
public String description() {
return "Check profile statistics.";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
ctx.findMember(content, members -> {
var member = CustomFinderUtil.findMemberDefault(content, members, ctx, ctx.getMember());
if (member == null) {
return;
}
var toLookup = member.getUser();
if (toLookup.isBot()) {
ctx.sendLocalized("commands.profile.bot_notice", EmoteReference.ERROR);
return;
}
var player = ctx.getPlayer(toLookup);
var dbUser = ctx.getDBUser(toLookup);
List<MessageEmbed.Field> fields = new LinkedList<>();
for (StatsComponent component : StatsComponent.values()) {
fields.add(new MessageEmbed.Field(component.getEmoji() + component.getName(ctx), component.getContent(new StatsComponent.Holder(ctx, languageContext, player, dbUser, toLookup)), false));
}
var splitFields = DiscordUtils.divideFields(9, fields);
var embed = new EmbedBuilder().setThumbnail(toLookup.getEffectiveAvatarUrl()).setAuthor(languageContext.get("commands.profile.stats.header").formatted(toLookup.getName()), null, toLookup.getEffectiveAvatarUrl()).setDescription(String.format(languageContext.get("general.buy_sell_paged_react"), String.format(languageContext.get("general.reaction_timeout"), 200))).setColor(ctx.getMemberColor()).setFooter("Thanks for using Mantaro! %s".formatted(EmoteReference.HEART), ctx.getGuild().getIconUrl());
DiscordUtils.listButtons(ctx, 200, embed, splitFields);
});
}
});
profileCommand.addSubCommand("widgets", new SubCommand() {
@Override
public String description() {
return "Set profile widgets and order. Arguments: `widget`, `ls` or `reset`";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
var user = ctx.getDBUser();
if (!user.isPremium()) {
ctx.sendLocalized("commands.profile.display.not_premium", EmoteReference.ERROR);
return;
}
var player = ctx.getPlayer();
var playerData = player.getData();
if (content.equalsIgnoreCase("ls") || content.equalsIgnoreCase("Is")) {
ctx.sendFormat(languageContext.get("commands.profile.display.ls") + languageContext.get("commands.profile.display.example"), EmoteReference.ZAP, EmoteReference.BLUE_SMALL_MARKER, defaultOrder.stream().map(Enum::name).collect(Collectors.joining(", ")), playerData.getProfileComponents().size() == 0 ? "Not personalized" : playerData.getProfileComponents().stream().map(Enum::name).collect(Collectors.joining(", ")));
return;
}
if (content.equalsIgnoreCase("reset")) {
playerData.getProfileComponents().clear();
player.save();
ctx.sendLocalized("commands.profile.display.reset", EmoteReference.CORRECT);
return;
}
var splitContent = content.replace(",", "").split("\\s+");
// new list of profile components
List<ProfileComponent> newComponents = new LinkedList<>();
for (var cmt : splitContent) {
var component = ProfileComponent.lookupFromString(cmt);
if (component != null && component.isAssignable()) {
newComponents.add(component);
}
}
if (newComponents.size() < 3) {
ctx.sendFormat(languageContext.get("commands.profile.display.not_enough") + languageContext.get("commands.profile.display.example"), EmoteReference.WARNING);
return;
}
playerData.setProfileComponents(newComponents);
player.save();
ctx.sendLocalized("commands.profile.display.success", EmoteReference.CORRECT, newComponents.stream().map(Enum::name).collect(Collectors.joining(", ")));
}
});
cr.registerAlias("profile", "me");
}
Aggregations