Skip to content

Commit

Permalink
Add the counts in bots and users to the warning in the console
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Feb 25, 2018
1 parent a045f82 commit 0314e57
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ plugins {

group = 'ml.duncte123.skybot'

version "3.61.13_${getGitHash()}"
version "3.61.14_${getGitHash()}"

sourceCompatibility = 1.9

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/ml/duncte123/skybot/BotListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,7 @@ public void onGuildMemberJoin(GuildMemberJoinEvent event) {
&& event.getGuild().getSelfMember().hasPermission(Permission.MANAGE_ROLES)) {
Role r = event.getGuild().getRoleById(settings.getAutoroleRole());
if(r != null && !event.getGuild().getPublicRole().equals(r))
event.getGuild().getController().addSingleRoleToMember(event.getMember(), r).queue(null, it -> {
MessageUtils.sendMsg(GuildUtils.getPublicChannel(event.getGuild()),"Error while trying to add a role to a user: " + it.toString() + "\n" +
"Make sure that the role " + r.getAsMention() + " is below my role");
});
event.getGuild().getController().addSingleRoleToMember(event.getMember(), r).queue(null, it -> {});
}
}

Expand Down Expand Up @@ -336,6 +333,7 @@ public void onGuildMemberLeave(GuildMemberLeaveEvent event) {
public void onGuildJoin(GuildJoinEvent event) {
//if 70 of a guild is bots, we'll leave it
double[] botToUserRatio = GuildUtils.getBotRatio(event.getGuild());
long[] counts = GuildUtils.getBotAndUserCount(event.getGuild());
if (botToUserRatio[1] >= 60) {
MessageUtils.sendMsg(GuildUtils.getPublicChannel(event.getGuild()),
String.format("Hey %s, %s%s of this guild are bots (%s is the total btw). I'm outta here.",
Expand All @@ -347,7 +345,10 @@ public void onGuildJoin(GuildJoinEvent event) {
message -> message.getGuild().leave().queue(),
er -> event.getGuild().leave().queue()
);
logger.info(TextColor.RED + "Joining guild: " + event.getGuild().getName() + ", and leaving it after. BOT ALERT" + TextColor.RESET);
logger.info(TextColor.RED + String.format("Joining guild: %s, and leaving it after. BOT ALERT (%s/%s)",
event.getGuild().getName(),
counts[0],
counts[1]) + TextColor.RESET);
return;
}
Guild g = event.getGuild();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ml/duncte123/skybot/utils/AirUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public static String generateRandomString() {
* @return a flipped table
*/
public static String flipTable() {
switch (AirUtils.RAND.nextInt(4)){
switch (RAND.nextInt(4)){
case 0:
return "(╯°□°)╯︵┻━┻";
case 1:
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/ml/duncte123/skybot/utils/GuildUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.utils.cache.MemberCacheView;
import org.apache.commons.lang3.tuple.Triple;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
Expand Down Expand Up @@ -99,6 +100,23 @@ public static void updateGuildCountAndCheck(JDA jda, long newGuildCount) {
}
}

/**
* Returns an array with the member counts of the guild
* 0 = the total users
* 1 = the total bots
* 3 = the total members
* @param g The {@link Guild Guild} to count the users in
* @return an array with the member counts of the guild
*/
public static long[] getBotAndUserCount(Guild g) {
MemberCacheView memberCache = g.getMemberCache();
long totalCount = memberCache.size();
long botCount = memberCache.stream().filter(it -> it.getUser().isBot()).count();
long userCount = totalCount - botCount;

return new long[] {userCount, botCount, totalCount};
}

/**
* This will calculate the bot to user ratio
*
Expand All @@ -107,10 +125,10 @@ public static void updateGuildCountAndCheck(JDA jda, long newGuildCount) {
*/
public static double[] getBotRatio(Guild g) {

MemberCacheView memberCache = g.getMemberCache();
double totalCount = memberCache.size();
double botCount = memberCache.stream().filter(it -> it.getUser().isBot()).count();
double userCount = totalCount - botCount;
long[] counts = getBotAndUserCount(g);
double totalCount = counts[2];
double userCount = counts[0];
double botCount = counts[1];

//percent in users
double userCountP = (userCount / totalCount) * 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ package ml.duncte123.skybot.unstable.utils
import ml.duncte123.skybot.Author
import ml.duncte123.skybot.utils.MessageUtils
import ml.duncte123.skybot.utils.TextColor
import ml.duncte123.skybot.utils.hastebin
import ml.duncte123.skybot.utils.leeks
import net.dv8tion.jda.core.entities.MessageChannel
import net.dv8tion.jda.core.entities.TextChannel
import org.apache.commons.lang3.StringUtils
Expand Down Expand Up @@ -107,7 +107,7 @@ class ComparatingUtils {

val hastedata = data.keys.map { it.ex.printStackTrace("") }.joinToString(separator = "\n\n\n\n================================\n\n\n\n", prefix = "\n\n\n\n")

val haste = hastebin(hastedata)
val haste = leeks(hastedata)

data.keys.forEachIndexed { index, exceptionType ->
table.add(listOf("$index", exceptionType.message, "${exceptionType.count}", haste))
Expand Down
21 changes: 19 additions & 2 deletions src/main/kotlin/ml/duncte123/skybot/utils/WebUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,18 @@ class WebUtils {
LOGGER.info("${TextColor.PURPLE}Generated wastebin link: $returnValue${TextColor.RESET}")
return returnValue
}
/**
* Posts [String]s to haste.leeks.life
* @param s the [String]
* @returns the url of the created post as kotlin file
*
* @see WebUtils.postRawToService([Service], [String])
*/
fun leeks(s: String): String {
val returnValue = "wastebin.party/" + postRawToService(Service.LEEKS, s).getString("key") + ".kt"
LOGGER.info("${TextColor.PURPLE}Generated paste by leeks link: $returnValue${TextColor.RESET}")
return returnValue
}
}
/**
* This holds some variables that we will accept
Expand All @@ -299,7 +311,8 @@ class WebUtils {

enum class Service(val url: String) {
HASTEBIN("https://hastebin.com/documents"),
WASTEBIN("https://wastebin.party/documents")
WASTEBIN("https://wastebin.party/documents"),
LEEKS("https://haste.leeks.life/documents")
}
}

Expand All @@ -313,4 +326,8 @@ fun hastebin(s: String): String = WebUtils.hastebin(s)
/**
* @see WebUtils.wastebin([String])
*/
fun wastebin(s: String): String = WebUtils.wastebin(s)
fun wastebin(s: String): String = WebUtils.wastebin(s)
/**
* @see WebUtils.leeks([String])
*/
fun leeks(s: String): String = WebUtils.wastebin(s)
2 changes: 1 addition & 1 deletion src/main/resources/logback.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if(logToFile) {
pattern = "[%d{dd-MM-yyyy HH:mm:ss, -5}] [%boldCyan(%thread)] [%boldGreen(%logger{36})] %red(%X{jda.shard}) %level - %msg%n"
}
}
root(DEBUG, ["FILE"])
root(DEBUG, ["FILE", "STDOUT"])
}


0 comments on commit 0314e57

Please sign in to comment.