Fix DiscordAuthor using wrong username to retrieve avatars

This commit is contained in:
2023-06-13 18:37:47 +02:00
parent 7b4222545d
commit 24e6d84527
6 changed files with 33 additions and 17 deletions

View File

@@ -21,7 +21,6 @@ repositories {
maven { url "https://maven.firstdarkdev.xyz/releases" } maven { url "https://maven.firstdarkdev.xyz/releases" }
maven { url "https://maven.firstdarkdev.xyz/snapshots" } maven { url "https://maven.firstdarkdev.xyz/snapshots" }
maven { url "https://m2.dv8tion.net/releases" } maven { url "https://m2.dv8tion.net/releases" }
maven { url "https://nexus.velocitypowered.com/repository/maven-public/" }
} }
dependencies { dependencies {
@@ -61,6 +60,7 @@ dependencies {
implementation("commons-io:commons-io:${commonsio}") implementation("commons-io:commons-io:${commonsio}")
implementation("com.google.code.gson:gson:${gson}") implementation("com.google.code.gson:gson:${gson}")
implementation("com.google.guava:guava:31.1-jre") implementation("com.google.guava:guava:31.1-jre")
implementation 'com.mojang:authlib:4.0.43'
} }
shadowJar { shadowJar {

View File

@@ -1,6 +1,6 @@
version_major=0 version_major=0
version_minor=0 version_minor=0
version_patch=1 version_patch=3
shade_group=com.hypherionmc.sdlink.shaded. shade_group=com.hypherionmc.sdlink.shaded.

View File

@@ -14,39 +14,46 @@ import com.hypherionmc.sdlink.core.services.SDLinkPlatform;
public class DiscordAuthor { public class DiscordAuthor {
// User used for Server Messages // User used for Server Messages
public static final DiscordAuthor SERVER = new DiscordAuthor(SDLinkConfig.INSTANCE.channelsAndWebhooks.serverName, SDLinkConfig.INSTANCE.channelsAndWebhooks.serverAvatar, true); public static final DiscordAuthor SERVER = new DiscordAuthor(SDLinkConfig.INSTANCE.channelsAndWebhooks.serverName, SDLinkConfig.INSTANCE.channelsAndWebhooks.serverAvatar, "server", true);
private final String username; private final String displayName;
private final String avatar; private final String avatar;
private final boolean isServer; private final boolean isServer;
private final String username;
/** /**
* Internal. Use {@link #of(String, String)} * Internal. Use {@link #of(String, String, String)}
* @param username The Username of the Author * @param displayName The Username of the Author
* @param avatar The avatar URL of the Author * @param avatar The avatar URL of the Author
* @param isServer Is the Author the Minecraft Server * @param isServer Is the Author the Minecraft Server
*/ */
private DiscordAuthor(String username, String avatar, boolean isServer) { private DiscordAuthor(String displayName, String avatar, String username, boolean isServer) {
this.username = username; this.displayName = displayName;
this.avatar = avatar; this.avatar = avatar;
this.username = username;
this.isServer = isServer; this.isServer = isServer;
} }
/** /**
* Create a new Discord Author * Create a new Discord Author
* @param username The name/Username of the Author * @param displayName The name/Username of the Author
* @param uuid The Mojang UUID of the Author * @param uuid The Mojang UUID of the Author
* @return A constructed {@link DiscordAuthor} * @return A constructed {@link DiscordAuthor}
*/ */
public static DiscordAuthor of(String username, String uuid) { public static DiscordAuthor of(String displayName, String uuid, String username) {
return new DiscordAuthor( return new DiscordAuthor(
username, displayName,
SDLinkConfig.INSTANCE.chatConfig.playerAvatarType.resolve(SDLinkPlatform.minecraftHelper.isOnlineMode() ? uuid : username), SDLinkConfig.INSTANCE.chatConfig.playerAvatarType.resolve(SDLinkPlatform.minecraftHelper.isOnlineMode() ? uuid : username),
username,
false false
); );
} }
public String getUsername() { public String getDisplayName() {
return displayName;
}
public String getRawUsername() {
return username; return username;
} }

View File

@@ -10,6 +10,7 @@ import com.hypherionmc.sdlink.core.discord.BotController;
import com.hypherionmc.sdlink.core.managers.RoleManager; import com.hypherionmc.sdlink.core.managers.RoleManager;
import com.hypherionmc.sdlink.core.messaging.Result; import com.hypherionmc.sdlink.core.messaging.Result;
import com.hypherionmc.sdlink.core.services.SDLinkPlatform; import com.hypherionmc.sdlink.core.services.SDLinkPlatform;
import com.mojang.authlib.GameProfile;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.User;
@@ -78,7 +79,7 @@ public class MinecraftAccount {
* Convert a username to an offline account * Convert a username to an offline account
* @param username The Username to search for * @param username The Username to search for
*/ */
public static MinecraftAccount offline(String username) { private static MinecraftAccount offline(String username) {
Pair<String, UUID> player = offlinePlayer(username); Pair<String, UUID> player = offlinePlayer(username);
return new MinecraftAccount( return new MinecraftAccount(
player.getLeft(), player.getLeft(),
@@ -88,6 +89,14 @@ public class MinecraftAccount {
); );
} }
/**
* Convert GameProfile to Minecraft account
* @param profile The profile of the player
*/
public static MinecraftAccount fromGameProfile(GameProfile profile) {
return standard(profile.getName());
}
public String getUsername() { public String getUsername() {
return username; return username;
} }

View File

@@ -70,7 +70,7 @@ public final class DiscordMessage {
// Check if a webhook is configured, and use that instead // Check if a webhook is configured, and use that instead
if (channel.getMiddle() != null && SDLinkConfig.INSTANCE.channelsAndWebhooks.webhooks.enabled) { if (channel.getMiddle() != null && SDLinkConfig.INSTANCE.channelsAndWebhooks.webhooks.enabled) {
WebhookMessageBuilder builder = new WebhookMessageBuilder(); WebhookMessageBuilder builder = new WebhookMessageBuilder();
builder.setUsername(this.author.getUsername()); builder.setUsername(this.author.getDisplayName());
if (!this.author.getAvatar().isEmpty()) { if (!this.author.getAvatar().isEmpty()) {
builder.setAvatarUrl(this.author.getAvatar()); builder.setAvatarUrl(this.author.getAvatar());
} }
@@ -99,7 +99,7 @@ public final class DiscordMessage {
} else { } else {
channel.getLeft().sendMessage( channel.getLeft().sendMessage(
this.messageType == MessageType.CHAT ? this.messageType == MessageType.CHAT ?
SDLinkConfig.INSTANCE.messageFormatting.chat.replace("%player%", author.getUsername()).replace("%message%", message) SDLinkConfig.INSTANCE.messageFormatting.chat.replace("%player%", author.getDisplayName()).replace("%message%", message)
: message) : message)
.queue(success -> { .queue(success -> {
if (afterSend != null) if (afterSend != null)
@@ -140,7 +140,7 @@ public final class DiscordMessage {
if (withAuthor) { if (withAuthor) {
builder.setAuthor( builder.setAuthor(
this.author.getUsername(), this.author.getDisplayName(),
null, null,
this.author.getAvatar().isEmpty() ? null : this.author.getAvatar() this.author.getAvatar().isEmpty() ? null : this.author.getAvatar()
); );

View File

@@ -32,7 +32,7 @@ public final class DiscordMessageBuilder {
public DiscordMessageBuilder author(DiscordAuthor author) { public DiscordMessageBuilder author(DiscordAuthor author) {
this.author = author; this.author = author;
if (author.getUsername().equalsIgnoreCase("server")) { if (author.getRawUsername().equalsIgnoreCase("server")) {
this.author = DiscordAuthor.SERVER; this.author = DiscordAuthor.SERVER;
} }