Correcting names, and deleting message

There is a known regresion for the MESSAGE_CREATE event while it's not on screen, though I have not been able to replicate it while I'm looking for it. If you see this bug, please let me know the conditions it happens under
This commit is contained in:
MathMan05 2024-06-28 11:13:26 -05:00
parent 845c7f6612
commit ac939e5fb6
17 changed files with 473 additions and 166 deletions

View file

@ -12,12 +12,25 @@ class Message {
author;
mentions;
mention_roles;
attachments;
attachments; //probably should be its own class tbh, should be Attachments[]
id;
message_reference;
type;
timestamp;
content;
static del;
static resolve;
div;
static setup() {
this.del = new Promise(_ => { this.resolve = _; });
Message.setupcmenu();
}
static async wipeChanel() {
this.resolve();
document.getElementById("messages").innerHTML = "";
await Promise.allSettled([this.resolve]);
this.del = new Promise(_ => { this.resolve = _; });
}
static setupcmenu() {
Message.contextmenu.addbutton("Copy raw text", function () {
navigator.clipboard.writeText(this.content);
@ -46,6 +59,9 @@ class Message {
this.channel.editing = this;
document.getElementById("typebox").value = this.content;
}, null, _ => { return _.author.id === _.localuser.user.id; });
Message.contextmenu.addbutton("Delete message", function () {
this.delete();
}, null, _ => { return _.canDelete(); });
}
constructor(messagejson, owner) {
this.owner = owner;
@ -68,6 +84,9 @@ class Message {
console.log(this);
}
}
canDelete() {
return this.channel.hasPermission("MANAGE_MESSAGES") || this.author.id === this.localuser.user.id;
}
get channel() {
return this.owner;
}
@ -81,7 +100,12 @@ class Message {
return this.owner.info;
}
messageevents(obj) {
Message.contextmenu.bind(obj, this);
const func = Message.contextmenu.bind(obj, this);
this.div = obj;
Message.del.then(_ => {
obj.removeEventListener("click", func);
this.div = null;
});
obj.classList.add("messagediv");
}
mentionsuser(userd) {
@ -108,10 +132,32 @@ class Message {
body: JSON.stringify({ content: content })
});
}
buildhtml(premessage) {
//premessage??=messages.lastChild;
delete() {
fetch(`${this.info.api.toString()}/channels/${this.channel.id}/messages/${this.id}`, {
headers: this.headers,
method: "DELETE",
});
}
deleteEvent() {
if (this.div) {
this.div.innerHTML = "";
this.div = null;
}
const index = this.channel.messages.indexOf(this);
this.channel.messages.splice(this.channel.messages.indexOf(this), 1);
delete this.channel.messageids[this.id];
const regen = this.channel.messages[index - 1];
if (regen) {
regen.generateMessage();
}
}
generateMessage(premessage = null) {
if (!premessage) {
premessage = this.channel.messages[this.channel.messages.indexOf(this) + 1];
}
const div = this.div;
div.innerHTML = "";
const build = document.createElement('table');
const div = document.createElement("div");
if (this.message_reference) {
const replyline = document.createElement("div");
const line = document.createElement("hr");
@ -124,7 +170,23 @@ class Message {
const reply = document.createElement("div");
username.classList.add("username");
Member.resolve(this.author, this.guild).then(_ => {
if (!_) {
return;
}
;
console.log(_.error);
if (_.error) {
username.textContent += "Error";
alert("Should've gotten here");
const error = document.createElement("span");
error.textContent = "!";
error.classList.add("membererror");
username.after(error);
return;
}
username.style.color = _.getColor();
}).catch(_ => {
console.log(_);
});
reply.classList.add("replytext");
replyline.appendChild(reply);
@ -150,7 +212,6 @@ class Message {
const pfpRow = document.createElement('th');
let pfpparent, current;
if (premessage != null) {
pfpparent = premessage.pfpparent;
pfpparent ??= premessage;
let pfpparent2 = pfpparent.all;
pfpparent2 ??= pfpparent;
@ -158,7 +219,7 @@ class Message {
const newt = (new Date(this.timestamp).getTime()) / 1000;
current = (newt - old) > 600;
}
const combine = (premessage?.userid != this.author.id && premessage?.author?.id != this.author.id) || (current) || this.message_reference;
const combine = (premessage?.author?.id != this.author.id) || (current) || this.message_reference;
if (combine) {
const pfp = this.author.buildpfp();
this.author.profileclick(pfp);
@ -181,6 +242,14 @@ class Message {
if (!_) {
return;
}
;
if (_.error) {
const error = document.createElement("span");
error.textContent = "!";
error.classList.add("membererror");
username.after(error);
return;
}
username.style.color = _.getColor();
});
username.textContent = this.author.username;
@ -254,10 +323,19 @@ class Message {
messagedwrap.append(time);
texttxt.appendChild(messagedwrap);
}
div["userid"] = this.author.id;
div["all"] = this;
return (div);
}
buildhtml(premessage) {
if (this.div) {
console.error(`HTML for ${this} already exists, aborting`);
return;
}
//premessage??=messages.lastChild;
const div = document.createElement("div");
this.div = div;
return this.generateMessage(premessage);
}
createunknown(fname, fsize, src) {
const div = document.createElement("table");
div.classList.add("unknownfile");
@ -314,5 +392,5 @@ function formatTime(date) {
return `${date.toLocaleDateString()} at ${formatTime(date)}`;
}
}
Message.setupcmenu();
Message.setup();
export { Message };