generated from city-unit/st-extension-example
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.js
133 lines (118 loc) · 3.8 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { getContext, extension_settings } from "../../../extensions.js";
import { extensionName, VN_MODES } from "./constants.js";
/**
* Returns the last character chat message
* @returns {object} - The last character chat message
*/
export function getLastChatMessage() {
const context = getContext();
const reversedChat = context.chat.slice().reverse();
return reversedChat.filter((mes) => !mes.is_system && !mes.extra?.image);
}
/**
* Lists out the characters that have spoken recently
* @param {number?} limit - The number of characters to return
* @returns {string[]} - An array of character original avatars
*/
export function getRecentTalkingCharacters(limit) {
const context = getContext();
const reversedChat = context.chat.slice().reverse();
const activeGroup = context.groups.find((x) => x.id === context.groupId);
const members = activeGroup?.members;
// Filter out system messages, images, user messages, and inactive/removed characters
const talkingCharacters = reversedChat
.filter((mes) => !mes.is_system && !mes.extra?.image && !mes.is_user)
.map((mes) => mes.original_avatar)
.filter((avatar) => members?.some((member) => member === avatar));
// Purge duplicates
const uniqueTalkingCharacters = [...new Set(talkingCharacters)];
if (limit) {
// Limit the number of characters to return
// If the number of characters is less than the limit, return all characters
if (uniqueTalkingCharacters.length < limit) {
return uniqueTalkingCharacters;
}
return uniqueTalkingCharacters.slice(0, limit);
}
return uniqueTalkingCharacters;
}
/**
* Returns the current chat ID
* @returns {string} - The current chat ID
*/
export function getChatId() {
const context = getContext();
return context.getCurrentChatId();
}
/**
* Returns whether letterbox mode is enabled
* @returns {boolean} - Whether letterbox mode is enabled
*/
export function isLetterboxModeEnabled() {
return Boolean(
extension_settings[extensionName].letterboxMode !== VN_MODES.NONE,
);
}
/**
* Returns whether the chat box (sheld) is visible
* @returns {boolean} - Whether the chat box is visible
*/
export function isSheldVisible() {
return Boolean(!extension_settings[extensionName].hideSheld);
}
/**
* Returns whether user sprites are enabled
* @returns {boolean} - Whether user sprites are enabled
*/
export function isUserSpriteEnabled() {
return Boolean(extension_settings[extensionName].enableUserSprite);
}
/**
* Returns whether auto-hide sprites are enabled
* @returns {boolean} - Whether sprites should be auto-hidden
*/
export function isAutoHideSpritesEnabled() {
return Boolean(extension_settings[extensionName].autoHideSprites);
}
export async function getSpriteList(name) {
try {
const result = await fetch(
`/api/sprites/get?name=${encodeURIComponent(name)}`,
);
const sprites = result.ok ? await result.json() : [];
return sprites;
} catch (err) {
console.log(err);
return [];
}
}
/**
* Returns the index of the current group (or -1 if not found)
* @returns {number} - The index of the current group
*/
export function getGroupIndex() {
const context = getContext();
const groupIndex = context.groups.findIndex((x) => {
return x.id === context.groupId;
});
return groupIndex;
}
/**
* Returns whether the current chat is a group chat
* @returns {boolean} - Whether the current chat is a group chat
*/
export function isGroupChat() {
const context = getContext();
return context.groupId !== null;
}
/**
* Check if the member is disabled in the group chat
* @param {string} name - The member name
* @returns {boolean} - Whether the member is disabled in the group chat
*/
export function isDisabledMember(name) {
const context = getContext();
const group = context.groups.find((x) => x.id === context.groupId);
if (!group) return false;
return group.disabled_members.includes(name);
}