-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better support for applications and their resources (#328)
* feat: stuff * fix: :b * chore: apply formatting * fix: lol --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
7849ad1
commit a677ec7
Showing
19 changed files
with
379 additions
and
105 deletions.
There are no files selected for viewing
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,44 +1,56 @@ | ||
import type { CacheFrom, ReturnCache } from '../..'; | ||
import type { ApplicationEmojiStructure, CacheFrom, ReturnCache } from '../..'; | ||
import { type GuildEmojiStructure, Transformers } from '../../client/transformers'; | ||
import { fakePromise } from '../../common'; | ||
import type { APIEmoji } from '../../types'; | ||
import type { APIApplicationEmoji, APIEmoji } from '../../types'; | ||
import { GuildRelatedResource } from './default/guild-related'; | ||
|
||
export class Emojis extends GuildRelatedResource<any, APIEmoji> { | ||
export class Emojis extends GuildRelatedResource<any, APIEmoji | APIApplicationEmoji> { | ||
namespace = 'emoji'; | ||
|
||
//@ts-expect-error | ||
filter(data: APIEmoji, id: string, guild_id: string, from: CacheFrom) { | ||
return true; | ||
} | ||
|
||
override get(id: string): ReturnCache<GuildEmojiStructure | undefined> { | ||
return fakePromise(super.get(id)).then(rawEmoji => | ||
rawEmoji ? Transformers.GuildEmoji(this.client, rawEmoji, rawEmoji.guild_id) : undefined, | ||
); | ||
override get(id: string): ReturnCache<GuildEmojiStructure | ApplicationEmojiStructure | undefined> { | ||
return fakePromise(super.get(id)).then(rawEmoji => { | ||
if (!rawEmoji) return undefined; | ||
if (rawEmoji.guild_id === this.client.applicationId) return Transformers.ApplicationEmoji(this.client, rawEmoji); | ||
return Transformers.GuildEmoji(this.client, rawEmoji, rawEmoji.guild_id); | ||
}); | ||
} | ||
|
||
raw(id: string): ReturnCache<APIEmoji | undefined> { | ||
return super.get(id); | ||
} | ||
|
||
override bulk(ids: string[]): ReturnCache<GuildEmojiStructure[]> { | ||
override bulk(ids: string[]): ReturnCache<(GuildEmojiStructure | ApplicationEmojiStructure)[]> { | ||
return fakePromise(super.bulk(ids) as (APIEmoji & { id: string; guild_id: string })[]).then(emojis => | ||
emojis.map(rawEmoji => Transformers.GuildEmoji(this.client, rawEmoji, rawEmoji.guild_id)), | ||
emojis.map(rawEmoji => { | ||
if (rawEmoji.guild_id === this.client.applicationId) | ||
return Transformers.ApplicationEmoji(this.client, rawEmoji as APIApplicationEmoji); | ||
return Transformers.GuildEmoji(this.client, rawEmoji, rawEmoji.guild_id); | ||
}), | ||
); | ||
} | ||
|
||
bulkRaw(ids: string[]): ReturnCache<(APIEmoji & { id: string; guild_id: string })[]> { | ||
return super.bulk(ids); | ||
} | ||
|
||
override values(guild: string): ReturnCache<GuildEmojiStructure[]> { | ||
override values(guild: string): ReturnCache<(GuildEmojiStructure | ApplicationEmojiStructure)[]> { | ||
return fakePromise(super.values(guild) as (APIEmoji & { id: string; guild_id: string })[]).then(emojis => | ||
emojis.map(rawEmoji => Transformers.GuildEmoji(this.client, rawEmoji, rawEmoji.guild_id)), | ||
emojis.map(rawEmoji => { | ||
if (rawEmoji.guild_id === this.client.applicationId) | ||
return Transformers.ApplicationEmoji(this.client, rawEmoji as APIApplicationEmoji); | ||
return Transformers.GuildEmoji(this.client, rawEmoji, rawEmoji.guild_id); | ||
}), | ||
); | ||
} | ||
|
||
valuesRaw(guild: string): ReturnCache<(APIEmoji & { id: string; guild_id: string })[]> { | ||
valuesRaw( | ||
guild: string, | ||
): ReturnCache<(((APIEmoji & { id: string }) | APIApplicationEmoji) & { guild_id: string })[]> { | ||
return super.values(guild); | ||
} | ||
} |
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
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,90 +1,141 @@ | ||
import { type EntitlementStructure, Transformers } from '../../client'; | ||
import { CacheFrom, resolveImage } from '../..'; | ||
import { type ApplicationEmojiStructure, type EntitlementStructure, Transformers } from '../../client'; | ||
import type { | ||
APIEntitlement, | ||
RESTGetAPIEntitlementsQuery, | ||
RESTPostAPIApplicationEmojiJSONBody, | ||
RESTPatchAPIApplicationEmojiJSONBody, | ||
RESTPatchCurrentApplicationJSONBody, | ||
RESTPostAPIEntitlementBody, | ||
} from '../../types'; | ||
import type { ApplicationEmojiResolvable } from '../types/resolvables'; | ||
import { BaseShorter } from './base'; | ||
|
||
export class ApplicationShorter extends BaseShorter { | ||
/** | ||
* Lists the emojis for the application. | ||
* @param applicationId The ID of the application. | ||
* @returns The emojis. | ||
*/ | ||
listEmojis(applicationId: string) { | ||
return this.client.proxy.applications(applicationId).emojis.get(); | ||
async listEmojis(force = false): Promise<ApplicationEmojiStructure[]> { | ||
if (!force) { | ||
const cached = (await this.client.cache.emojis?.values(this.client.applicationId)) as | ||
| ApplicationEmojiStructure[] | ||
| undefined; | ||
if (cached?.length) return cached; | ||
} | ||
const data = await this.client.proxy.applications(this.client.applicationId).emojis.get(); | ||
this.client.cache.emojis?.set( | ||
CacheFrom.Rest, | ||
data.items.map(e => [e.id, e]), | ||
this.client.applicationId, | ||
); | ||
return data.items.map(e => Transformers.ApplicationEmoji(this.client, e)); | ||
} | ||
/** | ||
* Gets an emoji for the application. | ||
* @param applicationId The ID of the application. | ||
* @param emojiId The ID of the emoji. | ||
* @returns The emoji. | ||
*/ | ||
getEmoji(applicationId: string, emojiId: string) { | ||
return this.client.proxy.applications(applicationId).emojis(emojiId).get(); | ||
async getEmoji(emojiId: string, force = false): Promise<ApplicationEmojiStructure> { | ||
if (!force) { | ||
const cached = (await this.client.cache.emojis?.get(emojiId)) as ApplicationEmojiStructure; | ||
if (cached) return cached; | ||
} | ||
const data = await this.client.proxy.applications(this.client.applicationId).emojis(emojiId).get(); | ||
this.client.cache.emojis?.set(CacheFrom.Rest, data.id, this.client.applicationId, data); | ||
return Transformers.ApplicationEmoji(this.client, data); | ||
} | ||
|
||
/** | ||
* Creates a new emoji for the application. | ||
* @param applicationId The ID of the application. | ||
* @param body.name The name of the emoji. | ||
* @param body.image The [image data string](https://discord.com/developers/docs/reference#image-data) of the emoji. | ||
* @returns The created emoji. | ||
*/ | ||
createEmoji(applicationId: string, body: RESTPostAPIApplicationEmojiJSONBody) { | ||
return this.client.proxy.applications(applicationId).emojis.post({ body }); | ||
async createEmoji(raw: ApplicationEmojiResolvable) { | ||
const data = await this.client.proxy | ||
.applications(this.client.applicationId) | ||
.emojis.post({ body: { ...raw, image: await resolveImage(raw.image) } }); | ||
this.client.cache.emojis?.set(CacheFrom.Rest, data.id, this.client.applicationId, data); | ||
return Transformers.ApplicationEmoji(this.client, data); | ||
} | ||
|
||
/** | ||
* Edits an emoji for the application. | ||
* @param emojiId The ID of the emoji. | ||
* @param body.name The new name of the emoji. | ||
* @returns The edited emoji. | ||
*/ | ||
async editEmoji(emojiId: string, body: RESTPatchAPIApplicationEmojiJSONBody) { | ||
const data = await this.client.proxy.applications(this.client.applicationId).emojis(emojiId).patch({ body }); | ||
this.client.cache.emojis?.patch(CacheFrom.Rest, emojiId, this.client.applicationId, data); | ||
return Transformers.ApplicationEmoji(this.client, data); | ||
} | ||
|
||
/** | ||
* Deletes an emoji for the application. | ||
* @param emojiId The ID of the emoji. | ||
*/ | ||
deleteEmoji(emojiId: string) { | ||
return this.client.proxy.applications(this.client.applicationId).emojis(emojiId).delete(); | ||
} | ||
|
||
/** | ||
* Lists the entitlements for the application. | ||
* @param applicationId The ID of the application. | ||
* @param [query] The query parameters. | ||
*/ | ||
listEntitlements(applicationId: string, query?: RESTGetAPIEntitlementsQuery): Promise<EntitlementStructure[]> { | ||
listEntitlements(query?: RESTGetAPIEntitlementsQuery): Promise<EntitlementStructure[]> { | ||
return this.client.proxy | ||
.applications(applicationId) | ||
.applications(this.client.applicationId) | ||
.entitlements.get({ query }) | ||
.then(et => et.map(e => Transformers.Entitlement(this.client, e))); | ||
} | ||
|
||
/** | ||
* Consumes an entitlement for the application. | ||
* @param applicationId The ID of the application. | ||
* @param entitlementId The ID of the entitlement. | ||
*/ | ||
consumeEntitlement(applicationId: string, entitlementId: string) { | ||
return this.client.proxy.applications(applicationId).entitlements(entitlementId).consume.post(); | ||
consumeEntitlement(entitlementId: string) { | ||
return this.client.proxy.applications(this.client.applicationId).entitlements(entitlementId).consume.post(); | ||
} | ||
|
||
/** | ||
* Creates a test entitlement for the application. | ||
* @param applicationId The ID of the application. | ||
* @param body The body of the request. | ||
*/ | ||
createTestEntitlement(applicationId: string, body: RESTPostAPIEntitlementBody): Promise<EntitlementStructure> { | ||
createTestEntitlement(body: RESTPostAPIEntitlementBody): Promise<EntitlementStructure> { | ||
return this.client.proxy | ||
.applications(applicationId) | ||
.applications(this.client.applicationId) | ||
.entitlements.post({ body }) | ||
.then(et => Transformers.Entitlement(this.client, et as APIEntitlement)); | ||
} | ||
|
||
/** | ||
* Deletes a test entitlement for the application. | ||
* @param applicationId The ID of the application. | ||
* @param entitlementId The ID of the entitlement. | ||
*/ | ||
deleteTestEntitlement(applicationId: string, entitlementId: string) { | ||
return this.client.proxy.applications(applicationId).entitlements(entitlementId).delete(); | ||
deleteTestEntitlement(entitlementId: string) { | ||
return this.client.proxy.applications(this.client.applicationId).entitlements(entitlementId).delete(); | ||
} | ||
|
||
/** | ||
* Lists the SKUs for the application. | ||
* @param applicationId The ID of the application. | ||
* @returns The SKUs. | ||
*/ | ||
listSKUs(applicationId: string) { | ||
return this.client.proxy.applications(applicationId).skus.get(); | ||
listSKUs() { | ||
return this.client.proxy.applications(this.client.applicationId).skus.get(); | ||
} | ||
|
||
async fetch() { | ||
const data = await this.client.proxy.applications('@me').get(); | ||
return Transformers.Application(this.client, data); | ||
} | ||
|
||
async edit(body: RESTPatchCurrentApplicationJSONBody) { | ||
const data = await this.client.proxy.applications('@me').patch({ body }); | ||
return Transformers.Application(this.client, data); | ||
} | ||
|
||
getActivityInstance(instanceId: string) { | ||
return this.client.proxy.applications(this.client.applicationId)['activity-instances'](instanceId).get(); | ||
} | ||
} |
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
Oops, something went wrong.