Add missing cdn functions, dynamic image format
This commit is contained in:
		
							parent
							
								
									d26059fdc8
								
							
						
					
					
						commit
						88519d9420
					
				
					 4 changed files with 82 additions and 8 deletions
				
			
		|  | @ -3,11 +3,13 @@ import { ImageFormats, ImageSize } from '../types/cdn.ts' | ||||||
| /** Function to get Image URL from a resource on Discord CDN */ | /** Function to get Image URL from a resource on Discord CDN */ | ||||||
| export const ImageURL = ( | export const ImageURL = ( | ||||||
|   url: string, |   url: string, | ||||||
|   format: ImageFormats | undefined = 'png', |   format: ImageFormats = 'png', | ||||||
|   size: ImageSize | undefined = 128 |   size: ImageSize = 128 | ||||||
| ): string => { | ): string => { | ||||||
|   size = size === undefined ? 128 : size |  | ||||||
|   if (url.includes('a_')) { |   if (url.includes('a_')) { | ||||||
|     return `${url}.${format === undefined ? 'gif' : format}?size=${size}` |     return `${url}.${format === 'dynamic' ? 'gif' : format}?size=${size}` | ||||||
|   } else return `${url}.${format === 'gif' ? 'png' : format}?size=${size}` |   } else | ||||||
|  |     return `${url}.${ | ||||||
|  |       format === 'gif' || format === 'dynamic' ? 'png' : format | ||||||
|  |     }?size=${size}` | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,8 +1,10 @@ | ||||||
| import { Client } from '../models/client.ts' | import { Client } from '../models/client.ts' | ||||||
|  | import { ImageSize } from '../types/cdn.ts' | ||||||
| import { EmojiPayload } from '../types/emoji.ts' | import { EmojiPayload } from '../types/emoji.ts' | ||||||
| import { EMOJI } from '../types/endpoint.ts' | import { CUSTOM_EMOJI, EMOJI } from '../types/endpoint.ts' | ||||||
| import { Snowflake } from '../utils/snowflake.ts' | import { Snowflake } from '../utils/snowflake.ts' | ||||||
| import { Base } from './base.ts' | import { Base } from './base.ts' | ||||||
|  | import { ImageURL } from './cdn.ts' | ||||||
| import { Guild } from './guild.ts' | import { Guild } from './guild.ts' | ||||||
| import { Role } from './role.ts' | import { Role } from './role.ts' | ||||||
| import { User } from './user.ts' | import { User } from './user.ts' | ||||||
|  | @ -54,6 +56,18 @@ export class Emoji extends Base { | ||||||
|     this.available = data.available |     this.available = data.available | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   /** | ||||||
|  |    * Gets emoji image URL | ||||||
|  |    */ | ||||||
|  |   emojiImageURL( | ||||||
|  |     format: 'png' | 'gif' | 'dynamic' = 'png', | ||||||
|  |     size: ImageSize = 512 | ||||||
|  |   ): string | undefined { | ||||||
|  |     return this.id != null | ||||||
|  |       ? `${ImageURL(CUSTOM_EMOJI(this.id), format, size)}` | ||||||
|  |       : undefined | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   /** Modify the given emoji. Requires the MANAGE_EMOJIS permission. Returns the updated emoji object on success. Fires a Guild Emojis Update Gateway event. */ |   /** Modify the given emoji. Requires the MANAGE_EMOJIS permission. Returns the updated emoji object on success. Fires a Guild Emojis Update Gateway event. */ | ||||||
|   async edit(data: ModifyGuildEmojiParams): Promise<Emoji> { |   async edit(data: ModifyGuildEmojiParams): Promise<Emoji> { | ||||||
|     if (this.id === null) throw new Error('Emoji ID is not valid.') |     if (this.id === null) throw new Error('Emoji ID is not valid.') | ||||||
|  |  | ||||||
|  | @ -32,9 +32,13 @@ import { User } from './user.ts' | ||||||
| import { Application } from './application.ts' | import { Application } from './application.ts' | ||||||
| import { | import { | ||||||
|   GUILD_BAN, |   GUILD_BAN, | ||||||
|  |   GUILD_BANNER, | ||||||
|   GUILD_BANS, |   GUILD_BANS, | ||||||
|  |   GUILD_DISCOVERY_SPLASH, | ||||||
|  |   GUILD_ICON, | ||||||
|   GUILD_INTEGRATIONS, |   GUILD_INTEGRATIONS, | ||||||
|   GUILD_PRUNE |   GUILD_PRUNE, | ||||||
|  |   GUILD_SPLASH | ||||||
| } from '../types/endpoint.ts' | } from '../types/endpoint.ts' | ||||||
| import { GuildVoiceStatesManager } from '../managers/guildVoiceStates.ts' | import { GuildVoiceStatesManager } from '../managers/guildVoiceStates.ts' | ||||||
| import { RequestMembersOptions } from '../gateway/index.ts' | import { RequestMembersOptions } from '../gateway/index.ts' | ||||||
|  | @ -42,6 +46,8 @@ import { GuildPresencesManager } from '../managers/presences.ts' | ||||||
| import { TemplatePayload } from '../types/template.ts' | import { TemplatePayload } from '../types/template.ts' | ||||||
| import { Template } from './template.ts' | import { Template } from './template.ts' | ||||||
| import { DiscordAPIError } from '../models/rest.ts' | import { DiscordAPIError } from '../models/rest.ts' | ||||||
|  | import { ImageFormats, ImageSize } from '../types/cdn.ts' | ||||||
|  | import { ImageURL } from './cdn.ts' | ||||||
| 
 | 
 | ||||||
| export class GuildBan extends Base { | export class GuildBan extends Base { | ||||||
|   guild: Guild |   guild: Guild | ||||||
|  | @ -258,6 +264,58 @@ export class Guild extends SnowflakeBase { | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   /** | ||||||
|  |    * Gets guild icon URL | ||||||
|  |    */ | ||||||
|  |   iconURL( | ||||||
|  |     format: ImageFormats = 'png', | ||||||
|  |     size: ImageSize = 512 | ||||||
|  |   ): string | undefined { | ||||||
|  |     return this.icon != null | ||||||
|  |       ? `${ImageURL(GUILD_ICON(this.id, this.icon), format, size)}` | ||||||
|  |       : undefined | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Gets guild splash URL | ||||||
|  |    */ | ||||||
|  |   splashURL( | ||||||
|  |     format: ImageFormats = 'png', | ||||||
|  |     size: ImageSize = 512 | ||||||
|  |   ): string | undefined { | ||||||
|  |     return this.splash != null | ||||||
|  |       ? `${ImageURL(GUILD_SPLASH(this.id, this.splash), format, size)}` | ||||||
|  |       : undefined | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Gets guild discover splash URL | ||||||
|  |    */ | ||||||
|  |   discoverSplashURL( | ||||||
|  |     format: ImageFormats = 'png', | ||||||
|  |     size: ImageSize = 512 | ||||||
|  |   ): string | undefined { | ||||||
|  |     return this.discoverySplash != null | ||||||
|  |       ? `${ImageURL( | ||||||
|  |           GUILD_DISCOVERY_SPLASH(this.id, this.discoverySplash), | ||||||
|  |           format, | ||||||
|  |           size | ||||||
|  |         )}` | ||||||
|  |       : undefined | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Gets guild banner URL | ||||||
|  |    */ | ||||||
|  |   bannerURL( | ||||||
|  |     format: ImageFormats = 'png', | ||||||
|  |     size: ImageSize = 512 | ||||||
|  |   ): string | undefined { | ||||||
|  |     return this.banner != null | ||||||
|  |       ? `${ImageURL(GUILD_BANNER(this.id, this.banner), format, size)}` | ||||||
|  |       : undefined | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   /** |   /** | ||||||
|    * Gets Everyone role of the Guild |    * Gets Everyone role of the Guild | ||||||
|    */ |    */ | ||||||
|  |  | ||||||
|  | @ -1,2 +1,2 @@ | ||||||
| export type ImageSize = 16 | 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | export type ImageSize = 16 | 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | ||||||
| export type ImageFormats = 'jpg' | 'jpeg' | 'png' | 'webp' | 'gif' | export type ImageFormats = 'jpg' | 'jpeg' | 'png' | 'webp' | 'gif' | 'dynamic' | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue