11import { formatUser , formatUserBold } from "#common/discord/format.ts" ;
22import { escapeMarkdown , makeMarkdownInlineCodeblock , makeMarkdownMultilineCodeblock , makeMarkdownQuote } from "#common/discord/markdown.ts" ;
3- import { DurationPresentationType , FormattingWrapper , GuildPresentationType , ParameterType , TimestampPresentationType , UserPresentationType , type GuildParameter , type ParameterRecord , type UserParameter } from "#common/template/index.ts" ;
3+ import { DurationPresentationType , FormattingWrapper , GuildPresentationType , ParameterType , RolePresentationType , TimestampPresentationType , UserPresentationType , type EntityParameter , type ParameterRecord , type UserParameter } from "#common/template/index.ts" ;
44import { TokenType , type Token } from "#common/template/parsing.ts" ;
55import { dateToUnixSeconds , humanizeDuration } from "#common/time.ts" ;
6+ import { INTERNAL_TYPE_INTEGRITY } from "#environment.ts" ;
67
78export function formatTokens ( params : ParameterRecord , tokens : Token [ ] ) : string {
89 let result = "" ;
@@ -22,23 +23,29 @@ export function formatTokens(params: ParameterRecord, tokens: Token[]): string {
2223
2324 switch ( token . valueType ) {
2425 case ParameterType . User :
25- if ( ! ( typeof value === "object"
26- && "id" in value && typeof value . id === "string"
27- && "tag" in value && typeof value . tag === "string" ) ) {
26+ if ( ! isUserParam ( value ) )
2827 throw new Error ( `params['${ token . parameter } '] is not a user!` ) ;
29- }
3028
3129 output = formatUserParam ( value , token . presentation , escaped ) ;
3230 break ;
3331 case ParameterType . Guild :
34- if ( ! ( typeof value === "object"
35- && "id" in value && typeof value . id === "string"
36- && "name" in value && typeof value . name === "string" ) ) {
37- throw new Error ( `params['${ token . parameter } '] is not a user!` ) ;
38- }
32+ if ( ! isEntityParam ( value ) )
33+ throw new Error ( `params['${ token . parameter } '] is not a guild!` ) ;
3934
4035 output = formatGuildParam ( value , token . presentation , escaped ) ;
4136 break ;
37+ case ParameterType . Role :
38+ if ( ! isEntityParam ( value ) )
39+ throw new Error ( `params['${ token . parameter } '] is not a role!` ) ;
40+
41+ output = formatRoleParam ( value , token . presentation , escaped ) ;
42+ break ;
43+ case ParameterType . Number :
44+ if ( typeof value !== "number" )
45+ throw new Error ( `params['${ token . parameter } '] is not a number!` ) ;
46+
47+ output = value . toString ( ) ;
48+ break ;
4249 case ParameterType . Duration :
4350 if ( typeof value !== "number" )
4451 throw new Error ( `params['${ token . parameter } '] is not a number!` ) ;
@@ -56,7 +63,10 @@ export function formatTokens(params: ParameterRecord, tokens: Token[]): string {
5663 if ( typeof value !== "string" )
5764 throw new Error ( `params['${ token . parameter } '] is not a string!` ) ;
5865
59- output = value ;
66+ if ( escaped && token . valueType === ParameterType . RawString )
67+ output = escapeMarkdown ( value ) ;
68+ else
69+ output = value ;
6070 break ;
6171 }
6272 } else
@@ -78,7 +88,24 @@ export function formatTokens(params: ParameterRecord, tokens: Token[]): string {
7888 }
7989
8090 return result ;
91+ }
8192
93+ function isUserParam ( value : { } | undefined ) : value is UserParameter {
94+ if ( ! INTERNAL_TYPE_INTEGRITY )
95+ return true ;
96+
97+ return typeof value === "object"
98+ && "id" in value && typeof value . id === "string"
99+ && "tag" in value && typeof value . tag === "string" ;
100+ }
101+
102+ function isEntityParam ( value : { } | undefined ) : value is EntityParameter {
103+ if ( ! INTERNAL_TYPE_INTEGRITY )
104+ return true ;
105+
106+ return typeof value === "object"
107+ && "id" in value && typeof value . id === "string"
108+ && "name" in value && typeof value . name === "string" ;
82109}
83110
84111function formatUserParam ( user : UserParameter , presentation : UserPresentationType , escaped : boolean ) : string {
@@ -100,7 +127,7 @@ function formatUserParam(user: UserParameter, presentation: UserPresentationType
100127 }
101128}
102129
103- function formatGuildParam ( guild : GuildParameter , presentation : GuildPresentationType , escaped : boolean ) : string {
130+ function formatGuildParam ( guild : EntityParameter , presentation : GuildPresentationType , escaped : boolean ) : string {
104131 switch ( presentation ) {
105132 case GuildPresentationType . Name :
106133 return escaped ? escapeMarkdown ( guild . name ) : guild . name ;
@@ -113,6 +140,21 @@ function formatGuildParam(guild: GuildParameter, presentation: GuildPresentation
113140 }
114141}
115142
143+ function formatRoleParam ( role : EntityParameter , presentation : RolePresentationType , escaped : boolean ) : string {
144+ switch ( presentation ) {
145+ case RolePresentationType . Name :
146+ return escaped ? escapeMarkdown ( role . name ) : role . name ;
147+ case RolePresentationType . Mention :
148+ return `<@&${ role . id } >` ;
149+ case RolePresentationType . NameMention :
150+ return `${ escapeMarkdown ( role . name ) } (<@&${ role . id } >)` ;
151+ case RolePresentationType . NameMentionBold :
152+ return `**${ escapeMarkdown ( role . name ) } (<@&${ role . id } >)**` ;
153+ case RolePresentationType . ID :
154+ return role . id ;
155+ }
156+ }
157+
116158function formatDurationParam ( duration : number , presentation : DurationPresentationType ) : string {
117159 switch ( presentation ) {
118160 case DurationPresentationType . Readable :
0 commit comments