cleaning up code and ditching .owner

This commit is contained in:
MathMan05 2024-06-23 14:27:54 -05:00
parent b9dcd46ea5
commit c60ce8cd99
6 changed files with 138 additions and 117 deletions

View file

@ -45,7 +45,13 @@ class channel{
this.lastmessageid=JSON.last_message_id;
}
isAdmin(){
return this.owner.isAdmin();
return this.guild.isAdmin();
}
get guild(){
return this.owner;
}
get localuser(){
return this.guild.localuser;
}
readStateInfo(json){
this.lastreadmessageid=json.last_message_id;
@ -58,7 +64,7 @@ class channel{
}
get canMessage(){
for(const thing of this.permission_overwrites){
if(this.owner.hasRole(thing.id)&&thing.deny&(1<<11)){
if(this.guild.hasRole(thing.id)&&thing.deny&(1<<11)){
return false;
}
}
@ -88,7 +94,7 @@ class channel{
thing.parent_id=thing.move_id;
thisthing.parent_id=thing.parent_id;
thing.move_id=undefined;
console.log(this.owner.channelids[thisthing.parent_id]);
console.log(this.guild.channelids[thisthing.parent_id]);
}
if(thisthing.position||thisthing.parent_id){
build.push(thisthing);
@ -191,7 +197,7 @@ class channel{
}
get myhtml(){
const search=document.getElementById("channels").children[0].children
if(this.owner!==this.owner.owner.lookingguild){
if(this.guild!==this.localuser.lookingguild){
return null
}else if(this.parrent){
for(const thing of search){
@ -222,7 +228,7 @@ class channel{
body:JSON.stringify({})
});
this.lastreadmessageid=this.lastmessageid;
this.owner.unreads();
this.guild.unreads();
if(this.myhtml!==null){
console.log(this.myhtml.classList)
this.myhtml.classList.remove("cunread");
@ -256,7 +262,7 @@ class channel{
if(that.parrent){
that.parrent.children.splice(that.parrent.children.indexOf(that),1);
}else{
this.owner.headchannels.splice(this.owner.headchannels.indexOf(that),1);
this.guild.headchannels.splice(this.guild.headchannels.indexOf(that),1);
}
that.parrent=this.parrent;
if(that.parrent){
@ -270,23 +276,23 @@ class channel{
that.parrent.children=build;
}else{
const build=[];
for(let i=0;i<this.owner.headchannels.length;i++){
build.push(this.owner.headchannels[i])
if(this.owner.headchannels[i]===this){
for(let i=0;i<this.guild.headchannels.length;i++){
build.push(this.guild.headchannels[i])
if(this.guild.headchannels[i]===this){
build.push(that);
}
}
this.owner.headchannels=build;
this.guild.headchannels=build;
}
div.after(channel.dragged[1]);
}
this.owner.calculateReorder()
this.guild.calculateReorder()
});
return div;
}
createChannel(name,type){
fetch(info.api.toString()+"/guilds/"+this.owner.id+"/channels",{
fetch(info.api.toString()+"/guilds/"+this.guild.id+"/channels",{
method:"Post",
headers:this.headers,
body:JSON.stringify({
@ -340,11 +346,11 @@ class channel{
})
}
getHTML(){
if(this.owner!==this.owner.owner.lookingguild){
this.owner.loadGuild();
if(this.guild!==this.localuser.lookingguild){
this.guild.loadGuild();
}
this.owner.prevchannel=this;
this.owner.owner.channelfocus=this.id;
this.guild.prevchannel=this;
this.localuser.channelfocus=this;
this.putmessages();
history.pushState(null, null,"/channels/"+this.guild_id+"/"+this.id);
document.getElementById("channelname").textContent="#"+this.name;
@ -457,7 +463,7 @@ class channel{
get notification(){
let notinumber=this.message_notifications;
if(+notinumber===3){notinumber=null;}
notinumber??=this.owner.message_notifications;
notinumber??=this.guild.message_notifications;
switch(+notinumber){
case 0:
return "all";
@ -469,10 +475,55 @@ class channel{
return "default";
}
}
async sendMessage(content,{attatchmenets=[],embeds=[],replyingto=false}){
let replyjson=false;
if(replyingto){
replyjson=
{
"guild_id":replyingto.guild.id,
"channel_id": replyingto.channel.id,
"message_id": replyingto.id,
};
}
if(attatchmenets.length===0){
const body={
content:content,
nonce:Math.floor(Math.random()*1000000000)
};
if(replyjson){
body.message_reference=replyjson;
}
console.log(body)
return await fetch(info.api.toString()+"/channels/"+window.location.pathname.split("/")[3]+"/messages",{
method:"POST",
headers:this.headers,
body:JSON.stringify(body)
})
}else{
const formData = new FormData();
const body={
content:content,
nonce:Math.floor(Math.random()*1000000000),
}
if(replyjson){
body.message_reference=replyjson;
}
formData.append('payload_json', JSON.stringify(body));
for(const i in attatchmenets){
console.log(attatchmenets[i])
formData.append("files["+i+"]",attatchmenets[i]);
}
return await fetch(info.api.toString()+"/channels/"+window.location.pathname.split("/")[3]+"/messages", {
method: 'POST',
body: formData,
headers:{"Authorization":this.headers.Authorization}
});
}
}
messageCreate(messagep,focus){
const messagez=new cmessage(messagep.d,this);
this.lastmessageid=messagez.id;
if(messagez.author===this.owner.owner.user){
if(messagez.author===this.localuser.user){
this.lastreadmessageid=messagez.id;
if(this.myhtml){
this.myhtml.classList.remove("cunread");
@ -482,31 +533,31 @@ class channel{
this.myhtml.classList.add("cunread");
}
}
this.owner.unreads();
this.guild.unreads();
this.messages.unshift(messagez);
const scrolly=document.getElementById("messagecontainer");
this.messageids[messagez.id]=messagez;
if(this.owner.owner.lookingguild.prevchannel===this){
if(this.localuser.lookingguild.prevchannel===this){
var shouldScroll=scrolly.scrollTop+scrolly.clientHeight>scrolly.scrollHeight-20;
messages.appendChild(messagez.buildhtml(this.messages[1]));
}
if(shouldScroll){
scrolly.scrollTop = scrolly.scrollHeight;
}
if(messagez.author===this.owner.owner.user){
if(messagez.author===this.localuser.user){
return;
}
if(this.owner.owner.lookingguild.prevchannel===this&&document.hasFocus()){
if(this.localuser.lookingguild.prevchannel===this&&document.hasFocus()){
return;
}
if(this.notification==="all"){
this.notify(messagez);
}else if(this.notification==="mentions"&&messagez.mentionsuser(this.owner.owner.user)){
}else if(this.notification==="mentions"&&messagez.mentionsuser(this.localuser.user)){
this.notify(messagez);
}
}
notititle(message){
return message.author.username+" > "+this.owner.properties.name+" > "+this.name;
return message.author.username+" > "+this.guild.properties.name+" > "+this.name;
}
notify(message){
voice.noises(voice.getNotificationSound());

View file

@ -1,10 +1,11 @@
class dirrect extends guild{
constructor(JSON,owner){
super(-1);
this.message_notifications=0;
console.log(JSON);
this.owner=owner;
this.headers=this.owner.headers;
if(!this.owner){
this.headers=this.localuser.headers;
if(!this.localuser){
console.error("Owner was not included, please fix")
}
this.channels=[];
@ -57,15 +58,15 @@ class group extends channel{
constructor(JSON,owner){
super(-1);
this.owner=owner;
this.headers=this.owner.headers;
this.headers=this.guild.headers;
this.messages=[];
this.name=JSON.recipients[0]?.username;
if(JSON.recipients[0]){
this.user=new user(JSON.recipients[0]);
}else{
this.user=this.owner.owner.user;
this.user=this.localuser.user;
}
this.name??=owner.owner.user.username;
this.name??=this.localuser.user.username;
this.id=JSON.id;
this.parent_id=null;
this.parrent=null;
@ -91,8 +92,8 @@ class group extends channel{
return div;
}
getHTML(){
this.owner.prevchannel=this;
this.owner.owner.channelfocus=this.id;
this.guild.prevchannel=this;
this.localuser.channelfocus=this;
this.putmessages();
history.pushState(null, null,"/channels/"+this.guild_id+"/"+this.id);
document.getElementById("channelname").textContent="@"+this.name;
@ -100,13 +101,13 @@ class group extends channel{
messageCreate(messagep,focus){
const messagez=new cmessage(messagep.d,this);
this.lastmessageid=messagez.id;
if(messagez.author===this.owner.owner.user){
if(messagez.author===this.localuser.user){
this.lastreadmessageid=messagez.id;
}
this.messages.unshift(messagez);
const scrolly=document.getElementById("messagecontainer");
this.messageids[messagez.id]=messagez;
if(this.owner.owner.lookingguild.prevchannel===this){
if(this.localuser.lookingguild.prevchannel===this){
var shouldScroll=scrolly.scrollTop+scrolly.clientHeight>scrolly.scrollHeight-20;
messages.appendChild(messagez.buildhtml(this.messages[1]));
}
@ -114,7 +115,7 @@ class group extends channel{
scrolly.scrollTop = scrolly.scrollHeight;
}
console.log(document.getElementById("channels").children)
if(this.owner.owner.lookingguild===this.owner){
if(this.localuser.lookingguild===this.guild){
const channellist=document.getElementById("channels").children[0]
for(const thing of channellist.children){
if(thing.myinfo===this){
@ -126,15 +127,16 @@ class group extends channel{
}
}
this.unreads();
if(messagez.author===this.owner.owner.user){
if(messagez.author===this.localuser.user){
return;
}
if(this.owner.owner.lookingguild.prevchannel===this&&document.hasFocus()){
if(this.localuser.lookingguild.prevchannel===this&&document.hasFocus()){
return;
}
console.log(this.notification);
if(this.notification==="all"){
this.notify(messagez);
}else if(this.notification==="mentions"&&messagez.mentionsuser(this.owner.owner.user)){
}else if(this.notification==="mentions"&&messagez.mentionsuser(this.localuser.user)){
this.notify(messagez);
}
}
@ -167,7 +169,7 @@ class group extends channel{
div.append(buildpfp)
sentdms.append(div);
div.onclick=function(){
this.all.owner.loadGuild();
this.all.guild.loadGuild();
this.all.getHTML();
}
}else if(current){

View file

@ -46,7 +46,7 @@ class guild{
this.owner=owner;
this.headers=this.owner.headers;
if(!this.owner){
console.error("Owner was not included, please fix")
console.error("localuser was not included, please fix")
}
this.channels=[];
this.channelids={};
@ -196,8 +196,11 @@ class guild{
}
}
get localuser(){
return this.owner;
}
loadChannel(id){
this.owner.channelfocus=id;
this.localuser.channelfocus=this.channelids[id];
this.channelids[id].getHTML();
}
sortchannels(){
@ -210,7 +213,7 @@ class guild{
const noti=document.createElement("div");
noti.classList.add("unread");
divy.append(noti);
this.owner.guildhtml[this.id]=divy;
this.localuser.guildhtml[this.id]=divy;
if(this.properties.icon!=null){
const img=document.createElement("img");
img.classList.add("pfp","servericon");
@ -379,7 +382,7 @@ class guild{
}
}
loadGuild(){
this.owner.loadGuild(this.id);
this.localuser.loadGuild(this.id);
}
updateChannel(JSON){
this.channelids[JSON.id].updateChannel(JSON);

View file

@ -248,82 +248,31 @@ typebox.addEventListener("keydown",event=>{
});
console.log(typebox)
typebox.onclick=console.log;
async function enter(event){
thisuser.lookingguild.prevchannel.typingstart();
thisuser.channelfocus.typingstart();
if(event.key === "Enter"&&!event.shiftKey){
event.preventDefault();
if(editing){
fetch(info.api.toString()+"/channels/"+window.location.pathname.split("/")[3]+"/messages/"+editing,{
method: "PATCH",
headers: thisuser.headers,
body:JSON.stringify({content:typebox.value})
})
typebox.value="";
editing=false;
}else{
let replyjson=false;
if(replyingto){
replyjson=
{
"guild_id":replyingto.all.guild_id,
"channel_id": replyingto.all.channel_id,
"message_id": replyingto.all.id,
};
replyingto.classList.remove("replying");
}
replyingto=false;
if(images.length==0){
const body={
content:typebox.value,
nonce:Math.floor(Math.random()*1000000000)
};
if(replyjson){
body.message_reference=replyjson;
}
console.log(body)
fetch(info.api.toString()+"/channels/"+window.location.pathname.split("/")[3]+"/messages",{
method:"POST",
headers:thisuser.headers,
body:JSON.stringify(body)
}).then(
function(out){
console.log(out,"here it is")
}
)
typebox.value="";
if(editing){
editing.edit(typebox.value);
editing=false;
}else{
let formData = new FormData();
const body={
content:typebox.value,
nonce:Math.floor(Math.random()*1000000000),
let replying=replyingto?.all;
if(replyingto){
replyingto.classList.remove("replying");
}
if(replyjson){
body.message_reference=replyjson;
}
formData.append('payload_json', JSON.stringify(body));
for(i in images){
console.log(images[i])
formData.append("files["+i+"]",images[i]);
}
const data=formData.entries()
console.log(data.next(),data.next(),data.next())
console.log((await fetch(info.api.toString()+"/channels/"+window.location.pathname.split("/")[3]+"/messages", {
method: 'POST',
body: formData,
headers:{"Authorization":thisuser.token}
})));
//fetch("/sendimagemessage",{body:formData,method:"POST"})
while(images.length!=0){
images.pop()
pasteimage.removeChild(imageshtml.pop())
}
typebox.value="";
replyingto=false;
thisuser.channelfocus.sendMessage(typebox.value,{
attatchmenets:images,
replyingto:replying,
})
}
}
while(images.length!=0){
images.pop();
pasteimage.removeChild(imageshtml.pop());
}
typebox.value="";
return;
}
}

View file

@ -238,7 +238,7 @@ class localuser{
console.log(guild);
guild.loadChannel(location[5]);
console.log(location[5])
this.channelfocus=location[5];
this.channelfocus=guild.channelids[location[5]];
}
this.buildservers();
}
@ -356,7 +356,7 @@ class localuser{
}
messageCreate(messagep){
messagep.d.guild_id??="@me";
this.guildids[messagep.d.guild_id].channelids[messagep.d.channel_id].messageCreate(messagep,this.channelfocus===messagep.d.channel_id);
this.guildids[messagep.d.guild_id].channelids[messagep.d.channel_id].messageCreate(messagep,this.channelfocus.id===messagep.d.channel_id);
this.unreads();
}
unreads(){
@ -367,7 +367,7 @@ class localuser{
}
}
typeingStart(typing){
if(this.channelfocus===typing.d.channel_id){
if(this.channelfocus.id===typing.d.channel_id){
const memb=typing.d.member;
let name;
if(memb.id===this.user.id){

View file

@ -32,7 +32,7 @@ class cmessage{
})
cmessage.contextmenu.addbutton("Edit",function(){
console.log(this)
editing=this.id;
editing=this;
document.getElementById("typebox").value=this.content;
},null,_=>{return _.author.id==READY.d.user.id});
}
@ -53,10 +53,19 @@ class cmessage{
if(this.mentions.length||this.mention_roles.length){//currently mention_roles isn't implemented on the spacebar servers
console.log(this.mentions,this.mention_roles)
}
if(this.mentionsuser(this.owner.owner.owner.user)){
if(this.mentionsuser(this.localuser.user)){
console.log(this);
}
}
get channel(){
return this.owner;
}
get guild(){
return this.owner.guild;
}
get localuser(){
return this.owner.localuser;
}
messageevents(obj){
cmessage.contextmenu.bind(obj,this)
obj.classList.add("messagediv")
@ -77,6 +86,13 @@ class cmessage{
}
return build;
}
async edit(content){
return await fetch(info.api.toString()+"/channels/"+window.location.pathname.split("/")[3]+"/messages/"+this.id,{
method: "PATCH",
headers: this.headers,
body:JSON.stringify({content:content})
});
}
buildhtml(premessage){
//premessage??=messages.lastChild;
const build = document.createElement('table');