2023-04-07 19:13:15 -04:00
|
|
|
import axios from "axios";
|
|
|
|
|
import { Channel } from "./Channel.js";
|
2023-04-07 20:12:16 -04:00
|
|
|
import { BaseStruct } from "../baseStruct.js";
|
2023-04-07 19:13:15 -04:00
|
|
|
|
|
|
|
|
export class Thread extends Channel {
|
|
|
|
|
/** @type {Number} */
|
|
|
|
|
total_message_sent;
|
|
|
|
|
|
|
|
|
|
/** @type {{locked: Boolean, create_timestamp: String, auto_archive_duration: Number, archived: Boolean, archive_timestamp: String}} */
|
|
|
|
|
thread_metadata;
|
|
|
|
|
|
|
|
|
|
/** @type {Number} */
|
|
|
|
|
message_count;
|
|
|
|
|
|
|
|
|
|
|
2023-04-07 20:12:16 -04:00
|
|
|
constructor(o, guild, client) {
|
|
|
|
|
super(o, guild, client);
|
2023-04-07 19:13:15 -04:00
|
|
|
this.last_message_sent = o['total_message_sent'];
|
|
|
|
|
this.thread_metadata = o['thread_metadata'];
|
|
|
|
|
this.message_count = o['thread_metadata'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-07 20:12:16 -04:00
|
|
|
export class ThreadManager extends BaseStruct {
|
2023-04-07 19:13:15 -04:00
|
|
|
/** @type {Map<String, Thread>} */
|
|
|
|
|
cache;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @returns {Thread | Boolean} The deleted thread or false if the operation failed
|
|
|
|
|
* @param {Thread | String} threadId
|
|
|
|
|
*/
|
|
|
|
|
has(thread) {
|
|
|
|
|
const tid = (typeof thread == 'string') ? thread : thread.id;
|
|
|
|
|
if (!this.cache.has(tid)) return false;
|
|
|
|
|
const threadToDel = this.cache.get(tid);
|
|
|
|
|
this.cache.delete(tid);
|
|
|
|
|
return threadToDel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description returns the deleted thread if successful
|
|
|
|
|
* @param {Thread} thread
|
|
|
|
|
* @param {String} reason
|
|
|
|
|
* @returns {promise<Channel>}
|
|
|
|
|
*/
|
|
|
|
|
async delete(thread, reason=null) {
|
|
|
|
|
return new Promise(async (resolve) => {
|
|
|
|
|
try {
|
|
|
|
|
if (!this.cache.has(thread.id)) throw "This thread does not exist!";
|
|
|
|
|
|
2023-04-07 20:12:16 -04:00
|
|
|
await this.client.axiosCustom.delete(`/channels/${thread.id}`);
|
2023-04-07 19:13:15 -04:00
|
|
|
const newChannel = this.cache.get(thread.id);
|
|
|
|
|
this.cache.delete(thread.id);
|
|
|
|
|
resolve(newChannel);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-07 20:12:16 -04:00
|
|
|
constructor(o, guild, client) {
|
|
|
|
|
super(client);
|
2023-04-07 19:13:15 -04:00
|
|
|
this.cache = new Map();
|
|
|
|
|
|
|
|
|
|
for (const k of o) {
|
2023-04-07 20:12:16 -04:00
|
|
|
const newThread = new Thread(k, guild, client);
|
2023-04-07 19:13:15 -04:00
|
|
|
this.cache.set(newThread.id, newThread);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|