ds4/src/module/macros/roll-check.ts

44 lines
1.6 KiB
TypeScript
Raw Normal View History

import { DS4Actor } from "../actor/actor";
import { Check } from "../actor/actor-prepared-data";
import { DS4 } from "../config";
import { getCanvas } from "../helpers";
import notifications from "../ui/notifications";
/**
* Creates a macro from a check drop.
* Get an existing roll check macro if one exists, otherwise create a new one.
* @param check - The name of the check to perform.
* @param slot - The hotbar slot to use.
*/
export async function createRollCheckMacro(check: Check, slot: string): Promise<void> {
const command = `game.ds4.macros.rollCheck("${check}");`;
const macro =
game.macros?.entities.find((m) => m.name === DS4.i18n.checks[check] && m.data.command === command) ??
(await Macro.create(
{
command,
name: DS4.i18n.checks[check],
type: "script",
// TODO: img, should be addressed in https://git.f3l.de/dungeonslayers/ds4/-/issues/79
flags: { "ds4.checkMacro": true },
},
{ displaySheet: false },
));
game.user?.assignHotbarMacro(macro, slot);
}
/**
* Executes the roll check macro for the given check.
*/
export async function rollCheck(check: Check): Promise<void> {
const speaker = ChatMessage.getSpeaker();
const actor = (getCanvas().tokens.get(speaker.token ?? "")?.actor ?? game.actors?.get(speaker.actor ?? "")) as
| DS4Actor
| null
| undefined;
if (!actor) {
return notifications.warn(game.i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro"));
}
return actor.rollCheck(check);
}