(fields: CreateMessageFields)
| 212 | * @internal |
| 213 | */ |
| 214 | export function toOpMessageFields(fields: CreateMessageFields): Internal.OpCreateMessageFields { |
| 215 | let allowedMentions: Internal.AllowedMentions; |
| 216 | if (fields.allowedMentions) { |
| 217 | allowedMentions = { |
| 218 | parse: fields.allowedMentions.parse, |
| 219 | users: fields.allowedMentions.users ?? [], |
| 220 | roles: fields.allowedMentions.roles ?? [], |
| 221 | repliedUser: fields.allowedMentions.repliedUser ?? false, |
| 222 | } |
| 223 | } else { |
| 224 | allowedMentions = { |
| 225 | parse: ["Users"], |
| 226 | users: [], |
| 227 | roles: [], |
| 228 | repliedUser: false, |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | const userProvidedIds = (fields.attachments ?? []).map(v => v.id).filter(v => v !== undefined) as number[] |
| 233 | let idIncrementer = 0 |
| 234 | // Provides automatic id generation for attachments, avoiding collisions with user provided ids |
| 235 | const nextAutoId = () => { |
| 236 | while (true) { |
| 237 | idIncrementer++ |
| 238 | |
| 239 | let next = idIncrementer |
| 240 | if (userProvidedIds.includes(next)) { |
| 241 | continue |
| 242 | } |
| 243 | |
| 244 | return next |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | const attachments: Internal.OpCreateMessageFields["attachments"] = (fields.attachments ?? []).map(a => ({ |
| 249 | dataB64: typeof a.data === "string" |
| 250 | ? base64Encode(encodeText(a.data)) |
| 251 | : base64Encode(new Uint8Array(a.data)), |
| 252 | |
| 253 | description: a.description ?? null, |
| 254 | filename: a.filename, |
| 255 | id: a.id ?? nextAutoId(), |
| 256 | })) |
| 257 | |
| 258 | return { |
| 259 | ...fields, |
| 260 | allowedMentions: allowedMentions!, |
| 261 | attachments, |
| 262 | flags: fields.flags ?? {} |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | export type MentionParseTypes = "Everyone" | "Roles" | "Users"; |
| 267 |
no test coverage detected