Skip to content

Instantly share code, notes, and snippets.

@thebwt
Created September 25, 2023 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thebwt/71c15f8d463651fa50deaff41f233ca7 to your computer and use it in GitHub Desktop.
Save thebwt/71c15f8d463651fa50deaff41f233ca7 to your computer and use it in GitHub Desktop.
Elevenlabs Foundry - Conversation HUD integration
// Check if a token is selected
let selectedActor = game.actors.get(canvas.tokens.controlled[0]?.actor?.id);
if (!selectedActor) {
ui.notifications.warn("You must select a token first.");
return;
}
// Fetch the voice name from the custom flag, or prompt for a new one if not set
let voiceName = selectedActor.getFlag("world", "voiceName");
if (!voiceName) {
let dialogContent = `
<form>
<div class="form-group">
<label for="voiceName">Set Voice Name:</label>
<input id="voiceName" name="voiceName" type="text"/>
</div>
</form>
`;
new Dialog({
title: "Set Voice for Actor",
content: dialogContent,
buttons: {
set: {
label: "Set Voice",
callback: async (html) => {
voiceName = html.find("#voiceName")[0].value;
await selectedActor.setFlag("world", "voiceName", voiceName);
ui.notifications.info(`Voice for ${selectedActor.name} set to ${voiceName}`);
}
}
},
default: "set",
close: () => {
if (voiceName) {
// If voiceName is set, proceed with the Sound Text Input
showSoundTextInput();
}
}
}).render(true);
} else {
showSoundTextInput();
}
function showSoundTextInput() {
// Create dialog content for Sound Text Input
let dialogContent = `
<form>
<div class="form-group">
<label for="soundText">Enter the sound text:</label>
<input id="soundText" name="soundText" type="text"/>
</div>
</form>
`;
// Create a dialog for Sound Text Input
new Dialog({
title: "Sound Text Input",
content: dialogContent,
buttons: {
ok: {
label: "Submit",
callback: (html) => {
// Get the sound text from the dialog
const text = html.find("#soundText")[0].value;
// Use the voice name from the custom flag or the newly set voice
ui.chat.processMessage(`/playsound [${voiceName}] ${text}`);
// Make the actor "speak" in the chat
ChatMessage.create({
speaker: {
alias: selectedActor.name,
actor: selectedActor.id
},
content: text
});
}
},
cancel: {
label: "Cancel",
callback: () => console.log("Sound text input cancelled")
}
},
default: "ok",
render: html => {
// Automatically focus on the text input
html.find("#soundText").focus();
}
}).render(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment