Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

Commit

Permalink
Implement tier getting and storage
Browse files Browse the repository at this point in the history
  • Loading branch information
weeryan17 committed Nov 9, 2019
1 parent 475a64d commit 965126d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void onCommand(Member sender, CommandContext context) {
scriptEngine.put("context", context);
scriptEngine.put("channel", context.getChannel());
scriptEngine.put("guild", context.getGuild());
scriptEngine.put("data", context.getData());

String imports = IMPORTS.stream().map(s -> "import " + s + ".*;").collect(Collectors.joining(" "));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
import org.bson.conversions.Bson;
import org.cascadebot.cascadebot.CascadeBot;
import org.cascadebot.cascadebot.data.database.DebugLogCallback;
import org.cascadebot.cascadebot.data.objects.CascadeUser;
import org.cascadebot.cascadebot.data.objects.user.CascadeUser;

import java.util.concurrent.TimeUnit;

import static com.mongodb.client.model.Filters.eq;

public class CascadeUserDataManager {

private static final String COLLECTION = "users";
private static final String COLLECTION = "bot_users";

private static LoadingCache<Long, CascadeUser> users = Caffeine.newBuilder()
.expireAfterAccess(5,TimeUnit.MINUTES)
.expireAfterAccess(1,TimeUnit.SECONDS)
.recordStats()
.build(id -> {
CascadeUser user = CascadeBot.INS.getDatabaseManager().getDatabase().getCollection(COLLECTION, CascadeUser.class).find(eq("_id", id)).first();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ public Set<Flag> getFlags() {
return flags;
}

public Flag getFlag(String id) {
for (Flag flag : flags) {

This comment has been minimized.

Copy link
@binaryoverload

binaryoverload Nov 9, 2019

Member

A simplified way of doing this could be getFlags().stream().filter(flag -> flat.getId().equal(id)).findFirst().orElse(null)

if (flag.getId().equals(id)) {
return flag;
}
}
if (!parent.isEmpty()) {
return tiers.get(parent).getFlag(id);
}
return null;
}

/**
* Returns the guild benefits gave in this tier.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import org.cascadebot.cascadebot.commandmeta.ICommandMain;
import org.cascadebot.cascadebot.commandmeta.Module;
import org.cascadebot.cascadebot.data.language.Locale;
import org.cascadebot.cascadebot.data.managers.CascadeUserDataManager;
import org.cascadebot.cascadebot.data.objects.donation.Tier;
import org.cascadebot.cascadebot.data.objects.user.CascadeUser;
import org.cascadebot.cascadebot.utils.buttons.ButtonGroup;
import org.cascadebot.cascadebot.utils.buttons.ButtonsCache;
import org.cascadebot.cascadebot.utils.buttons.PersistentButtonGroup;
Expand Down Expand Up @@ -57,13 +60,6 @@ public class GuildData {
private GuildSettingsUseful usefulSettings = new GuildSettingsUseful();
private GuildModeration guildModeration = new GuildModeration();

/*
Eventually these will be used but they're commented out for now
private GuildModeration guildModeration = new GuildModeration();
*/

//endregion

//region Transient fields
Expand All @@ -76,6 +72,8 @@ public class GuildData {

private HashMap<Long, HashMap<Long, PersistentButtonGroup>> persistentButtons = new HashMap<>();

private long userForTiers = -1;

@PreSave
public void preSave() {
this.stateLock = UUID.randomUUID();
Expand Down Expand Up @@ -185,4 +183,17 @@ public Collection<GuildCommandInfo> getGuildCommandInfos() {

//endregion

public Tier getGuildTier() {
if (userForTiers == -1) {
return Tier.getTier("default");
}

CascadeUser user = CascadeUserDataManager.getUser(userForTiers);
return user.getTier();
}

public void setUserForTiers(long userId) {
this.userForTiers = userId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
* Licensed under the MIT license.
*/

package org.cascadebot.cascadebot.data.objects;
package org.cascadebot.cascadebot.data.objects.user;

import de.bild.codec.annotations.Id;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.cascadebot.cascadebot.data.objects.donation.Tier;

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CascadeUser {

Expand All @@ -20,6 +22,10 @@ public CascadeUser(long id) {
userId = id;
}

private Tier tier;
private String tier = "default";

public Tier getTier() {
return Tier.getTier(tier);
}

}
1 change: 1 addition & 0 deletions src/main/resources/default_tiers.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"music_services",
"music_controls",
"music_soundcloud",
"donator_node",
"custom_backgrounds",
"planner_agenda",
"medium_badge"
Expand Down

0 comments on commit 965126d

Please sign in to comment.