fix bot invite creator

This commit is contained in:
MathMan05 2025-03-10 14:20:07 -05:00
parent 99c348b086
commit 3ce6748ad6

View file

@ -1,36 +1,36 @@
import { mainuserjson } from "./jsontypes.js"; import {mainuserjson} from "./jsontypes.js";
import { Localuser } from "./localuser.js"; import {Localuser} from "./localuser.js";
import { MarkDown } from "./markdown.js"; import {MarkDown} from "./markdown.js";
import { Form, Settings } from "./settings.js"; import {Form, Settings} from "./settings.js";
import { User } from "./user.js"; import {User} from "./user.js";
import {guildjson} from "./jsontypes.js"; import {guildjson} from "./jsontypes.js";
import { PermissionToggle } from "./role.js"; import {PermissionToggle} from "./role.js";
import { Permissions } from "./permissions.js"; import {Permissions} from "./permissions.js";
import { I18n } from "./i18n.js"; import {I18n} from "./i18n.js";
class Bot{ class Bot {
readonly owner:Localuser; readonly owner: Localuser;
readonly token:string; readonly token: string;
readonly json:mainuserjson; readonly json: mainuserjson;
headers: { "Content-type": string; Authorization: string }; headers: {"Content-type": string; Authorization: string};
get localuser(){ get localuser() {
return this.owner; return this.owner;
} }
get info(){ get info() {
return this.localuser.info; return this.localuser.info;
} }
constructor(json:mainuserjson,token:string,owner:Localuser){ constructor(json: mainuserjson, token: string, owner: Localuser) {
this.owner=owner; this.owner = owner;
this.token=token; this.token = token;
this.json=json; this.json = json;
this.headers={ this.headers = {
"Content-type": "application/json; charset=UTF-8", "Content-type": "application/json; charset=UTF-8",
Authorization: token, Authorization: token,
}; };
} }
settings(){ settings() {
const settings = new Settings(I18n.getTranslation("botSettings")); const settings = new Settings(I18n.getTranslation("botSettings"));
const botOptions = settings.addButton("Profile",{ltr:true}); const botOptions = settings.addButton("Profile", {ltr: true});
const bot=new User(this.json,this.localuser); const bot = new User(this.json, this.localuser);
{ {
const hypotheticalProfile = document.createElement("div"); const hypotheticalProfile = document.createElement("div");
@ -39,7 +39,7 @@ class Bot{
let newbio: string | undefined; let newbio: string | undefined;
const hypouser = bot.clone(); const hypouser = bot.clone();
let color: string; let color: string;
async function regen(){ async function regen() {
hypotheticalProfile.textContent = ""; hypotheticalProfile.textContent = "";
const hypoprofile = await hypouser.buildprofile(-1, -1); const hypoprofile = await hypouser.buildprofile(-1, -1);
@ -52,22 +52,22 @@ class Bot{
const finput = settingsLeft.addFileInput( const finput = settingsLeft.addFileInput(
I18n.getTranslation("uploadPfp"), I18n.getTranslation("uploadPfp"),
_=>{ (_) => {
if(file){ if (file) {
this.updatepfp(file); this.updatepfp(file);
} }
}, },
{ clear: true } {clear: true},
); );
finput.watchForChange(_=>{ finput.watchForChange((_) => {
if(!_){ if (!_) {
file = null; file = null;
hypouser.avatar = null; hypouser.avatar = null;
hypouser.hypotheticalpfp = true; hypouser.hypotheticalpfp = true;
regen(); regen();
return; return;
} }
if(_.length){ if (_.length) {
file = _[0]; file = _[0];
const blob = URL.createObjectURL(file); const blob = URL.createObjectURL(file);
hypouser.avatar = blob; hypouser.avatar = blob;
@ -78,22 +78,22 @@ class Bot{
let bfile: undefined | File | null; let bfile: undefined | File | null;
const binput = settingsLeft.addFileInput( const binput = settingsLeft.addFileInput(
I18n.getTranslation("uploadBanner"), I18n.getTranslation("uploadBanner"),
_=>{ (_) => {
if(bfile !== undefined){ if (bfile !== undefined) {
this.updatebanner(bfile); this.updatebanner(bfile);
} }
}, },
{ clear: true } {clear: true},
); );
binput.watchForChange(_=>{ binput.watchForChange((_) => {
if(!_){ if (!_) {
bfile = null; bfile = null;
hypouser.banner = undefined; hypouser.banner = undefined;
hypouser.hypotheticalbanner = true; hypouser.hypotheticalbanner = true;
regen(); regen();
return; return;
} }
if(_.length){ if (_.length) {
bfile = _[0]; bfile = _[0];
const blob = URL.createObjectURL(bfile); const blob = URL.createObjectURL(bfile);
hypouser.banner = blob; hypouser.banner = blob;
@ -104,8 +104,8 @@ class Bot{
let changed = false; let changed = false;
const pronounbox = settingsLeft.addTextInput( const pronounbox = settingsLeft.addTextInput(
I18n.getTranslation("pronouns"), I18n.getTranslation("pronouns"),
_=>{ (_) => {
if(newpronouns || newbio || changed){ if (newpronouns || newbio || changed) {
this.updateProfile({ this.updateProfile({
pronouns: newpronouns, pronouns: newpronouns,
bio: newbio, bio: newbio,
@ -113,33 +113,33 @@ class Bot{
}); });
} }
}, },
{ initText: bot.pronouns } {initText: bot.pronouns},
); );
pronounbox.watchForChange(_=>{ pronounbox.watchForChange((_) => {
hypouser.pronouns = _; hypouser.pronouns = _;
newpronouns = _; newpronouns = _;
regen(); regen();
}); });
const bioBox = settingsLeft.addMDInput(I18n.getTranslation("bio"), _=>{}, { const bioBox = settingsLeft.addMDInput(I18n.getTranslation("bio"), (_) => {}, {
initText: bot.bio.rawString, initText: bot.bio.rawString,
}); });
bioBox.watchForChange(_=>{ bioBox.watchForChange((_) => {
newbio = _; newbio = _;
hypouser.bio = new MarkDown(_, this.owner); hypouser.bio = new MarkDown(_, this.owner);
regen(); regen();
}); });
if(bot.accent_color){ if (bot.accent_color) {
color = "#" + bot.accent_color.toString(16); color = "#" + bot.accent_color.toString(16);
}else{ } else {
color = "transparent"; color = "transparent";
} }
const colorPicker = settingsLeft.addColorInput( const colorPicker = settingsLeft.addColorInput(
I18n.getTranslation("profileColor"), I18n.getTranslation("profileColor"),
_=>{}, (_) => {},
{ initColor: color } {initColor: color},
); );
colorPicker.watchForChange(_=>{ colorPicker.watchForChange((_) => {
console.log(); console.log();
color = _; color = _;
hypouser.accent_color = Number.parseInt("0x" + _.substr(1), 16); hypouser.accent_color = Number.parseInt("0x" + _.substr(1), 16);
@ -148,65 +148,71 @@ class Bot{
}); });
} }
{ {
const guildsettings=settings.addButton("Guilds"); const guildsettings = settings.addButton("Guilds");
guildsettings.addTitle(I18n.getTranslation("botGuilds")); guildsettings.addTitle(I18n.getTranslation("botGuilds"));
fetch(this.info.api+"/users/@me/guilds/",{ fetch(this.info.api + "/users/@me/guilds/", {
headers:this.headers headers: this.headers,
}).then(_=>_.json()).then((json:(guildjson["properties"])[])=>{
for(const guild of json){
const content = document.createElement("div");
content.classList.add("discovery-guild");
if(guild.banner){
const banner = document.createElement("img");
banner.classList.add("banner");
banner.crossOrigin = "anonymous";
banner.src = this.info.cdn + "/icons/" + guild.id + "/" + guild.banner + ".png?size=256";
banner.alt = "";
content.appendChild(banner);
}
const nameContainer = document.createElement("div");
nameContainer.classList.add("flex");
const img = document.createElement("img");
img.classList.add("icon");
img.crossOrigin = "anonymous";
img.src = this.info.cdn + (guild.icon? "/icons/" + guild.id + "/" + guild.icon + ".png?size=48": "/embed/avatars/3.png");
img.alt = "";
nameContainer.appendChild(img);
const name = document.createElement("h3");
name.textContent = guild.name;
nameContainer.appendChild(name);
content.appendChild(nameContainer);
const desc = document.createElement("p");
desc.textContent = guild.description;
content.appendChild(desc);
guildsettings.addHTMLArea(content);
content.onclick=()=>{
const guildsetting=guildsettings.addSubOptions(guild.name);
guildsetting.addHTMLArea(content);
guildsetting.addButtonInput("",I18n.getTranslation("leaveGuild"),()=>{
if(confirm(I18n.getTranslation("confirmGuildLeave",guild.name))){
fetch(this.info.api+"/users/@me/guilds/"+guild.id,{
method:"DELETE",
headers:this.headers
})
}
})
}
}
}) })
.then((_) => _.json())
.then((json: guildjson["properties"][]) => {
for (const guild of json) {
const content = document.createElement("div");
content.classList.add("discovery-guild");
if (guild.banner) {
const banner = document.createElement("img");
banner.classList.add("banner");
banner.crossOrigin = "anonymous";
banner.src =
this.info.cdn + "/icons/" + guild.id + "/" + guild.banner + ".png?size=256";
banner.alt = "";
content.appendChild(banner);
}
const nameContainer = document.createElement("div");
nameContainer.classList.add("flex");
const img = document.createElement("img");
img.classList.add("icon");
img.crossOrigin = "anonymous";
img.src =
this.info.cdn +
(guild.icon
? "/icons/" + guild.id + "/" + guild.icon + ".png?size=48"
: "/embed/avatars/3.png");
img.alt = "";
nameContainer.appendChild(img);
const name = document.createElement("h3");
name.textContent = guild.name;
nameContainer.appendChild(name);
content.appendChild(nameContainer);
const desc = document.createElement("p");
desc.textContent = guild.description;
content.appendChild(desc);
guildsettings.addHTMLArea(content);
content.onclick = () => {
const guildsetting = guildsettings.addSubOptions(guild.name);
guildsetting.addHTMLArea(content);
guildsetting.addButtonInput("", I18n.getTranslation("leaveGuild"), () => {
if (confirm(I18n.getTranslation("confirmGuildLeave", guild.name))) {
fetch(this.info.api + "/users/@me/guilds/" + guild.id, {
method: "DELETE",
headers: this.headers,
});
}
});
};
}
});
} }
settings.show(); settings.show();
} }
updatepfp(file: Blob): void{ updatepfp(file: Blob): void {
const reader = new FileReader(); const reader = new FileReader();
reader.readAsDataURL(file); reader.readAsDataURL(file);
reader.onload = ()=>{ reader.onload = () => {
fetch(this.info.api + "/users/@me", { fetch(this.info.api + "/users/@me", {
method: "PATCH", method: "PATCH",
headers: this.headers, headers: this.headers,
@ -216,11 +222,11 @@ class Bot{
}); });
}; };
} }
updatebanner(file: Blob | null): void{ updatebanner(file: Blob | null): void {
if(file){ if (file) {
const reader = new FileReader(); const reader = new FileReader();
reader.readAsDataURL(file); reader.readAsDataURL(file);
reader.onload = ()=>{ reader.onload = () => {
fetch(this.info.api + "/users/@me", { fetch(this.info.api + "/users/@me", {
method: "PATCH", method: "PATCH",
headers: this.headers, headers: this.headers,
@ -229,7 +235,7 @@ class Bot{
}), }),
}); });
}; };
}else{ } else {
fetch(this.info.api + "/users/@me", { fetch(this.info.api + "/users/@me", {
method: "PATCH", method: "PATCH",
headers: this.headers, headers: this.headers,
@ -239,40 +245,39 @@ class Bot{
}); });
} }
} }
updateProfile(json: { updateProfile(json: {bio?: string; pronouns?: string; accent_color?: number}) {
bio?: string;
pronouns?: string;
accent_color?: number;
}){
fetch(this.info.api + "/users/@me/profile", { fetch(this.info.api + "/users/@me/profile", {
method: "PATCH", method: "PATCH",
headers: this.headers, headers: this.headers,
body: JSON.stringify(json), body: JSON.stringify(json),
}); });
} }
static InviteMaker(id:string,container:Form,info:Localuser["info"]){ static InviteMaker(id: string, container: Form, info: Localuser["info"]) {
const gen=container.addSubOptions(I18n.getTranslation("UrlGen"),{ const gen = container.addSubOptions(I18n.getTranslation("UrlGen"), {
noSubmit:true noSubmit: true,
}); });
const params = new URLSearchParams(""); const params = new URLSearchParams("");
params.set("instance", info.wellknown); params.set("instance", info.wellknown);
params.set("client_id", id); params.set("client_id", id);
params.set("scope", "bot"); params.set("scope", "bot");
const url=gen.addText(""); const url = gen.addText("");
const perms=new Permissions("0"); const perms = new Permissions("0");
for(const perm of Permissions.info()){ for (const perm of Permissions.info()) {
const permsisions=new PermissionToggle(perm,perms,gen); const permsisions = new PermissionToggle(perm, perms, gen);
gen.options.push(permsisions); gen.options.push(permsisions);
gen.generate(permsisions); gen.generate(permsisions);
} }
const cancel=setInterval(()=>{ const cancel = setInterval(() => {
if(!gen.container.deref()){ if (!gen.container.deref()) {
clearInterval(cancel); clearInterval(cancel);
} }
params.set("permissions",perms.allow.toString()); params.set("permissions", perms.allow.toString());
const encoded = params.toString(); const encoded = params.toString();
url.setText(`${location.origin}/oauth2/authorize?${encoded}`); const urlStr = `${location.origin}/oauth2/authorize?${encoded}`;
},100) if (urlStr == url.elm.deref()?.textContent) return;
console.log(urlStr, url.text);
url.setText(urlStr);
}, 100);
} }
} }
export {Bot}; export {Bot};