Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/org/enginehub/discord/util/PunishmentUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

package org.enginehub.discord.util;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User;
Expand All @@ -30,6 +32,12 @@

public final class PunishmentUtil {

private record BanKey(long guildId, long userId) { }

private static final Cache<BanKey, Boolean> RECENT_BANS = CacheBuilder.newBuilder()
.expireAfterAccess(1, TimeUnit.MINUTES)
.build();

private static String getContactString() {
return "Submit an appeal via https://ehub.to/ban-appeal if you wish to appeal.";
}
Expand All @@ -44,6 +52,10 @@ public static void kickUser(Guild guild, Member member, String reason) {
}

public static void banUser(Guild guild, User user, String reason, boolean eraseHistory) {
if (RECENT_BANS.asMap().putIfAbsent(new BanKey(guild.getIdLong(), user.getIdLong()), Boolean.TRUE) != null) {
return;
}

var _ = user.openPrivateChannel().submit()
.thenCompose(privateChannel ->
privateChannel.sendMessage("You have been banned for `" + reason + "`. " + getContactString())
Expand Down