Skip to content

Commit 3ce6748

Browse files
committed
fix bot invite creator
1 parent 99c348b commit 3ce6748

File tree

1 file changed

+120
-115
lines changed

1 file changed

+120
-115
lines changed

src/webpage/bot.ts

+120-115
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
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";
66
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() {
1616
return this.owner;
1717
}
18-
get info(){
18+
get info() {
1919
return this.localuser.info;
2020
}
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 = {
2626
"Content-type": "application/json; charset=UTF-8",
2727
Authorization: token,
2828
};
2929
}
30-
settings(){
30+
settings() {
3131
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);
3434
{
3535
const hypotheticalProfile = document.createElement("div");
3636

@@ -39,7 +39,7 @@ class Bot{
3939
let newbio: string | undefined;
4040
const hypouser = bot.clone();
4141
let color: string;
42-
async function regen(){
42+
async function regen() {
4343
hypotheticalProfile.textContent = "";
4444
const hypoprofile = await hypouser.buildprofile(-1, -1);
4545

@@ -52,22 +52,22 @@ class Bot{
5252

5353
const finput = settingsLeft.addFileInput(
5454
I18n.getTranslation("uploadPfp"),
55-
_=>{
56-
if(file){
55+
(_) => {
56+
if (file) {
5757
this.updatepfp(file);
5858
}
5959
},
60-
{ clear: true }
60+
{clear: true},
6161
);
62-
finput.watchForChange(_=>{
63-
if(!_){
62+
finput.watchForChange((_) => {
63+
if (!_) {
6464
file = null;
6565
hypouser.avatar = null;
6666
hypouser.hypotheticalpfp = true;
6767
regen();
6868
return;
6969
}
70-
if(_.length){
70+
if (_.length) {
7171
file = _[0];
7272
const blob = URL.createObjectURL(file);
7373
hypouser.avatar = blob;
@@ -78,22 +78,22 @@ class Bot{
7878
let bfile: undefined | File | null;
7979
const binput = settingsLeft.addFileInput(
8080
I18n.getTranslation("uploadBanner"),
81-
_=>{
82-
if(bfile !== undefined){
81+
(_) => {
82+
if (bfile !== undefined) {
8383
this.updatebanner(bfile);
8484
}
8585
},
86-
{ clear: true }
86+
{clear: true},
8787
);
88-
binput.watchForChange(_=>{
89-
if(!_){
88+
binput.watchForChange((_) => {
89+
if (!_) {
9090
bfile = null;
9191
hypouser.banner = undefined;
9292
hypouser.hypotheticalbanner = true;
9393
regen();
9494
return;
9595
}
96-
if(_.length){
96+
if (_.length) {
9797
bfile = _[0];
9898
const blob = URL.createObjectURL(bfile);
9999
hypouser.banner = blob;
@@ -104,42 +104,42 @@ class Bot{
104104
let changed = false;
105105
const pronounbox = settingsLeft.addTextInput(
106106
I18n.getTranslation("pronouns"),
107-
_=>{
108-
if(newpronouns || newbio || changed){
107+
(_) => {
108+
if (newpronouns || newbio || changed) {
109109
this.updateProfile({
110110
pronouns: newpronouns,
111111
bio: newbio,
112112
accent_color: Number.parseInt("0x" + color.substr(1), 16),
113113
});
114114
}
115115
},
116-
{ initText: bot.pronouns }
116+
{initText: bot.pronouns},
117117
);
118-
pronounbox.watchForChange(_=>{
118+
pronounbox.watchForChange((_) => {
119119
hypouser.pronouns = _;
120120
newpronouns = _;
121121
regen();
122122
});
123-
const bioBox = settingsLeft.addMDInput(I18n.getTranslation("bio"), _=>{}, {
123+
const bioBox = settingsLeft.addMDInput(I18n.getTranslation("bio"), (_) => {}, {
124124
initText: bot.bio.rawString,
125125
});
126-
bioBox.watchForChange(_=>{
126+
bioBox.watchForChange((_) => {
127127
newbio = _;
128128
hypouser.bio = new MarkDown(_, this.owner);
129129
regen();
130130
});
131131

132-
if(bot.accent_color){
132+
if (bot.accent_color) {
133133
color = "#" + bot.accent_color.toString(16);
134-
}else{
134+
} else {
135135
color = "transparent";
136136
}
137137
const colorPicker = settingsLeft.addColorInput(
138138
I18n.getTranslation("profileColor"),
139-
_=>{},
140-
{ initColor: color }
139+
(_) => {},
140+
{initColor: color},
141141
);
142-
colorPicker.watchForChange(_=>{
142+
colorPicker.watchForChange((_) => {
143143
console.log();
144144
color = _;
145145
hypouser.accent_color = Number.parseInt("0x" + _.substr(1), 16);
@@ -148,65 +148,71 @@ class Bot{
148148
});
149149
}
150150
{
151-
const guildsettings=settings.addButton("Guilds");
151+
const guildsettings = settings.addButton("Guilds");
152152
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");
168161

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+
}
177171

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);
185184

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);
186192

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+
};
199206
}
200-
}
201-
})
207+
});
202208
}
203209
settings.show();
204210
}
205211

206-
updatepfp(file: Blob): void{
212+
updatepfp(file: Blob): void {
207213
const reader = new FileReader();
208214
reader.readAsDataURL(file);
209-
reader.onload = ()=>{
215+
reader.onload = () => {
210216
fetch(this.info.api + "/users/@me", {
211217
method: "PATCH",
212218
headers: this.headers,
@@ -216,11 +222,11 @@ class Bot{
216222
});
217223
};
218224
}
219-
updatebanner(file: Blob | null): void{
220-
if(file){
225+
updatebanner(file: Blob | null): void {
226+
if (file) {
221227
const reader = new FileReader();
222228
reader.readAsDataURL(file);
223-
reader.onload = ()=>{
229+
reader.onload = () => {
224230
fetch(this.info.api + "/users/@me", {
225231
method: "PATCH",
226232
headers: this.headers,
@@ -229,7 +235,7 @@ class Bot{
229235
}),
230236
});
231237
};
232-
}else{
238+
} else {
233239
fetch(this.info.api + "/users/@me", {
234240
method: "PATCH",
235241
headers: this.headers,
@@ -239,40 +245,39 @@ class Bot{
239245
});
240246
}
241247
}
242-
updateProfile(json: {
243-
bio?: string;
244-
pronouns?: string;
245-
accent_color?: number;
246-
}){
248+
updateProfile(json: {bio?: string; pronouns?: string; accent_color?: number}) {
247249
fetch(this.info.api + "/users/@me/profile", {
248250
method: "PATCH",
249251
headers: this.headers,
250252
body: JSON.stringify(json),
251253
});
252254
}
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,
256258
});
257259
const params = new URLSearchParams("");
258260
params.set("instance", info.wellknown);
259261
params.set("client_id", id);
260262
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);
265267
gen.options.push(permsisions);
266268
gen.generate(permsisions);
267269
}
268-
const cancel=setInterval(()=>{
269-
if(!gen.container.deref()){
270+
const cancel = setInterval(() => {
271+
if (!gen.container.deref()) {
270272
clearInterval(cancel);
271273
}
272-
params.set("permissions",perms.allow.toString());
274+
params.set("permissions", perms.allow.toString());
273275
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);
276281
}
277282
}
278283
export {Bot};

0 commit comments

Comments
 (0)