-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
132 lines (118 loc) · 3.36 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { Api } from "https://deno.land/x/[email protected]/mod.ts";
import {
EditMessageParams,
SendMessageParams,
} from "https://deno.land/x/[email protected]/client/types.ts";
const entities = {
"MessageEntityBold": Api.MessageEntityBold,
"MessageEntityCode": Api.MessageEntityCode,
"MessageEntityItalic": Api.MessageEntityItalic,
"MessageEntityTextUrl": Api.MessageEntityTextUrl,
"MessageEntityPre": Api.MessageEntityPre,
"MessageEntitySpoiler": Api.MessageEntitySpoiler,
"MessageEntityStrike": Api.MessageEntityStrike,
"MessageEntityUnderline": Api.MessageEntityUnderline,
};
export interface Stringable {
toString(): string;
}
class FormattedString implements Stringable {
text: string;
entities: Api.TypeMessageEntity[];
constructor(text: string, entities: Api.TypeMessageEntity[]) {
this.text = text;
this.entities = entities;
}
toString() {
return this.text;
}
get send(): SendMessageParams {
return {
message: this.text,
formattingEntities: this.entities,
};
}
get edit(): Omit<EditMessageParams, "message"> {
return {
text: this.text,
formattingEntities: this.entities,
};
}
}
const unwrap = (stringLike: Stringable): FormattedString => {
if (stringLike instanceof FormattedString) {
return stringLike;
}
return new FormattedString(stringLike.toString(), []);
};
// deno-lint-ignore no-explicit-any
const buildFormatter = <T extends Array<any> = never>(
type: keyof typeof entities,
...formatArgKeys: T
) => {
return (stringLike: Stringable, ...formatArgs: T) => {
const formattedString = unwrap(stringLike);
const formatArgObj = Object.fromEntries(
formatArgKeys.map((formatArgKey, i) => [formatArgKey, formatArgs[i]]),
);
return new FormattedString(
formattedString.text,
[
new entities[type]({
offset: 0,
length: formattedString.text.length,
...formatArgObj,
}),
...formattedString.entities,
],
);
};
};
// Native entity functions
const bold = buildFormatter("MessageEntityBold");
const code = buildFormatter("MessageEntityCode");
const italic = buildFormatter("MessageEntityItalic");
const link = buildFormatter<[string]>("MessageEntityTextUrl", "url");
const pre = buildFormatter<[string]>("MessageEntityPre", "language");
const spoiler = buildFormatter("MessageEntitySpoiler");
const strikethrough = buildFormatter("MessageEntitySpoiler");
const underline = buildFormatter("MessageEntityUnderline");
// Utility functions
const mentionUser = (stringLike: Stringable, userId: number) => {
return link(stringLike, `tg://user?id=${userId}`);
};
// Root format function
const fmt = (
rawStringParts: TemplateStringsArray | string[],
...stringLikes: Stringable[]
): FormattedString => {
let text = rawStringParts[0];
const entities = new Array<Api.TypeMessageEntity>();
for (let i = 0; i < stringLikes.length; i++) {
const stringLike = stringLikes[i];
if (stringLike instanceof FormattedString) {
entities.push(
...stringLike.entities.map((e) => {
e.offset += text.length;
return e;
}),
);
}
text += stringLike.toString();
text += rawStringParts[i + 1];
}
return new FormattedString(text, entities);
};
export {
bold,
code,
fmt,
FormattedString,
italic,
link,
mentionUser,
pre,
spoiler,
strikethrough,
underline,
};