1
- import { mainuserjson } from "./jsontypes.js" ;
2
- import { Localuser } from "./localuser.js" ;
3
- import { MarkDown } from "./markdown.js" ;
4
- import { Form , Settings } from "./settings.js" ;
5
- import { User } from "./user.js" ;
1
+ import { mainuserjson } from "./jsontypes.js" ;
2
+ import { Localuser } from "./localuser.js" ;
3
+ import { MarkDown } from "./markdown.js" ;
4
+ import { Form , Settings } from "./settings.js" ;
5
+ import { User } from "./user.js" ;
6
6
import { guildjson } from "./jsontypes.js" ;
7
- import { PermissionToggle } from "./role.js" ;
8
- import { Permissions } from "./permissions.js" ;
9
- import { I18n } from "./i18n.js" ;
10
- class Bot {
11
- readonly owner :Localuser ;
12
- readonly token :string ;
13
- readonly json :mainuserjson ;
14
- headers : { "Content-type" : string ; Authorization : string } ;
15
- get localuser ( ) {
7
+ import { PermissionToggle } from "./role.js" ;
8
+ import { Permissions } from "./permissions.js" ;
9
+ import { I18n } from "./i18n.js" ;
10
+ class Bot {
11
+ readonly owner : Localuser ;
12
+ readonly token : string ;
13
+ readonly json : mainuserjson ;
14
+ headers : { "Content-type" : string ; Authorization : string } ;
15
+ get localuser ( ) {
16
16
return this . owner ;
17
17
}
18
- get info ( ) {
18
+ get info ( ) {
19
19
return this . localuser . info ;
20
20
}
21
- constructor ( json :mainuserjson , token :string , owner :Localuser ) {
22
- this . owner = owner ;
23
- this . token = token ;
24
- this . json = json ;
25
- this . headers = {
21
+ constructor ( json : mainuserjson , token : string , owner : Localuser ) {
22
+ this . owner = owner ;
23
+ this . token = token ;
24
+ this . json = json ;
25
+ this . headers = {
26
26
"Content-type" : "application/json; charset=UTF-8" ,
27
27
Authorization : token ,
28
28
} ;
29
29
}
30
- settings ( ) {
30
+ settings ( ) {
31
31
const settings = new Settings ( I18n . getTranslation ( "botSettings" ) ) ;
32
- const botOptions = settings . addButton ( "Profile" , { ltr :true } ) ;
33
- const bot = new User ( this . json , this . localuser ) ;
32
+ const botOptions = settings . addButton ( "Profile" , { ltr : true } ) ;
33
+ const bot = new User ( this . json , this . localuser ) ;
34
34
{
35
35
const hypotheticalProfile = document . createElement ( "div" ) ;
36
36
@@ -39,7 +39,7 @@ class Bot{
39
39
let newbio : string | undefined ;
40
40
const hypouser = bot . clone ( ) ;
41
41
let color : string ;
42
- async function regen ( ) {
42
+ async function regen ( ) {
43
43
hypotheticalProfile . textContent = "" ;
44
44
const hypoprofile = await hypouser . buildprofile ( - 1 , - 1 ) ;
45
45
@@ -52,22 +52,22 @@ class Bot{
52
52
53
53
const finput = settingsLeft . addFileInput (
54
54
I18n . getTranslation ( "uploadPfp" ) ,
55
- _ => {
56
- if ( file ) {
55
+ ( _ ) => {
56
+ if ( file ) {
57
57
this . updatepfp ( file ) ;
58
58
}
59
59
} ,
60
- { clear : true }
60
+ { clear : true } ,
61
61
) ;
62
- finput . watchForChange ( _ => {
63
- if ( ! _ ) {
62
+ finput . watchForChange ( ( _ ) => {
63
+ if ( ! _ ) {
64
64
file = null ;
65
65
hypouser . avatar = null ;
66
66
hypouser . hypotheticalpfp = true ;
67
67
regen ( ) ;
68
68
return ;
69
69
}
70
- if ( _ . length ) {
70
+ if ( _ . length ) {
71
71
file = _ [ 0 ] ;
72
72
const blob = URL . createObjectURL ( file ) ;
73
73
hypouser . avatar = blob ;
@@ -78,22 +78,22 @@ class Bot{
78
78
let bfile : undefined | File | null ;
79
79
const binput = settingsLeft . addFileInput (
80
80
I18n . getTranslation ( "uploadBanner" ) ,
81
- _ => {
82
- if ( bfile !== undefined ) {
81
+ ( _ ) => {
82
+ if ( bfile !== undefined ) {
83
83
this . updatebanner ( bfile ) ;
84
84
}
85
85
} ,
86
- { clear : true }
86
+ { clear : true } ,
87
87
) ;
88
- binput . watchForChange ( _ => {
89
- if ( ! _ ) {
88
+ binput . watchForChange ( ( _ ) => {
89
+ if ( ! _ ) {
90
90
bfile = null ;
91
91
hypouser . banner = undefined ;
92
92
hypouser . hypotheticalbanner = true ;
93
93
regen ( ) ;
94
94
return ;
95
95
}
96
- if ( _ . length ) {
96
+ if ( _ . length ) {
97
97
bfile = _ [ 0 ] ;
98
98
const blob = URL . createObjectURL ( bfile ) ;
99
99
hypouser . banner = blob ;
@@ -104,42 +104,42 @@ class Bot{
104
104
let changed = false ;
105
105
const pronounbox = settingsLeft . addTextInput (
106
106
I18n . getTranslation ( "pronouns" ) ,
107
- _ => {
108
- if ( newpronouns || newbio || changed ) {
107
+ ( _ ) => {
108
+ if ( newpronouns || newbio || changed ) {
109
109
this . updateProfile ( {
110
110
pronouns : newpronouns ,
111
111
bio : newbio ,
112
112
accent_color : Number . parseInt ( "0x" + color . substr ( 1 ) , 16 ) ,
113
113
} ) ;
114
114
}
115
115
} ,
116
- { initText : bot . pronouns }
116
+ { initText : bot . pronouns } ,
117
117
) ;
118
- pronounbox . watchForChange ( _ => {
118
+ pronounbox . watchForChange ( ( _ ) => {
119
119
hypouser . pronouns = _ ;
120
120
newpronouns = _ ;
121
121
regen ( ) ;
122
122
} ) ;
123
- const bioBox = settingsLeft . addMDInput ( I18n . getTranslation ( "bio" ) , _ => { } , {
123
+ const bioBox = settingsLeft . addMDInput ( I18n . getTranslation ( "bio" ) , ( _ ) => { } , {
124
124
initText : bot . bio . rawString ,
125
125
} ) ;
126
- bioBox . watchForChange ( _ => {
126
+ bioBox . watchForChange ( ( _ ) => {
127
127
newbio = _ ;
128
128
hypouser . bio = new MarkDown ( _ , this . owner ) ;
129
129
regen ( ) ;
130
130
} ) ;
131
131
132
- if ( bot . accent_color ) {
132
+ if ( bot . accent_color ) {
133
133
color = "#" + bot . accent_color . toString ( 16 ) ;
134
- } else {
134
+ } else {
135
135
color = "transparent" ;
136
136
}
137
137
const colorPicker = settingsLeft . addColorInput (
138
138
I18n . getTranslation ( "profileColor" ) ,
139
- _ => { } ,
140
- { initColor : color }
139
+ ( _ ) => { } ,
140
+ { initColor : color } ,
141
141
) ;
142
- colorPicker . watchForChange ( _ => {
142
+ colorPicker . watchForChange ( ( _ ) => {
143
143
console . log ( ) ;
144
144
color = _ ;
145
145
hypouser . accent_color = Number . parseInt ( "0x" + _ . substr ( 1 ) , 16 ) ;
@@ -148,65 +148,71 @@ class Bot{
148
148
} ) ;
149
149
}
150
150
{
151
- const guildsettings = settings . addButton ( "Guilds" ) ;
151
+ const guildsettings = settings . addButton ( "Guilds" ) ;
152
152
guildsettings . addTitle ( I18n . getTranslation ( "botGuilds" ) ) ;
153
- fetch ( this . info . api + "/users/@me/guilds/" , {
154
- headers :this . headers
155
- } ) . then ( _ => _ . json ( ) ) . then ( ( json :( guildjson [ "properties" ] ) [ ] ) => {
156
- for ( const guild of json ) {
157
- const content = document . createElement ( "div" ) ;
158
- content . classList . add ( "discovery-guild" ) ;
159
-
160
- if ( guild . banner ) {
161
- const banner = document . createElement ( "img" ) ;
162
- banner . classList . add ( "banner" ) ;
163
- banner . crossOrigin = "anonymous" ;
164
- banner . src = this . info . cdn + "/icons/" + guild . id + "/" + guild . banner + ".png?size=256" ;
165
- banner . alt = "" ;
166
- content . appendChild ( banner ) ;
167
- }
153
+ fetch ( this . info . api + "/users/@me/guilds/" , {
154
+ headers : this . headers ,
155
+ } )
156
+ . then ( ( _ ) => _ . json ( ) )
157
+ . then ( ( json : guildjson [ "properties" ] [ ] ) => {
158
+ for ( const guild of json ) {
159
+ const content = document . createElement ( "div" ) ;
160
+ content . classList . add ( "discovery-guild" ) ;
168
161
169
- const nameContainer = document . createElement ( "div" ) ;
170
- nameContainer . classList . add ( "flex" ) ;
171
- const img = document . createElement ( "img" ) ;
172
- img . classList . add ( "icon" ) ;
173
- img . crossOrigin = "anonymous" ;
174
- img . src = this . info . cdn + ( guild . icon ? "/icons/" + guild . id + "/" + guild . icon + ".png?size=48" : "/embed/avatars/3.png" ) ;
175
- img . alt = "" ;
176
- nameContainer . appendChild ( img ) ;
162
+ if ( guild . banner ) {
163
+ const banner = document . createElement ( "img" ) ;
164
+ banner . classList . add ( "banner" ) ;
165
+ banner . crossOrigin = "anonymous" ;
166
+ banner . src =
167
+ this . info . cdn + "/icons/" + guild . id + "/" + guild . banner + ".png?size=256" ;
168
+ banner . alt = "" ;
169
+ content . appendChild ( banner ) ;
170
+ }
177
171
178
- const name = document . createElement ( "h3" ) ;
179
- name . textContent = guild . name ;
180
- nameContainer . appendChild ( name ) ;
181
- content . appendChild ( nameContainer ) ;
182
- const desc = document . createElement ( "p" ) ;
183
- desc . textContent = guild . description ;
184
- content . appendChild ( desc ) ;
172
+ const nameContainer = document . createElement ( "div" ) ;
173
+ nameContainer . classList . add ( "flex" ) ;
174
+ const img = document . createElement ( "img" ) ;
175
+ img . classList . add ( "icon" ) ;
176
+ img . crossOrigin = "anonymous" ;
177
+ img . src =
178
+ this . info . cdn +
179
+ ( guild . icon
180
+ ? "/icons/" + guild . id + "/" + guild . icon + ".png?size=48"
181
+ : "/embed/avatars/3.png" ) ;
182
+ img . alt = "" ;
183
+ nameContainer . appendChild ( img ) ;
185
184
185
+ const name = document . createElement ( "h3" ) ;
186
+ name . textContent = guild . name ;
187
+ nameContainer . appendChild ( name ) ;
188
+ content . appendChild ( nameContainer ) ;
189
+ const desc = document . createElement ( "p" ) ;
190
+ desc . textContent = guild . description ;
191
+ content . appendChild ( desc ) ;
186
192
187
- guildsettings . addHTMLArea ( content ) ;
188
- content . onclick = ( ) => {
189
- const guildsetting = guildsettings . addSubOptions ( guild . name ) ;
190
- guildsetting . addHTMLArea ( content ) ;
191
- guildsetting . addButtonInput ( "" , I18n . getTranslation ( "leaveGuild" ) , ( ) => {
192
- if ( confirm ( I18n . getTranslation ( "confirmGuildLeave" , guild . name ) ) ) {
193
- fetch ( this . info . api + "/users/@me/guilds/" + guild . id , {
194
- method :"DELETE" ,
195
- headers :this . headers
196
- } )
197
- }
198
- } )
193
+ guildsettings . addHTMLArea ( content ) ;
194
+ content . onclick = ( ) => {
195
+ const guildsetting = guildsettings . addSubOptions ( guild . name ) ;
196
+ guildsetting . addHTMLArea ( content ) ;
197
+ guildsetting . addButtonInput ( "" , I18n . getTranslation ( "leaveGuild" ) , ( ) => {
198
+ if ( confirm ( I18n . getTranslation ( "confirmGuildLeave" , guild . name ) ) ) {
199
+ fetch ( this . info . api + "/users/@me/guilds/" + guild . id , {
200
+ method : "DELETE" ,
201
+ headers : this . headers ,
202
+ } ) ;
203
+ }
204
+ } ) ;
205
+ } ;
199
206
}
200
- }
201
- } )
207
+ } ) ;
202
208
}
203
209
settings . show ( ) ;
204
210
}
205
211
206
- updatepfp ( file : Blob ) : void {
212
+ updatepfp ( file : Blob ) : void {
207
213
const reader = new FileReader ( ) ;
208
214
reader . readAsDataURL ( file ) ;
209
- reader . onload = ( ) => {
215
+ reader . onload = ( ) => {
210
216
fetch ( this . info . api + "/users/@me" , {
211
217
method : "PATCH" ,
212
218
headers : this . headers ,
@@ -216,11 +222,11 @@ class Bot{
216
222
} ) ;
217
223
} ;
218
224
}
219
- updatebanner ( file : Blob | null ) : void {
220
- if ( file ) {
225
+ updatebanner ( file : Blob | null ) : void {
226
+ if ( file ) {
221
227
const reader = new FileReader ( ) ;
222
228
reader . readAsDataURL ( file ) ;
223
- reader . onload = ( ) => {
229
+ reader . onload = ( ) => {
224
230
fetch ( this . info . api + "/users/@me" , {
225
231
method : "PATCH" ,
226
232
headers : this . headers ,
@@ -229,7 +235,7 @@ class Bot{
229
235
} ) ,
230
236
} ) ;
231
237
} ;
232
- } else {
238
+ } else {
233
239
fetch ( this . info . api + "/users/@me" , {
234
240
method : "PATCH" ,
235
241
headers : this . headers ,
@@ -239,40 +245,39 @@ class Bot{
239
245
} ) ;
240
246
}
241
247
}
242
- updateProfile ( json : {
243
- bio ?: string ;
244
- pronouns ?: string ;
245
- accent_color ?: number ;
246
- } ) {
248
+ updateProfile ( json : { bio ?: string ; pronouns ?: string ; accent_color ?: number } ) {
247
249
fetch ( this . info . api + "/users/@me/profile" , {
248
250
method : "PATCH" ,
249
251
headers : this . headers ,
250
252
body : JSON . stringify ( json ) ,
251
253
} ) ;
252
254
}
253
- static InviteMaker ( id :string , container :Form , info :Localuser [ "info" ] ) {
254
- const gen = container . addSubOptions ( I18n . getTranslation ( "UrlGen" ) , {
255
- noSubmit :true
255
+ static InviteMaker ( id : string , container : Form , info : Localuser [ "info" ] ) {
256
+ const gen = container . addSubOptions ( I18n . getTranslation ( "UrlGen" ) , {
257
+ noSubmit : true ,
256
258
} ) ;
257
259
const params = new URLSearchParams ( "" ) ;
258
260
params . set ( "instance" , info . wellknown ) ;
259
261
params . set ( "client_id" , id ) ;
260
262
params . set ( "scope" , "bot" ) ;
261
- const url = gen . addText ( "" ) ;
262
- const perms = new Permissions ( "0" ) ;
263
- for ( const perm of Permissions . info ( ) ) {
264
- const permsisions = new PermissionToggle ( perm , perms , gen ) ;
263
+ const url = gen . addText ( "" ) ;
264
+ const perms = new Permissions ( "0" ) ;
265
+ for ( const perm of Permissions . info ( ) ) {
266
+ const permsisions = new PermissionToggle ( perm , perms , gen ) ;
265
267
gen . options . push ( permsisions ) ;
266
268
gen . generate ( permsisions ) ;
267
269
}
268
- const cancel = setInterval ( ( ) => {
269
- if ( ! gen . container . deref ( ) ) {
270
+ const cancel = setInterval ( ( ) => {
271
+ if ( ! gen . container . deref ( ) ) {
270
272
clearInterval ( cancel ) ;
271
273
}
272
- params . set ( "permissions" , perms . allow . toString ( ) ) ;
274
+ params . set ( "permissions" , perms . allow . toString ( ) ) ;
273
275
const encoded = params . toString ( ) ;
274
- url . setText ( `${ location . origin } /oauth2/authorize?${ encoded } ` ) ;
275
- } , 100 )
276
+ const urlStr = `${ location . origin } /oauth2/authorize?${ encoded } ` ;
277
+ if ( urlStr == url . elm . deref ( ) ?. textContent ) return ;
278
+ console . log ( urlStr , url . text ) ;
279
+ url . setText ( urlStr ) ;
280
+ } , 100 ) ;
276
281
}
277
282
}
278
283
export { Bot } ;
0 commit comments