Skip to content

Commit

Permalink
Make use of the guild id to check warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Jan 23, 2018
1 parent 269b7ad commit 44c95f8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
23 changes: 23 additions & 0 deletions src/main/java/StringInInt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import sun.misc.Unsafe;


public class StringInInt {
private static Integer integer;

public static void main(String[] args) throws Throwable {
Constructor<Unsafe> c = Unsafe.class.getDeclaredConstructor();
c.setAccessible(true);
Unsafe u = c.newInstance();

Field field = StringInInt.class.getDeclaredField("integer");
Object b = u.staticFieldBase(field);
long o = u.staticFieldOffset(field);

u.putObject(b, o, "this is a string");

//String s = (String)(Object)integer;
System.out.println(integer);
}
}
12 changes: 9 additions & 3 deletions src/main/java/ml/duncte123/skybot/utils/ModerationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,17 @@ public static void addBannedUserToDb(String modID, String userName, String userD
* @param u the {@link User User} to check the warnings for
* @return The current amount of warnings that a user has
*/
public static int getWarningCountForUser(User u) {
public static int getWarningCountForUser(User u, Guild g) {
if(u == null)
throw new IllegalArgumentException("User to check can not be null");
try {
final int[] out = new int[1];
WebUtils.getJSONObject(Settings.apiBase + "/getWarnsForUser/json?user_id=" + u.getId(), it -> {
WebUtils.getJSONObject(String.format(
"%s/getWarnsForUser/json?user_id=%s&guild_id=%s",
Settings.apiBase,
u.getId(),
g.getId())
, it -> {
out[0] = it.getJSONArray("warnings").length();
return null;
});
Expand All @@ -160,10 +165,11 @@ public static int getWarningCountForUser(User u) {
* @param reason the reason for the warn
* @param jda a jda instance because we need the token for auth
*/
public static void addWarningToDb(User moderator, User target, String reason, JDA jda) {
public static void addWarningToDb(User moderator, User target, String reason, Guild guild, JDA jda) {
Map<String, Object> postFields = new HashMap<>();
postFields.put("mod_id", moderator.getId());
postFields.put("user_id", target.getId());
postFields.put("guild_id", guild.getId());
postFields.put("reason", reason.isEmpty()? "No Reason provided" : " for " + reason);
postFields.put("token", jda.getToken());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class WarnCommand: Command() {
sendError(event.message)
return
}
if(ModerationUtils.getWarningCountForUser(target.user) >= 3) {
if(ModerationUtils.getWarningCountForUser(target.user, event.guild) >= 3) {
event.guild.controller.kick(target).reason("Reached 3 warnings").queue()
ModerationUtils.modLog(event.author, target.user, "kicked", "Reached 3 warnings", event.guild)
return
Expand All @@ -58,7 +58,7 @@ class WarnCommand: Command() {
|Reason: ${if(reason.isEmpty()) "No reason given" else "`$reason`"}
""".trimMargin()

ModerationUtils.addWarningToDb(event.author, target.user, reason, event.jda)
ModerationUtils.addWarningToDb(event.author, target.user, reason, event.guild, event.jda)
ModerationUtils.modLog(event.author, target.user, "warned", reason, event.guild)
target.user.openPrivateChannel().queue {
//Ignore the fail consumer, we don't want to have spam in the console
Expand Down

0 comments on commit 44c95f8

Please sign in to comment.