Add "webhook" storage method

this methods allows for storing messageIDs in Discord message
This commit is contained in:
Wroclaw 2024-02-21 18:03:45 +01:00
parent a34b52e1d0
commit 8d8bd0fbb8
5 changed files with 76 additions and 5 deletions

View file

@ -17,10 +17,15 @@ inputs:
one of:
- none - doesn't store ids of sent messages
- git - stores ids in git repository
- webhook - stores in message sent by another webhook
default: none
required: true
storegeGitFileLocation:
description: "Where in git repository file should be saved"
storageWebhookUrl:
description: "Webhook url used to send and read message containing messageIDs"
storageWebhookMessageID:
description: "Message ID to read and edit"
runs:
using: node20

35
dist/index.js vendored
View file

@ -129106,7 +129106,7 @@ async function main() {
core.info(`Messages sent! IDs:\n${messageIDsConcatenated}`);
await promises_1.default.writeFile(core.getInput("outputFileLocation"), messageIDsConcatenated);
await Promise.all([
storage_1.default.pushMessageIDs(),
storage_1.default.pushMessageIDs(messageIDs),
presentMessageIds.map(v => client.deleteMessage(v)),
].flat());
}
@ -129154,6 +129154,7 @@ exports.pushMessageIDs = exports.getMessageIDs = void 0;
const promises_1 = __importDefault(__nccwpck_require__(93977));
const core = __importStar(__nccwpck_require__(42186));
const exec_1 = __nccwpck_require__(71514);
const discord_js_1 = __nccwpck_require__(85973);
function exec(command, options) {
const splitted = command.trim().split(/ +/g);
if (splitted.length === 0)
@ -129163,6 +129164,11 @@ function exec(command, options) {
function getStorageMethod() {
return core.getInput("storageMethod", { required: true });
}
function getStorageWebhook() {
return new discord_js_1.WebhookClient({
url: core.getInput("storageWebhookUrl", { required: true }),
});
}
/** @returns array of messageIDs */
async function getMessageIDs() {
const method = getStorageMethod();
@ -129178,12 +129184,20 @@ async function getMessageIDs() {
core.warning("Continuing anyway");
return [];
});
case "webhook":
return getStorageWebhook()
.fetchMessage(core.getInput("storageWebhookMessageID"))
.then(v => v.content.split(", "))
.catch(e => {
core.warning("Couldn't read messageIDs");
return [];
});
default:
throw new Error(`Storage method is unknown: ${method}`);
}
}
exports.getMessageIDs = getMessageIDs;
async function pushMessageIDs() {
async function pushMessageIDs(messageIDs) {
const method = getStorageMethod();
switch (method) {
case "git":
@ -129197,6 +129211,23 @@ async function pushMessageIDs() {
return true;
case "none":
return false;
case "webhook":
const content = messageIDs.join(", ");
try {
const id = core.getInput("storageWebhookMessageID");
if (id === "" || isNaN(Number(id)))
throw new Error("Invalid argument provided for storageWebhookMessageID");
await getStorageWebhook().editMessage(core.getInput("storageWebhookMessageID"), content);
core.info("Message for storage edited!");
}
catch (e) {
core.warning("Couldn't edit storage message");
core.warning(e);
core.warning("Sending a new storage message");
const message = await getStorageWebhook().send(messageIDs.join(", "));
core.info(`Message for storage sent! ID: ${message.id}`);
}
return true;
default:
throw new Error(`Storage method is unknown: ${method}`);
}

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -102,7 +102,7 @@ async function main() {
await fs.writeFile(core.getInput("outputFileLocation"), messageIDsConcatenated);
await Promise.all([
storage.pushMessageIDs(),
storage.pushMessageIDs(messageIDs),
presentMessageIds.map(v => client.deleteMessage(v)),
].flat()
);

View file

@ -2,6 +2,7 @@ import fs from "node:fs/promises";
import * as core from "@actions/core";
import { exec as _exec, type ExecOptions } from "@actions/exec";
import { WebhookClient } from "discord.js";
function exec(command: string, options?: ExecOptions) {
const splitted = command.trim().split(/ +/g);
@ -14,6 +15,12 @@ function getStorageMethod(): string {
return core.getInput("storageMethod", {required: true})
}
function getStorageWebhook(): WebhookClient {
return new WebhookClient({
url: core.getInput("storageWebhookUrl", { required: true }),
});
}
/** @returns array of messageIDs */
export async function getMessageIDs(): Promise<string[]> {
const method = getStorageMethod();
@ -29,12 +36,20 @@ export async function getMessageIDs(): Promise<string[]> {
core.warning("Continuing anyway");
return [];
})
case "webhook":
return getStorageWebhook()
.fetchMessage(core.getInput("storageWebhookMessageID"))
.then(v => v.content.split(", "))
.catch(e => {
core.warning("Couldn't read messageIDs");
return [];
});
default:
throw new Error(`Storage method is unknown: ${method}`);
}
}
export async function pushMessageIDs() {
export async function pushMessageIDs(messageIDs: string[]) {
const method = getStorageMethod();
switch (method) {
case "git":
@ -51,6 +66,26 @@ export async function pushMessageIDs() {
return true;
case "none":
return false;
case "webhook":
const content = messageIDs.join(", ");
try {
const id = core.getInput("storageWebhookMessageID");
if (id === "" || isNaN(Number(id)))
throw new Error("Invalid argument provided for storageWebhookMessageID");
await getStorageWebhook().editMessage(
core.getInput("storageWebhookMessageID"),
content
)
core.info("Message for storage edited!")
}
catch (e) {
core.warning("Couldn't edit storage message");
core.warning(e as Error);
core.warning("Sending a new storage message");
const message = await getStorageWebhook().send(messageIDs.join(", "));
core.info(`Message for storage sent! ID: ${message.id}`);
}
return true;
default:
throw new Error(`Storage method is unknown: ${method}`);
}