-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add support for migrated discord usernames
- Loading branch information
Showing
5 changed files
with
99 additions
and
13 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/main/java/com/github/khakers/modmailviewer/util/DiscordUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.github.khakers.modmailviewer.util; | ||
|
||
import com.github.khakers.modmailviewer.auth.UserToken; | ||
import com.github.khakers.modmailviewer.data.User; | ||
|
||
public class DiscordUtils { | ||
/** | ||
* Returns true if the user is a migrated user (i.e. has no discriminator) | ||
* | ||
* @return True if the user is a migrated user | ||
*/ | ||
public static boolean isMigratedUserName(UserToken user) { | ||
return user.getDiscriminator().equals("0") || user.getDiscriminator().isBlank(); | ||
} | ||
|
||
public static boolean isLegacyUsername(UserToken user) { | ||
return !(user.getDiscriminator().equals("0") || user.getDiscriminator().isBlank()); | ||
|
||
} | ||
|
||
/** | ||
* Returns true if the user is a migrated user (i.e. has no discriminator) | ||
* | ||
* @param user The user to check | ||
* @return True if the user is a migrated user | ||
*/ | ||
public static boolean isMigratedUserName(User user) { | ||
return user.discriminator().equals("0") || user.discriminator().isBlank(); | ||
} | ||
|
||
public static boolean isLegacyUsername(User user) { | ||
return !(user.discriminator().equals("0") || user.discriminator().isBlank()); | ||
} | ||
|
||
/** | ||
* @return The discriminator string for the user starting with a # or an empty string if the user is a migrated user | ||
*/ | ||
public static String getDiscriminatorString(UserToken user) { | ||
if (isMigratedUserName(user)) | ||
return ""; | ||
else | ||
return "#" + user.getDiscriminator(); | ||
} | ||
|
||
/** | ||
* @param user The user to get the Discord discriminator string for | ||
* @return The discriminator string for the user starting with a # or an empty string if the user is a migrated user | ||
*/ | ||
public static String getDiscriminatorString(User user) { | ||
if (isMigratedUserName(user)) | ||
return ""; | ||
else | ||
return "#" + user.discriminator(); | ||
} | ||
|
||
/** | ||
* Returns the avatar ID for the user | ||
* | ||
* @param user The user to get the avatar ID for | ||
* @return The avatar ID for the user | ||
*/ | ||
public static int getAvatarId(User user) { | ||
if (isMigratedUserName(user)) | ||
return (int) ((Long.parseLong(user.id()) >> 22) % 5); | ||
else | ||
return Integer.parseInt(user.discriminator()) % 5; | ||
} | ||
|
||
/** | ||
* Returns the avatar URL for the user | ||
* | ||
* @param user The user to get the avatar URL for | ||
* @return The avatar URL for the user | ||
*/ | ||
public static String getAvatarUrl(User user) { | ||
return "https://cdn.discordapp.com/embed/avatars/" + getAvatarId(user) + ".png"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
@import com.github.khakers.modmailviewer.data.User | ||
@import com.github.khakers.modmailviewer.util.DiscordUtils | ||
|
||
|
||
@param User user | ||
|
||
<span class="user-select-all" data-bs-toggle="tooltip" | ||
data-bs-title="${user.id()}"><span | ||
class="fw-semibold">${user.name()}</span><span | ||
class="text-muted">#${user.discriminator()}</span> | ||
</span> | ||
data-bs-title="${user.id()}"> | ||
<span class="fw-semibold">${user.name()}</span> | ||
@if(!DiscordUtils.isMigratedUserName(user)) | ||
<span class="text-muted">#${user.discriminator()}</span> | ||
|
||
@endif | ||
</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters