Skip to content
This repository was archived by the owner on Aug 10, 2025. It is now read-only.

Commit e097122

Browse files
committed
add permission setting for each script chat card button
1 parent 726688e commit e097122

4 files changed

Lines changed: 34 additions & 29 deletions

File tree

scripts/applications/script-config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export class ScriptConfig extends FormApplication {
4242
const context = super.getData();
4343

4444
context.data = this.script;
45+
context.buttonPermissionChoices = Object.fromEntries(
46+
Object.entries(CONST.DOCUMENT_OWNERSHIP_LEVELS).filter(([k, v]) =>
47+
this.script.schema.fields.buttonPermission.choices.includes(v),
48+
),
49+
);
4550

4651
context.triggers = Object.values(TRIGGER).reduce((acc, x) => {
4752
acc[x] = {

scripts/data/script-model.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ export class ScriptModel extends foundry.abstract.DataModel {
4141
label: "Trigger Set",
4242
},
4343
),
44+
buttonPermission: new fields.NumberField({
45+
required: true,
46+
integer: true,
47+
positive: true,
48+
choices: [
49+
CONST.DOCUMENT_OWNERSHIP_LEVELS.OWNER,
50+
CONST.DOCUMENT_OWNERSHIP_LEVELS.OBSERVER,
51+
CONST.DOCUMENT_OWNERSHIP_LEVELS.LIMITED,
52+
],
53+
initial: CONST.DOCUMENT_OWNERSHIP_LEVELS.OWNER,
54+
}),
4455
buttonIconClasses: new fields.StringField({
4556
required: false,
4657
blank: true,

scripts/scriptable-items.mjs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ Hooks.on("dnd5e.renderChatMessage", async (message, html) => {
6262
const item = message.getAssociatedItem();
6363
if (!item) return;
6464

65-
const scripts = ScriptModel.getAll(item).filter((x) =>
66-
x.triggers.has(TRIGGER.BUTTON),
67-
);
65+
const scripts = ScriptModel.getAll(item).filter((x) => {
66+
return (
67+
x.triggers.has(TRIGGER.BUTTON) &&
68+
item.testUserPermission(game.user, x.buttonPermission)
69+
);
70+
});
6871
if (!scripts.length) return;
6972

7073
// Add button for each script with button trigger to the card

templates/script-config.hbs

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,37 @@
11
<form class="{{cssClass}} flexcol script-config" autocomplete="off">
22
<header class="sheet-header">
3-
<img
4-
src="{{data.img}}"
5-
data-edit="img"
6-
title="{{data.name}}"
7-
height="64"
8-
width="64"
9-
/>
10-
<h1><input
11-
name="name"
12-
type="text"
13-
value="{{data.name}}"
14-
placeholder="{{localize 'Name'}}"
15-
/></h1>
3+
<img src="{{data.img}}" data-edit="img" title="{{data.name}}" height="64" width="64" />
4+
<h1><input name="name" type="text" value="{{data.name}}" placeholder="{{localize 'Name'}}" /></h1>
165
</header>
176

187
<div class="form-group">
198
<label>Triggers</label>
209
<multi-select name="triggers">
2110
{{#each triggers}}
22-
<option value={{@key}} {{selected}}>{{label}}</option>
11+
<option value={{@key}} {{selected}}>{{label}}</option>
2312
{{/each}}
2413
</multi-select>
2514
</div>
2615

2716
<details>
2817
<summary>Button Configuration</summary>
2918
<fieldset>
19+
<div class="form-group">
20+
<label>Required Permissions</label>
21+
<select name="buttonPermission">
22+
{{selectOptions buttonPermissionChoices selected=data.buttonPermission inverted=true}}
23+
</select>
24+
</div>
3025
<div class="form-group">
3126
<label>Icon</label>
32-
<input
33-
name="buttonIconClasses"
34-
type="text"
35-
value="{{data.buttonIconClasses}}"
36-
placeholder="fa-solid fa-code"
37-
/>
27+
<input name="buttonIconClasses" type="text" value="{{data.buttonIconClasses}}"
28+
placeholder="fa-solid fa-code" />
3829
<p class="hint">CSS classes used for the icon of the button. If
3930
empty, no icon will be shown.</p>
4031
</div>
4132
<div class="form-group">
4233
<label>Text</label>
43-
<input
44-
name="buttonText"
45-
type="text"
46-
value="{{data.buttonText}}"
47-
placeholder="{{data.name}}"
48-
/>
34+
<input name="buttonText" type="text" value="{{data.buttonText}}" placeholder="{{data.name}}" />
4935
<p class="hint">Text displayed on the button. Is always right of
5036
the icon and defaults to the scripts name if left empty.</p>
5137
</div>

0 commit comments

Comments
 (0)