use of com.rethinkdb.net.Connection in project MantaroBot by Mantaro.
the class MoneyCmds method richest.
@Subscribe
public void richest(CommandRegistry cr) {
final RateLimiter rateLimiter = new RateLimiter(TimeUnit.SECONDS, 10);
final String pattern = ":g$";
ITreeCommand leaderboards = (ITreeCommand) cr.register("leaderboard", new TreeCommand(Category.CURRENCY) {
@Override
public Command defaultTrigger(GuildMessageReceivedEvent event, String mainCommand, String commandName) {
return new SubCommand() {
@Override
protected void call(GuildMessageReceivedEvent event, String content) {
if (!handleDefaultRatelimit(rateLimiter, event.getAuthor(), event))
return;
OrderBy template = r.table("players").orderBy().optArg("index", r.desc("money"));
Cursor<Map> c1 = getGlobalRichest(template, pattern);
List<Map> c = c1.toList();
c1.close();
event.getChannel().sendMessage(baseEmbed(event, "Money leaderboard (Top 10)", event.getJDA().getSelfUser().getEffectiveAvatarUrl()).setDescription(c.stream().map(map -> Pair.of(MantaroBot.getInstance().getUserById(map.get("id").toString().split(":")[0]), map.get("money").toString())).filter(p -> Objects.nonNull(p.getKey())).map(p -> String.format("%s**%s#%s** - $%s", EmoteReference.MARKER, p.getKey().getName(), p.getKey().getDiscriminator(), p.getValue())).collect(Collectors.joining("\n"))).build()).queue();
}
};
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Leaderboard").setDescription("**Returns the leaderboard.**").addField("Usage", "`~>leaderboard` - **Returns the money leaderboard.**\n" + "`~>leaderboard rep` - **Returns the reputation leaderboard.**\n" + "`~>leaderboard lvl` - **Returns the level leaderboard.**\n" + "~>leaderboard streak - **Returns the daily streak leaderboard.", false).build();
}
});
leaderboards.addSubCommand("lvl", new SubCommand() {
@Override
protected void call(GuildMessageReceivedEvent event, String content) {
if (!handleDefaultRatelimit(rateLimiter, event.getAuthor(), event))
return;
Cursor<Map> m;
try (Connection conn = Utils.newDbConnection()) {
m = r.table("players").orderBy().optArg("index", r.desc("level")).filter(player -> player.g("id").match(pattern)).map(player -> player.pluck("id", "level", r.hashMap("data", "experience"))).limit(10).run(conn, OptArgs.of("read_mode", "outdated"));
}
List<Map> c = m.toList();
m.close();
event.getChannel().sendMessage(baseEmbed(event, "Level leaderboard (Top 10)", event.getJDA().getSelfUser().getEffectiveAvatarUrl()).setDescription(c.stream().map(map -> Pair.of(MantaroBot.getInstance().getUserById(map.get("id").toString().split(":")[0]), map.get("level").toString() + "\n - Experience: **" + ((Map) map.get("data")).get("experience") + "**")).filter(p -> Objects.nonNull(p.getKey())).map(p -> String.format("%s**%s#%s** - %s", EmoteReference.MARKER, p.getKey().getName(), p.getKey().getDiscriminator(), p.getValue())).collect(Collectors.joining("\n"))).build()).queue();
}
});
leaderboards.addSubCommand("rep", new SubCommand() {
@Override
protected void call(GuildMessageReceivedEvent event, String content) {
Cursor<Map> m;
try (Connection conn = Utils.newDbConnection()) {
m = r.table("players").orderBy().optArg("index", r.desc("reputation")).filter(player -> player.g("id").match(pattern)).map(player -> player.pluck("id", "reputation")).limit(10).run(conn, OptArgs.of("read_mode", "outdated"));
}
List<Map> c = m.toList();
m.close();
event.getChannel().sendMessage(baseEmbed(event, "Reputation leaderboard (Top 10)", event.getJDA().getSelfUser().getEffectiveAvatarUrl()).setDescription(c.stream().map(map -> Pair.of(MantaroBot.getInstance().getUserById(map.get("id").toString().split(":")[0]), map.get("reputation").toString())).filter(p -> Objects.nonNull(p.getKey())).map(p -> String.format("%s**%s#%s** - %s", EmoteReference.MARKER, p.getKey().getName(), p.getKey().getDiscriminator(), p.getValue())).collect(Collectors.joining("\n"))).build()).queue();
}
});
leaderboards.addSubCommand("streak", new SubCommand() {
@Override
protected void call(GuildMessageReceivedEvent event, String content) {
Cursor<Map> m;
try (Connection conn = Utils.newDbConnection()) {
m = r.table("players").orderBy().optArg("index", r.desc("userDailyStreak")).filter(player -> player.g("id").match(pattern)).map(player -> player.pluck("id", r.hashMap("data", "dailyStrike"))).limit(10).run(conn, OptArgs.of("read_mode", "outdated"));
}
List<Map> c = m.toList();
m.close();
event.getChannel().sendMessage(baseEmbed(event, "Daily streak leaderboard (Top 10)", event.getJDA().getSelfUser().getEffectiveAvatarUrl()).setDescription(c.stream().map(map -> Pair.of(MantaroBot.getInstance().getUserById(map.get("id").toString().split(":")[0]), ((HashMap) (map.get("data"))).get("dailyStrike").toString())).filter(p -> Objects.nonNull(p.getKey())).map(p -> String.format("%s**%s#%s** - %sx", EmoteReference.MARKER, p.getKey().getName(), p.getKey().getDiscriminator(), p.getValue())).collect(Collectors.joining("\n"))).build()).queue();
}
});
// TODO enable in 4.9
/*
leaderboards.addSubCommand("localxp", new SubCommand() {
@Override
protected void call(GuildMessageReceivedEvent event, String content) {
List<Map> l;
try(Connection conn = Utils.newDbConnection()) {
l = r.table("guilds")
.get(event.getGuild().getId())
.getField("data")
.getField("localPlayerExperience")
.run(conn, OptArgs.of("read_mode", "outdated"));
}
l.sort(Comparator.<Map>comparingLong(o -> (long) o.get("experience")).reversed());
event.getChannel().sendMessage(
baseEmbed(event,
"Local level leaderboard", event.getJDA().getSelfUser().getEffectiveAvatarUrl()
).setDescription(l.stream()
.map(map -> Pair.of(MantaroBot.getInstance().getUserById(map.get("userId").toString()), map.get("level").toString() +
"\n - Experience: **" + map.get("experience") + "**\n"))
.map(p -> String.format("%s**%s** - %s", EmoteReference.MARKER,
p == null ? "User left guild" : p.getKey().getName() + "#" + p.getKey().getDiscriminator(), p.getValue()))
.collect(Collectors.joining("\n"))
).build()
).queue();
}
});
leaderboards.createSubCommandAlias("localxp", "local");
*/
leaderboards.createSubCommandAlias("rep", "reputation");
leaderboards.createSubCommandAlias("lvl", "level");
leaderboards.createSubCommandAlias("streak", "daily");
cr.registerAlias("leaderboard", "richest");
}
Aggregations