Implement basic caching

This commit is contained in:
2023-04-23 14:23:20 +07:00
parent 89c5b2fa8b
commit 59554b9fd8
3 changed files with 181 additions and 81 deletions
+40 -36
View File
@@ -38,20 +38,20 @@ const EMBEDS = {
export default class VRChatWorld {
public static async run(data: HybridInteractionMessage, args: any) {
let world;
let query;
if (data.isMessage()) {
if (typeof args[1] === 'undefined')
return await sendHybridInteractionMessageResponse(data, {
embeds: [EMBEDS.NO_WORLD_MENTIONED(data)]
});
const [removed, ...newArgs] = args;
world = newArgs.join(' ');
} else if (data.isApplicationCommand()) world = data.getSlashCommand().options.get('world')?.value?.toString();
query = newArgs.join(' ');
} else if (data.isApplicationCommand()) query = data.getSlashCommand().options.get('world')?.value?.toString();
let result;
try {
if (world.startsWith('wrld_')) result = await VRChatAPI.client!.WorldsApi.getWorld(world);
if (query.startsWith('wrld_')) result = await VRChatAPI.getCachedWorldById(query);
else {
let search_result = await VRChatAPI.client!.WorldsApi.searchWorlds(
undefined,
@@ -61,7 +61,7 @@ export default class VRChatWorld {
100,
undefined, //VRChat.OrderOption.Descending,
undefined,
world
query
);
if (search_result.data.length === 0)
return await sendHybridInteractionMessageResponse(data, {
@@ -70,14 +70,14 @@ export default class VRChatWorld {
let foundExact = false;
for (let i = 0; i < search_result.data.length; i++) {
if (search_result.data[i].name.toLowerCase() === world.toLowerCase()) {
result = await VRChatAPI.client!.WorldsApi.getWorld(search_result.data[i].id);
if (search_result.data[i].name.toLowerCase() === query.toLowerCase()) {
result = await VRChatAPI.getCachedWorldById(search_result.data[i].id);
foundExact = true;
break;
}
}
if (!foundExact) result = await VRChatAPI.client!.WorldsApi.getWorld(search_result.data[0].id);
if (!foundExact) result = await VRChatAPI.getCachedWorldById(search_result.data[0].id);
}
} catch (err: any) {
if (err.response?.status === 404)
@@ -99,76 +99,80 @@ export default class VRChatWorld {
await data.getSlashCommand().deferReply();
}
let author = await VRChatAPI.client!.UsersApi.getUser(result.data.authorId);
let author = await VRChatAPI.getCachedUserById(result.authorId);
if (!author)
return await sendHybridInteractionMessageResponse(data, {
embeds: [EMBEDS.ERROR(data)]
});
const embed = makeInfoEmbed({
icon: null,
title: `**${result.data.name}**`,
description: `Created by **[${result.data.authorName}](https://vrchat.com/home/user/${author.data.id})**\n\u200b`,
title: `**${result.name}**`,
description: `Created by **[${result.authorName}](https://vrchat.com/home/user/${author.id})**\n\u200b`,
fields: [
{
name: '🌎 World Description',
value: `${result.data.description}\n\u200b`,
value: `${result.description}\n\u200b`,
inline: false
},
{
name: `${this.getReleaseStatusEmoji(result.data)} Visiblity`,
value: `${this.getReleaseStatus(result.data)}\n\u200b`,
name: `${this.getReleaseStatusEmoji(result)} Visiblity`,
value: `${this.getReleaseStatus(result)}\n\u200b`,
inline: true
},
{
name: '✨ Visits',
value: `${this.numberWithCommas(result.data.visits)}\n\u200b`,
value: `${this.numberWithCommas(result.visits)}\n\u200b`,
inline: true
},
{
name: '💕 Favorites',
value: `${result.data.favorites ? this.numberWithCommas(result.data.favorites) : 0}\n\u200b`,
value: `${result.favorites ? this.numberWithCommas(result.favorites) : 0}\n\u200b`,
inline: true
},
{
name: '👥 Players',
value: `${result.data.occupants ? this.numberWithCommas(result.data.occupants) : 0} ${
result.data.instances
? `(${this.numberWithCommas(
result.data.publicOccupants || 0
)} public, ${this.numberWithCommas(result.data.privateOccupants || 0)} private)`
value: `${result.occupants ? this.numberWithCommas(result.occupants) : 0} ${
result.instances
? `(${this.numberWithCommas(result.publicOccupants || 0)} public, ${this.numberWithCommas(
result.privateOccupants || 0
)} private)`
: ''
}\n\u200b`,
inline: true
},
{
name: '💖 Popularity',
value: `${this.numberWithCommas(result.data.popularity)}\n\u200b`,
value: `${this.numberWithCommas(result.popularity)}\n\u200b`,
inline: true
},
{
name: '🔥 Heat',
value: `${result.data.heat}\n\u200b`,
value: `${result.heat}\n\u200b`,
inline: true
},
{
name: '📥 Capacity',
value: `${result.data.capacity}\n\u200b`,
value: `${result.capacity}\n\u200b`,
inline: true
},
{
name: '📅 Last updated',
value: `<t:${Math.round(new Date(result.data.updated_at).getTime() / 1000)}:R>\n<t:${Math.round(
new Date(result.data.updated_at).getTime() / 1000
value: `<t:${Math.round(new Date(result.updated_at).getTime() / 1000)}:R>\n<t:${Math.round(
new Date(result.updated_at).getTime() / 1000
)}:f>\n\u200b`,
inline: true
},
{
name: '📅 Created at',
value: `<t:${Math.round(new Date(result.data.created_at).getTime() / 1000)}:R>\n<t:${Math.round(
new Date(result.data.created_at).getTime() / 1000
value: `<t:${Math.round(new Date(result.created_at).getTime() / 1000)}:R>\n<t:${Math.round(
new Date(result.created_at).getTime() / 1000
)}:f>\n\u200b`,
inline: true
},
{
name: `❤ World Information`,
value: `World ID: \`\`${result.data.id}\`\`\nCreator ID: \`\`${result.data.authorId}\`\`\n`
value: `World ID: \`\`${result.id}\`\`\nCreator ID: \`\`${result.authorId}\`\`\n`
}
],
user: data.getUser()
@@ -176,14 +180,14 @@ export default class VRChatWorld {
embed.setAuthor({
name: `VRChat World`,
url: `https://vrchat.com/home/world/${result.data.id}`,
url: `https://vrchat.com/home/world/${result.id}`,
iconURL:
'https://www.theladders.com/s3proxy/company-photo.theladders.com/68949/5721f0dd-d662-4b5e-9c0b-236b8a41b0d3.png'
});
let mainImage;
if (result.data.thumbnailImageUrl) mainImage = result.data.thumbnailImageUrl;
if (result.thumbnailImageUrl) mainImage = result.thumbnailImageUrl;
if (mainImage)
embed.setImage(
@@ -193,8 +197,8 @@ export default class VRChatWorld {
let userMainImage;
if (author.data.userIcon) userMainImage = author.data.userIcon;
else if (author.data.currentAvatarImageUrl) userMainImage = author.data.currentAvatarImageUrl;
if (author.userIcon) userMainImage = author.userIcon;
else if (author.currentAvatarImageUrl) userMainImage = author.currentAvatarImageUrl;
if (userMainImage)
embed.setThumbnail(
@@ -206,12 +210,12 @@ export default class VRChatWorld {
new ButtonBuilder()
.setEmoji('🔗')
.setLabel(' Open World')
.setURL(`https://vrchat.com/home/world/${result.data.id}`)
.setURL(`https://vrchat.com/home/world/${result.id}`)
.setStyle(ButtonStyle.Link),
new ButtonBuilder()
.setEmoji('🔗')
.setLabel(` ${author.data.displayName}'s Profile`)
.setURL(`https://vrchat.com/home/user/${author.data.id}`)
.setLabel(` ${author.displayName}'s Profile`)
.setURL(`https://vrchat.com/home/user/${author.id}`)
.setStyle(ButtonStyle.Link)
]);