Skip to content

Commit

Permalink
Merge pull request #181 from Gerald-Development/xp-dev-pain-balls
Browse files Browse the repository at this point in the history
Xp dev pain balls
Need I say anything else?
  • Loading branch information
ElementalMP4 authored May 31, 2022
2 parents b3a48ea + 4fb943b commit 6ed6bcf
Show file tree
Hide file tree
Showing 16 changed files with 1,185 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/main/java/de/voidtech/gerald/GlobalConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public class GlobalConstants {
public static final String STREAM_URL = "https://twitch.tv/elementalmp4";
public static final String LINKTREE_URL = "https://linktr.ee/GeraldBot";
public static final String INVITE_URL = "https://discord.com/api/oauth2/authorize?client_id=555816892141404163&permissions=805694544&scope=bot%20applications.commands";
public static final String VERSION = "1.2.8 - Mucho Mocha";
public static final String VERSION = "1.3.0 - Delightful Doppio";
}
136 changes: 104 additions & 32 deletions src/main/java/de/voidtech/gerald/commands/actions/ActionsCommand.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
package main.java.de.voidtech.gerald.commands.actions;

import main.java.de.voidtech.gerald.commands.AbstractCommand;
import main.java.de.voidtech.gerald.commands.CommandContext;
import main.java.de.voidtech.gerald.entities.ActionStats;
import main.java.de.voidtech.gerald.service.ServerService;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.awt.*;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import main.java.de.voidtech.gerald.commands.AbstractCommand;
import main.java.de.voidtech.gerald.commands.CommandContext;
import main.java.de.voidtech.gerald.entities.ActionStats;
import main.java.de.voidtech.gerald.entities.Server;
import main.java.de.voidtech.gerald.service.ServerService;
import main.java.de.voidtech.gerald.util.ParsingUtils;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;

public abstract class ActionsCommand extends AbstractCommand {

private static final String API_URL = "http://api.nekos.fun:8080/api/";
Expand Down Expand Up @@ -63,6 +67,32 @@ private ActionStats getOrCreateProfile(String id, ActionType action, long server
}
return stats;
}

@SuppressWarnings("unchecked")
private List<ActionStats> getTopGivenInServer(ActionType type, long serverID) {
try(Session session = sessionFactory.openSession())
{
return (List<ActionStats>) session
.createQuery("FROM ActionStats WHERE type = :type AND serverID = :serverID AND givenCount > 0 ORDER BY givenCount DESC")
.setMaxResults(5)
.setParameter("type", type.getType())
.setParameter("serverID", serverID)
.list();
}
}

@SuppressWarnings("unchecked")
private List<ActionStats> getTopReceivedInServer(ActionType type, long serverID) {
try(Session session = sessionFactory.openSession())
{
return (List<ActionStats>) session
.createQuery("FROM ActionStats WHERE type = :type AND serverID = :serverID AND receivedCount > 0 ORDER BY receivedCount DESC")
.setMaxResults(5)
.setParameter("type", type.getType())
.setParameter("serverID", serverID)
.list();
}
}

private void updateStatsProfile(ActionStats stats) {
try(Session session = sessionFactory.openSession())
Expand Down Expand Up @@ -103,29 +133,71 @@ private String getStatsString(String giver, String receiver, ActionType action,
}

public void sendAction(CommandContext context, ActionType action) {
if(context.getMentionedMembers().isEmpty()) {
if (context.getArgs().get(0).equals("leaderboard")) {
sendActionLeaderboard(context, action);
return;
}
if (context.getMentionedMembers().isEmpty()) {
context.reply("You need to mention someone to " + action.getType() + "!");
} else {
String gifURL = getActionGif(action.getType());
if (gifURL != null)
{
updateActionStats(context.getAuthor().getId(), context.getMentionedMembers().get(0).getId(), action, context);
String phrase = String.format("%s %s %s", context.getMember().getEffectiveName(), conjugateAction(action.getType()),
context.getMentionedMembers().get(0).getId().equals(context.getAuthor().getId()) ? "themself" : context.getMentionedMembers().get(0).getEffectiveName());

EmbedBuilder actionEmbedBuilder = new EmbedBuilder();
actionEmbedBuilder.setTitle(phrase);
actionEmbedBuilder.setColor(Color.ORANGE);
if (!gifURL.equals("")) {
actionEmbedBuilder.setImage(gifURL);
}
actionEmbedBuilder.setFooter(getStatsString(context.getAuthor().getId(), context.getMentionedMembers().get(0).getId(), action, context));
MessageEmbed actionEmbed = actionEmbedBuilder.build();
context.reply(actionEmbed);
}
return;
}
String gifURL = getActionGif(action.getType());
if (gifURL != null) {
updateActionStats(context.getAuthor().getId(), context.getMentionedMembers().get(0).getId(), action, context);
String phrase = String.format("%s %s %s",
context.getMember().getEffectiveName(),
conjugateAction(action.getType()),
context.getMentionedMembers().get(0).getId().equals(context.getAuthor().getId()) ?
"themself" : context.getMentionedMembers().get(0).getEffectiveName());
//Wow that was a big one
EmbedBuilder actionEmbedBuilder = new EmbedBuilder();
actionEmbedBuilder.setTitle(phrase);
actionEmbedBuilder.setColor(Color.ORANGE);
if (!gifURL.equals("")) actionEmbedBuilder.setImage(gifURL);
actionEmbedBuilder.setFooter(getStatsString(context.getAuthor().getId(), context.getMentionedMembers().get(0).getId(), action, context));
MessageEmbed actionEmbed = actionEmbedBuilder.build();
context.reply(actionEmbed);
}
}

private void sendActionLeaderboard(CommandContext context, ActionType action) {
Server server = serverService.getServer(context.getGuild().getId());
List<ActionStats> topGiven = getTopGivenInServer(action, server.getId());
List<ActionStats> topReceived = getTopReceivedInServer(action, server.getId());

StringBuilder leaderboardBuilder = new StringBuilder();
int i = 1;
leaderboardBuilder.append("**Top 5 " + action.getType() + " givers**\n\n");
for (ActionStats stat : topGiven) {
leaderboardBuilder.append(ParsingUtils.convertSingleDigitToEmoji(String.valueOf(i)));
leaderboardBuilder.append(" ");
leaderboardBuilder.append(context.getGuild().retrieveMemberById(stat.getMember()).complete().getAsMention());
leaderboardBuilder.append(" - `");
leaderboardBuilder.append(stat.getGivenCount());
leaderboardBuilder.append("`\n");
i++;
}
if (i == 1) leaderboardBuilder.append("Nobody to show! Go " + action.getType() + " someone!\n");
i = 1;
leaderboardBuilder.append("**\nTop 5 " + action.getType() + " receivers**\n\n");
for (ActionStats stat : topReceived) {
leaderboardBuilder.append(ParsingUtils.convertSingleDigitToEmoji(String.valueOf(i)));
leaderboardBuilder.append(" ");
leaderboardBuilder.append(context.getGuild().retrieveMemberById(stat.getMember()).complete().getAsMention());
leaderboardBuilder.append(" - `");
leaderboardBuilder.append(stat.getReceivedCount());
leaderboardBuilder.append("`\n");
i++;
}
if (i == 1) leaderboardBuilder.append("Nobody to show! Go " + action.getType() + " someone!");
MessageEmbed leaderboardEmbed = new EmbedBuilder()
.setColor(Color.ORANGE)
.setTitle(context.getGuild().getName() + "'s " + action.getType() + " leaderboard")
.setDescription(leaderboardBuilder.toString())
.build();
context.reply(leaderboardEmbed);
}

private String conjugateAction(String action) {
String conjugatedAction = action;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private void sendWebhookMessage(CommandContext context, List<String> args, Membe
messageToBeSent.append(args.get(i)).append(" ");
}
Webhook impersonateHook = webhookManager.getOrCreateWebhook((TextChannel) context.getChannel(), "BGImpersonate", context.getJDA().getSelfUser().getId());
webhookManager.postMessage(messageToBeSent.toString(), memberToBeImpersonated.getUser().getAvatarUrl(), memberToBeImpersonated.getUser().getName(), impersonateHook);
webhookManager.postMessage(messageToBeSent.toString(), null, memberToBeImpersonated.getUser().getAvatarUrl(), memberToBeImpersonated.getUser().getName(), impersonateHook);

//TODO (from: Franziska): Message needs to be deleted, message context does not have a message object. Should we add one? Do we do this somehow else? Should this command be available through slashes at all!?
//context.delete().queue();
Expand Down
Loading

0 comments on commit 6ed6bcf

Please sign in to comment.