Skip to content

Commit

Permalink
Refer to file system avatar in non-debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Rian8337 committed Nov 11, 2024
1 parent 1b9eed9 commit 38bbccc
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 87 deletions.
11 changes: 4 additions & 7 deletions src/events/ready/utils/playerTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,14 @@ export const run: EventUtil["run"] = async (client) => {
break;
}

const embed = await EmbedCreator.createRecentPlayEmbed(
score,
DroidHelper.getAvatarURL(trackedPlayer.uid),
8311585,
);

channel.send({
...(await EmbedCreator.createRecentPlayEmbed(
score,
8311585,
)),
content: MessageCreator.createAccept(
`Recent play for ${player.username}:`,
),
embeds: [embed],
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,18 @@ export const run: ButtonCommand["run"] = async (_, interaction) => {
PPCalculationMethod.live,
);

const embed = await EmbedCreator.createRecentPlayEmbed(
score,
player instanceof Player
? player.avatarUrl
: DroidHelper.getAvatarURL(player.id),
interaction.member.displayColor,
scoreAttribs?.attributes,
undefined,
localization.language,
);

const options: InteractionReplyOptions = {
content: MessageCreator.createAccept(
localization.getTranslation("recentPlayDisplay"),
player.username,
),
embeds: [embed],
...(await EmbedCreator.createRecentPlayEmbed(
score,
interaction.member.displayColor,
scoreAttribs?.attributes,
undefined,
localization.language,
)),
ephemeral: true,
};

Expand Down
19 changes: 7 additions & 12 deletions src/interactions/commands/osu! and osu!droid/compare/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,18 @@ export const run: SlashCommand["run"] = async (_, interaction) => {
PPCalculationMethod.live,
);

const embed = await EmbedCreator.createRecentPlayEmbed(
score,
player instanceof Player
? player.avatarUrl
: DroidHelper.getAvatarURL(player.id),
(<GuildMember | null>interaction.member)?.displayColor,
scoreAttribs?.attributes,
undefined,
localization.language,
);

const options: InteractionReplyOptions = {
...(await EmbedCreator.createRecentPlayEmbed(
score,
(<GuildMember | null>interaction.member)?.displayColor,
scoreAttribs?.attributes,
undefined,
localization.language,
)),
content: MessageCreator.createAccept(
localization.getTranslation("comparePlayDisplay"),
player.username,
),
embeds: [embed],
};

const replay = await ReplayHelper.analyzeReplay(score);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bold, GuildMember } from "discord.js";
import { AttachmentBuilder, bold, GuildMember } from "discord.js";
import { DatabaseManager } from "@database/DatabaseManager";
import { SlashSubcommand } from "structures/core/SlashSubcommand";
import { MessageCreator } from "@utils/creators/MessageCreator";
Expand Down Expand Up @@ -131,6 +131,9 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {
? player.rank
: ((await DroidHelper.getPlayerPPRank(player.id)) ?? 0);

const avatar = await DroidHelper.getAvatar(player.id);
const embedAvatarURL = "attachment://avatar.png";

embed
.setAuthor({
name: StringHelper.formatString(
Expand All @@ -140,11 +143,7 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {
iconURL: interaction.user.avatarURL()!,
url: ProfileManager.getProfileLink(player.id).toString(),
})
.setThumbnail(
player instanceof Player
? player.avatarUrl
: DroidHelper.getAvatarURL(player.id),
)
.setThumbnail(embedAvatarURL)
.setDescription(
`[${localization.getTranslation("avatarLink")}](${
player instanceof Player
Expand Down Expand Up @@ -176,6 +175,9 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {

InteractionHelper.reply(interaction, {
embeds: [embed],
files: avatar
? [new AttachmentBuilder(avatar, { name: "avatar.png" })]
: [],
});
};

Expand Down
21 changes: 9 additions & 12 deletions src/interactions/commands/osu! and osu!droid/recent/recent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,23 +213,20 @@ export const run: SlashCommand["run"] = async (_, interaction) => {
)
)?.attributes;

const embed = await EmbedCreator.createRecentPlayEmbed(
score,
player instanceof Player
? player.avatarUrl
: DroidHelper.getAvatarURL(player.id),
(<GuildMember | null>interaction.member)?.displayColor,
scoreAttribs,
score instanceof RecentPlay ? (score.osuAttribs ?? null) : undefined,
localization.language,
);

const options: InteractionReplyOptions = {
...(await EmbedCreator.createRecentPlayEmbed(
score,
(<GuildMember | null>interaction.member)?.displayColor,
scoreAttribs,
score instanceof RecentPlay
? (score.osuAttribs ?? null)
: undefined,
localization.language,
)),
content: MessageCreator.createAccept(
localization.getTranslation("recentPlayDisplay"),
player.username,
),
embeds: [embed],
};

const replay = await ReplayHelper.analyzeReplay(score);
Expand Down
17 changes: 6 additions & 11 deletions src/interactions/commands/osu! and osu!droid/simulate/simulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,22 +658,17 @@ export const run: SlashCommand["run"] = async (_, interaction) => {

BeatmapManager.setChannelLatestBeatmap(interaction.channelId, score.hash);

const embed = await EmbedCreator.createRecentPlayEmbed(
score,
player instanceof Player
? player.avatarUrl
: DroidHelper.getAvatarURL(player.id),
(<GuildMember | null>interaction.member)?.displayColor,
droidAttribs?.attributes,
osuAttribs?.attributes,
);

InteractionHelper.reply(interaction, {
...(await EmbedCreator.createRecentPlayEmbed(
score,
(<GuildMember | null>interaction.member)?.displayColor,
droidAttribs?.attributes,
osuAttribs?.attributes,
)),
content: MessageCreator.createAccept(
localization.getTranslation("simulatedPlayDisplay"),
player.username,
),
embeds: [embed],
});
};

Expand Down
20 changes: 7 additions & 13 deletions src/interactions/contextmenus/message/compareScore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { CommandHelper } from "@utils/helpers/CommandHelper";
import { InteractionHelper } from "@utils/helpers/InteractionHelper";
import { BeatmapManager } from "@utils/managers/BeatmapManager";
import { Modes } from "@rian8337/osu-base";
import { Player } from "@rian8337/osu-droid-utilities";
import { GuildMember, InteractionReplyOptions } from "discord.js";
import { MessageButtonCreator } from "@utils/creators/MessageButtonCreator";
import { PPCalculationMethod } from "@enums/utils/PPCalculationMethod";
Expand Down Expand Up @@ -121,23 +120,18 @@ export const run: MessageContextMenuCommand["run"] = async (_, interaction) => {
PPCalculationMethod.live,
);

const embed = await EmbedCreator.createRecentPlayEmbed(
score,
player instanceof Player
? player.avatarUrl
: DroidHelper.getAvatarURL(player.id),
(<GuildMember | null>interaction.member)?.displayColor,
scoreAttribs?.attributes,
undefined,
localization.language,
);

const options: InteractionReplyOptions = {
...(await EmbedCreator.createRecentPlayEmbed(
score,
(<GuildMember | null>interaction.member)?.displayColor,
scoreAttribs?.attributes,
undefined,
localization.language,
)),
content: MessageCreator.createAccept(
localization.getTranslation("comparePlayDisplay"),
player.username,
),
embeds: [embed],
};

const replay = await ReplayHelper.analyzeReplay(score);
Expand Down
22 changes: 16 additions & 6 deletions src/utils/creators/EmbedCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
underscore,
channelMention,
hyperlink,
AttachmentBuilder,
} from "discord.js";
import { Config } from "@core/Config";
import { BeatmapManager } from "@utils/managers/BeatmapManager";
Expand Down Expand Up @@ -514,7 +515,6 @@ export abstract class EmbedCreator {
>
| Score
| RecentPlay,
playerAvatarURL: string,
embedColor?: ColorResolvable,
droidAttribs?: CompleteCalculationAttributes<
DroidDifficultyAttributes,
Expand All @@ -525,7 +525,7 @@ export abstract class EmbedCreator {
OsuPerformanceAttributes
> | null,
language: Language = "en",
): Promise<EmbedBuilder> {
): Promise<BaseMessageOptions> {
const localization = this.getLocalization(language);
const BCP47 = LocaleHelper.convertToBCP47(language);
const arrow = Symbols.rightArrowSmall;
Expand All @@ -543,9 +543,19 @@ export abstract class EmbedCreator {
? score.completeModString
: DroidHelper.getCompleteModString(score.mode);

const avatar = await DroidHelper.getAvatar(score.uid);
const avatarURL = "attachment://avatar.png";

const options: BaseMessageOptions = {
embeds: [embed],
files: avatar
? [new AttachmentBuilder(avatar, { name: avatarURL })]
: [],
};

embed.setAuthor({
name: `${score instanceof Score || score instanceof RecentPlay ? score.title : DroidHelper.cleanupFilename(score.filename)} ${modString}`,
iconURL: playerAvatarURL,
iconURL: avatarURL,
});

if (droidAttribs === undefined && osuAttribs !== null) {
Expand Down Expand Up @@ -602,7 +612,7 @@ export abstract class EmbedCreator {
}/${accuracy.nmiss}]`;

embed.setDescription(beatmapInformation);
return embed;
return options;
}

const beatmap: MapInfo = (await BeatmapManager.getBeatmap(score.hash, {
Expand All @@ -616,7 +626,7 @@ export abstract class EmbedCreator {
} | ${osuAttribs.difficulty.starRating.toFixed(2)}${
Symbols.star
}]`,
iconURL: playerAvatarURL,
iconURL: avatarURL,
url: beatmap.beatmapLink,
})
.setThumbnail(
Expand Down Expand Up @@ -796,7 +806,7 @@ export abstract class EmbedCreator {

embed.setDescription(beatmapInformation);

return embed;
return options;
}

/**
Expand Down
17 changes: 9 additions & 8 deletions src/utils/creators/ProfileCardCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class ProfileCardCreator {
this.drawPlayerLevel();
}

await this.writePlayerProfile();
this.writePlayerProfile();
}

/**
Expand Down Expand Up @@ -215,12 +215,13 @@ export class ProfileCardCreator {
private async drawPlayerAvatar(): Promise<void> {
this.context.save();

const avatar = await loadImage(
this.player instanceof Player
? this.player.avatarUrl
: DroidHelper.getAvatarURL(this.player.id),
);
this.context.drawImage(avatar, 9, 9, 150, 150);
const avatar = await DroidHelper.getAvatar(this.player.id);

if (!avatar) {
return;
}

this.context.drawImage(await loadImage(avatar), 9, 9, 150, 150);

this.context.restore();
}
Expand Down Expand Up @@ -371,7 +372,7 @@ export class ProfileCardCreator {
/**
* Writes the details of the player's profile.
*/
private async writePlayerProfile(): Promise<void> {
private writePlayerProfile(): void {
this.context.save();

const x = 169;
Expand Down
30 changes: 30 additions & 0 deletions src/utils/helpers/DroidHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { DroidAPIRequestBuilder, ModUtil } from "@rian8337/osu-base";
import { APIScore, Player, Score } from "@rian8337/osu-droid-utilities";
import { RowDataPacket } from "mysql2";
import { OnlinePlayerRank } from "@structures/utils/OnlinePlayerRank";
import { readFile, stat } from "fs/promises";

/**
* A helper for osu!droid related requests.
Expand Down Expand Up @@ -552,6 +553,35 @@ export abstract class DroidHelper {
return `https://osudroid.moe/user/avatar?id=${uid}`;
}

/**
* Obtains the avatar of a player.
*
* In debug mode, the avatar will be obtained by requesting the osu!droid server.
* Otherwise, the avatar will be obtained via the file system.
*
* @param uid The uid of the player.
* @returns The avatar of the player, `null` if the avatar is not found or the osu!droid server cannot be requested.
*/
static async getAvatar(uid: number): Promise<Buffer | null> {
if (Config.isDebug) {
return fetch(this.getAvatarURL(uid))
.then((res) => res.arrayBuffer())
.then(Buffer.from)
.catch(() => null);
}

const avatarBasePath = "/DroidData/osudroid/zip/avatar/";
let avatarPath = `${avatarBasePath}${uid}.png`;

const avatarStats = await stat(avatarPath);

if (!avatarStats.isFile()) {
avatarPath = `${avatarBasePath}0.png`;
}

return readFile(avatarPath).catch(() => null);
}

/**
* Cleans up filenames received from the score table to a proper title.
*
Expand Down

0 comments on commit 38bbccc

Please sign in to comment.