Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix pingable name listener #494

Merged
merged 2 commits into from
Nov 29, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import net.discordjug.javabot.util.StringUtils;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberUpdateEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.io.IOException;
Expand All @@ -15,6 +15,7 @@
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand All @@ -25,6 +26,7 @@ public class PingableNameListener extends ListenerAdapter {

private static final String ADJECTIVES_URL = "https://gist.githubusercontent.com/karlbright/f91229b8c5ac6f4291dc/raw/4a69c2c50b88ee4559b021c443fee899535adc60/adjectives.txt";
private static final String NOUNS_URL = "https://raw.githubusercontent.com/hugsy/stuff/main/random-word/english-nouns.txt";
private static final Pattern PINGABLE_NAME_PATTERN = Pattern.compile("(\\W*\\w){3,}\\W*");
private static final Random random = new Random();
private final NotificationService notificationService;
private final List<String> nouns;
Expand All @@ -44,21 +46,20 @@ public PingableNameListener(NotificationService notificationService) {

@Override
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
checkNickname(event.getMember(), null);
checkNickname(event.getMember());
}

@Override
public void onGuildMemberUpdateNickname(GuildMemberUpdateNicknameEvent event) {
checkNickname(event.getMember(), event.getNewNickname());
public void onGuildMemberUpdate(GuildMemberUpdateEvent event) {
checkNickname(event.getMember());
}

/**
* Checks whether the given {@link Member}'s nickname should be changed.
* @param member The {@link Member} to check.
* @param nickname The {@link Member}'s new Nickname, null if that does not exist.
*/
private void checkNickname(Member member, String nickname) {
if (!(nickname==null||isPingable(nickname)) && !isPingable(member.getUser().getName()) && !canBypassCheck(member)) {
private void checkNickname(Member member) {
if (!isPingable(member.getEffectiveName()) && !canBypassCheck(member)) {
changeName(member);
}
}
Expand All @@ -72,9 +73,9 @@ private void changeName(Member member) {
String newName = generateRandomName();
member.modifyNickname(newName.substring(0, Math.min(31, newName.length()))).queue();
member.getUser().openPrivateChannel()
.flatMap(channel -> channel.sendMessageFormat("Your nickname has been set to `%s` since both your user- and nickname's first three characters were deemed as not-pingable.", newName))
.flatMap(channel -> channel.sendMessageFormat("Your display name in %s has been set to `%s` since your previous name was deemed as non-pingable.", member.getGuild().getName(), newName))
.queue();
notificationService.withGuild(member.getGuild()).sendToModerationLog(c -> c.sendMessageFormat("Changed %s's nickname from `%s` to `%s`.", member.getAsMention(), oldName, newName));
notificationService.withGuild(member.getGuild()).sendToMessageLog(c -> c.sendMessageFormat("Changed %s's nickname from `%s` to `%s`.", member.getAsMention(), oldName, newName));
}

/**
Expand All @@ -83,15 +84,7 @@ private void changeName(Member member) {
* @return True if first three characters contain invalid characters, False if not.
*/
private boolean isPingable(String name) {
if (name == null) return true;
char[] nameChars = name.toCharArray();
for (int i = 0; i < Math.min(2,name.length()); i++) {
char c = nameChars[i];
if (c < 32 || c > 126) {
return false;
}
}
return true;
return PINGABLE_NAME_PATTERN.matcher(name).matches();
}

/**
Expand All @@ -101,8 +94,7 @@ private boolean isPingable(String name) {
private String generateRandomName() {
String noun = nouns.get(random.nextInt(nouns.size()));
String adjective = adjectives.get(random.nextInt(adjectives.size()));
int number = random.nextInt(10000);
return StringUtils.capitalize(adjective) + StringUtils.capitalize(noun) + number;
return StringUtils.capitalize(adjective) + StringUtils.capitalize(noun);
}

/**
Expand Down Expand Up @@ -130,6 +122,6 @@ private static List<String> readStrings(String url) {
* @return Whether the Member can bypass name-checks or not.
*/
private static boolean canBypassCheck(Member member) {
return member.getUser().isBot() || member.getUser().isSystem() || member.getGuild().getSelfMember().canInteract(member);
return member.getUser().isBot() || member.getUser().isSystem() || !member.getGuild().getSelfMember().canInteract(member);
}
}