diff --git a/languages/en.json b/languages/en.json index 5fc89d3..1704a48 100644 --- a/languages/en.json +++ b/languages/en.json @@ -1,6 +1,5 @@ { "oronder.Auth-Token": "Auth Token", - "oronder.Discord-Server-Id": "Discord Server Id", "oronder.Fetch-Discord-User-Ids": "Fetch Discord User Ids", "oronder.Full-Sync": "Manually Sync All Actors", "oronder.Fetching-Discord-Ids": "Fetching Discord Ids", @@ -10,7 +9,6 @@ "oronder.Discord-Ids": "Discord Ids", "oronder.Oronder-Bot-Config": "Oronder Bot Config", "oronder.Invalid-Auth": "Invalid Server Id and/or Auth Token. Have you run \"/admin init\" in Discord?", - "oronder.Server-Id-NaN": "Discord Server Id must be a number.", "oronder.Unexpected-Error": "Unexpected Error Occurred!", "oronder.Could-Not-Be-Found": "could not be found in Discord server.", "oronder.Discord-Server-Link-Title": "Oronder Discord", diff --git a/src/constants.mjs b/src/constants.mjs index a4dd2a9..4119d5f 100644 --- a/src/constants.mjs +++ b/src/constants.mjs @@ -6,14 +6,15 @@ export const MODULE_DEBUG_TAG = [ `|`, ]; -export const GUILD_ID = "guild_id" +export const GUILD_NAME = "guild_name" export const AUTH = "auth" export const ORONDER_CONFIGURATION_FORM = "oronder_options" export const VALID_CONFIG = "valid_config" export const ID_MAP = "id_map" -export const ACTORS = "actors" +export const ACTORS = `${MODULE_ID}.actors` -const common = window.location.host === 'localhost:65434' ? '://localhost:65435' : 's://api.oronder.com' +export const DEV_MODE = window.location.host === 'localhost:65434' +const common = DEV_MODE ? '://localhost:65435' : 's://api.oronder.com' export const ORONDER_BASE_URL = `http${common}` export const ORONDER_WS_URL = `ws${common}` \ No newline at end of file diff --git a/src/module.mjs b/src/module.mjs index d727321..7da9ced 100644 --- a/src/module.mjs +++ b/src/module.mjs @@ -1,6 +1,6 @@ import {Logger} from "./util.mjs" import {registerSettings} from "./settings.mjs" -import {AUTH, GUILD_ID, MODULE_ID, ORONDER_WS_URL} from "./constants.mjs" +import {AUTH, MODULE_ID, ORONDER_WS_URL} from "./constants.mjs" import {del_actor, sync_actor} from "./sync.mjs" let socket @@ -69,13 +69,12 @@ export function open_socket_with_oronder(update = false) { } } - const guild_id = game.settings.get(MODULE_ID, GUILD_ID) const authorization = game.settings.get(MODULE_ID, AUTH) - if (!guild_id || !authorization) return + if (!authorization) return socket = io(ORONDER_WS_URL, { transports: ["websocket"], - auth: {'Guild-Id': guild_id, 'Authorization': authorization} + auth: {'Authorization': authorization} }) socket.on('connect', () => { diff --git a/src/settings-form-application.mjs b/src/settings-form-application.mjs index 0090655..5e70fc2 100644 --- a/src/settings-form-application.mjs +++ b/src/settings-form-application.mjs @@ -1,5 +1,5 @@ import {Logger} from "./util.mjs"; -import {AUTH, GUILD_ID, ID_MAP, MODULE_ID, ORONDER_BASE_URL, VALID_CONFIG} from "./constants.mjs"; +import {AUTH, GUILD_NAME, ID_MAP, MODULE_ID, ORONDER_BASE_URL, VALID_CONFIG} from "./constants.mjs"; import {full_sync} from "./sync.mjs"; import {open_socket_with_oronder} from "./module.mjs"; @@ -8,7 +8,7 @@ export class OronderSettingsFormApplication extends FormApplication { constructor(object = {}, options = {}) { const id_map = game.settings.get(MODULE_ID, ID_MAP) foundry.utils.mergeObject(object, { - guild_id: game.settings.get(MODULE_ID, GUILD_ID), + guild_name: game.settings.get(MODULE_ID, GUILD_NAME), auth: game.settings.get(MODULE_ID, AUTH), valid_config: game.settings.get(MODULE_ID, VALID_CONFIG), fetch_button_icon: "fa-solid fa-rotate", @@ -62,12 +62,11 @@ export class OronderSettingsFormApplication extends FormApplication { } } - _requestOptions(guild_id, auth) { + _getRequestOptions(auth) { return { method: 'GET', headers: new Headers({ "Accept": "application/json", - 'Guild-Id': guild_id, 'Authorization': auth }), redirect: 'follow' @@ -76,7 +75,6 @@ export class OronderSettingsFormApplication extends FormApplication { /** @override */ async _updateObject(event, formData) { - this.object.guild_id = this.form.elements.guild_id.value this.object.auth = this.form.elements.auth.value const id_map = {} @@ -88,7 +86,7 @@ export class OronderSettingsFormApplication extends FormApplication { id_map[p.foundry_id] = p.discord_id } }) - const requestOptions = this._requestOptions(this.object.guild_id, this.object.auth) + const requestOptions = this._getRequestOptions(this.object.auth) let valid_config = false await fetch(`${ORONDER_BASE_URL}/validate_discord_ids?${queryParams}`, requestOptions) .then(this._handle_json_response) @@ -106,16 +104,17 @@ export class OronderSettingsFormApplication extends FormApplication { }) .catch(Logger.error) + if (!this.object.guild_name) { + this._set_guild_name(requestOptions) + } + let updated = false - if (game.settings.get(MODULE_ID, GUILD_ID) !== this.object.guild_id) { - game.settings.set(MODULE_ID, GUILD_ID, this.object.guild_id) - updated = true - } if (game.settings.get(MODULE_ID, AUTH) !== this.object.auth) { game.settings.set(MODULE_ID, AUTH, this.object.auth) updated = true } + game.settings.set(MODULE_ID, GUILD_NAME, this.object.guild_name) game.settings.set(MODULE_ID, VALID_CONFIG, valid_config) game.settings.set(MODULE_ID, ID_MAP, id_map) @@ -137,7 +136,6 @@ export class OronderSettingsFormApplication extends FormApplication { } async _fetch_discord_ids() { - this.object.guild_id = this.form.elements.guild_id.value this.object.auth = this.form.elements.auth.value this.object.players.forEach(p => p.discord_id = this.form.elements[p.foundry_id].value @@ -148,10 +146,6 @@ export class OronderSettingsFormApplication extends FormApplication { ) let err = false - if (!Number.fromString(this.object.guild_id)) { - err = true - Logger.error(game.i18n.localize("oronder.Server-Id-NaN")) - } if (!this.object.auth) { err = true Logger.error(game.i18n.localize("oronder.Auth-Token-Empty")) @@ -174,9 +168,10 @@ export class OronderSettingsFormApplication extends FormApplication { players_without_discord_ids.forEach(p => queryParams.append('p', p.foundry_name) ) - const requestOptions = this._requestOptions(this.object.guild_id, this.object.auth) + const requestOptions = this._getRequestOptions(this.object.auth) - await fetch(`${ORONDER_BASE_URL}/discord_id?${queryParams}`, requestOptions) + const p1 = this._set_guild_name(requestOptions) + const p2 = fetch(`${ORONDER_BASE_URL}/discord_id?${queryParams}`, requestOptions) .then(this._handle_json_response) .then(result => { for (const [foundry_name, discord_user_id] of Object.entries(result)) { @@ -185,21 +180,40 @@ export class OronderSettingsFormApplication extends FormApplication { }) .catch(Logger.error) + await Promise.all([p1, p2]) + this.object.fetch_button_icon = "fa-solid fa-rotate" this.object.fetch_sync_disabled = false this.render() } - _handle_json_response(response) { + async _handle_json_response(response) { if (!response.ok) { + const errorMessage = await response.text(); + const errorDetails = `Status: ${response.status}, Message: ${errorMessage}` + if (response.status === 401) { - throw new Error(game.i18n.localize("oronder.Invalid-Auth")) - } else if (response.status === 400) { - throw new Error(game.i18n.localize("oronder.Server-Id-NaN")) + throw new Error(game.i18n.localize("oronder.Invalid-Auth") + ". " + errorDetails) } else { - throw new Error(game.i18n.localize("oronder.Unexpected-Error")) + throw new Error(game.i18n.localize("oronder.Unexpected-Error") + ". " + errorDetails) } } - return response.json() + + try { + return await response.json() + } catch (error) { + throw new Error(`Failed to parse JSON response. Error: ${error.message}`) + } + } + + async _set_guild_name(requestOptions) { + try { + const response = await fetch(`${ORONDER_BASE_URL}/guild`, requestOptions) + const guild = await this._handle_json_response(response) + this.object.guild_name = guild.name + } catch (error) { + this.object.guild_name = undefined + Logger.error(`Error setting guild name: ${error.message}`) + } } } \ No newline at end of file diff --git a/src/settings.mjs b/src/settings.mjs index 7c2cb38..2c0cede 100644 --- a/src/settings.mjs +++ b/src/settings.mjs @@ -1,9 +1,9 @@ -import {AUTH, GUILD_ID, ID_MAP, MODULE_ID, ORONDER_CONFIGURATION_FORM, VALID_CONFIG} from "./constants.mjs"; +import {AUTH, GUILD_NAME, ID_MAP, MODULE_ID, ORONDER_CONFIGURATION_FORM, VALID_CONFIG} from "./constants.mjs"; import {Logger} from "./util.mjs"; import {OronderSettingsFormApplication} from "./settings-form-application.mjs"; export const registerSettings = async () => { - game.settings.register(MODULE_ID, GUILD_ID, { + game.settings.register(MODULE_ID, GUILD_NAME, { scope: "world", type: String, config: false diff --git a/src/sync.mjs b/src/sync.mjs index fcf6f1b..9a1a6b8 100644 --- a/src/sync.mjs +++ b/src/sync.mjs @@ -1,4 +1,4 @@ -import {ACTORS, AUTH, GUILD_ID, ID_MAP, MODULE_ID, ORONDER_BASE_URL} from "./constants.mjs"; +import {ACTORS, AUTH, ID_MAP, MODULE_ID, ORONDER_BASE_URL} from "./constants.mjs"; import {hash, Logger} from "./util.mjs"; function prune_roll_data( @@ -152,12 +152,10 @@ export function enrich_actor(actor) { } function headers() { - const guild_id = game.settings.get(MODULE_ID, GUILD_ID) const authorization = game.settings.get(MODULE_ID, AUTH) return new Headers({ "Accept": "application/json", "Content-Type": "application/json", - 'Guild-Id': guild_id, 'Authorization': authorization }) } diff --git a/templates/settings-form-application.hbs b/templates/settings-form-application.hbs index 66cd16a..abfa8d3 100644 --- a/templates/settings-form-application.hbs +++ b/templates/settings-form-application.hbs @@ -1,63 +1,60 @@
-
- {{#unless valid_config}} -

Installation Instructions

-
    -
  1. Invite the - {{localize "oronder.Discord-Bot-Link-Title"}} to your Discord. -
  2. -
  3. Subscribe to the - {{localize "oronder.Discord-Server-Link-Title"}} - to gain access to advanced features. -
  4. -
  5. From your Discord, run "/admin init"
  6. -
  7. Copy your Discord server id, and the generated token into the fields below.
  8. -
  9. Clicking "{{localize "oronder.Fetch-Discord-User-Ids"}}" will populate Discord user ids for - players whose Discord name - matches their Foundry name. For everyone else, you will need to manually add their user id. -
  10. -
-
- {{/unless}} +
+

{{#if guild_name}}{{guild_name}}{{else}}No Discord Server Configured{{/if}}

+ {{#unless valid_config}} +

Installation Instructions

+
    +
  1. Invite the + {{localize "oronder.Discord-Bot-Link-Title"}} to your Discord. +
  2. +
  3. Subscribe to the + {{localize "oronder.Discord-Server-Link-Title"}} + to gain access to advanced features. +
  4. +
  5. From your Discord, run "/admin init"
  6. +
  7. Copy the generated token into the fields below.
  8. +
  9. Clicking "{{localize "oronder.Fetch-Discord-User-Ids"}}" will populate Discord user ids + for + players whose Discord name + matches their Foundry name. For everyone else, you will need to manually add their user id. +
  10. +
+
+ {{/unless}} +
+
+ + +
-
-
- - -
+
+ +
+
+ +
-
- - -
+ {{#if players}} +
+

{{localize "oronder.Discord-Ids"}}

+ {{/if}} -
- -
-
- -
+ {{#each players}} +
+ + +
+ {{/each}} - {{#if players}}
-

{{localize "oronder.Discord-Ids"}}

- {{/if}} - - {{#each players}} -
- - -
- {{/each}} - -
- +
\ No newline at end of file