Skip to content

Commit bf22c6d

Browse files
committed
support unarchiving
1 parent e74ad8c commit bf22c6d

1 file changed

Lines changed: 40 additions & 6 deletions

File tree

src/undiscord-core.js

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,8 @@ class UndiscordCore {
404404

405405
if (resp.status === 400 && r.code === 50083) {
406406
// 400 can happen if the thread is archived (code=50083)
407-
// in this case we need to "skip" this message from the next search
408-
// otherwise it will come up again in the next page (and fail to delete again)
409-
log.warn('Error deleting message (Thread is archived). Will increment offset so we don\'t search this in the next page...');
410-
this.state.offset++;
411-
this.state.failCount++;
412-
return 'FAIL_SKIP'; // Failed but we will skip it next time
407+
log.warn('Error deleting message (Thread is archived). Attempting to unarchive...');
408+
return await this.unarchiveThread(message.channel_id);
413409
}
414410

415411
log.error(`Error deleting message, API responded with status ${resp.status}!`, r);
@@ -426,6 +422,44 @@ class UndiscordCore {
426422
return 'OK';
427423
}
428424

425+
async unarchiveThread(channel_id) {
426+
// attempts to unarchive the thread located at the provided channel_id.
427+
// this method is meant to be run inside of deleteMessage(), as the response
428+
// indicates how retries should behave (⌒ω⌒)ノ
429+
const API_CHANNELS_URL = `https://discord.com/api/v9/channels/${channel_id}`;
430+
let resp;
431+
try {
432+
this.beforeRequest();
433+
resp = await fetch(API_CHANNELS_URL, {
434+
method: 'PATCH',
435+
headers: {
436+
'Authorization': this.options.authToken,
437+
'Content-Type': 'application/json',
438+
'Accept': 'application/json'
439+
},
440+
body: JSON.stringify({'archived': false}),
441+
});
442+
this.afterRequest();
443+
} catch (err) {
444+
// no response error (e.g. network error)
445+
log.error('Unarchive request threw an error:', err);
446+
log.verb('Related object:', redact(JSON.stringify(message)));
447+
this.state.failCount++;
448+
return 'FAILED';
449+
}
450+
451+
if (!resp.ok) {
452+
log.error('Unarchive request returned not-okay response:', resp);
453+
log.warn('This message will be skipped');
454+
this.state.offset++;
455+
this.state.failCount++;
456+
return 'FAIL_SKIP';
457+
}
458+
459+
log.verb('Thread successfully unarchived');
460+
return 'RETRY';
461+
}
462+
429463
#beforeTs = 0; // used to calculate latency
430464
beforeRequest() {
431465
this.#beforeTs = Date.now();

0 commit comments

Comments
 (0)