Skip to content

Commit

Permalink
Try to delete kmail first and fail early if that doesn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
gausie committed Jul 28, 2024
1 parent a990e61 commit ae3d5fc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/kol.js/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,17 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<E
}

async deleteKmails(ids: number[]) {
await this.fetchText("messages.php", {
const response = await this.fetchText("messages.php", {
searchParams: {
the_action: "delete",
box: "Inbox",
...Object.fromEntries(ids.map((id) => [`sel${id}`, "on"])),
pwd: true,
},
});
return response.includes(
`<td>${ids.length} message${ids.length === 1 ? "" : "s"} deleted.</td>`,
);
}

async checkKmails() {
Expand Down
11 changes: 9 additions & 2 deletions packages/oaf/src/greenbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import { kolClient } from "./clients/kol.js";
export async function handleGreenboxKmail(message: KoLMessage) {
if (message.type !== "kmail") return;

const deleted = await kolClient.deleteKmails([message.id]);

if (!deleted) {
await discordClient.alert(
`Could not delete greenbox message ahead of action ${message.msg}`,
);
return;
}

// Remove spaces - KoL adds weird spaces to long messages.
const text = message.msg.replace(/ /g, "").slice(9);

Expand All @@ -18,8 +27,6 @@ export async function handleGreenboxKmail(message: KoLMessage) {
await update(message.who.id, message.who.name, text);
break;
}

await kolClient.deleteKmails([message.id]);
}

async function update(
Expand Down

0 comments on commit ae3d5fc

Please sign in to comment.