fix: skip problematic name-value pairs in encodeCQCode to prevent undefined errors

Added logic to skip name-value pairs in encodeCQCode when value cannot be converted to string, preventing errors caused by undefined values. This ensures the function can handle such cases gracefully and continue processing other valid data.
This commit is contained in:
夏影 2024-07-20 00:49:34 +08:00
parent bc4511e175
commit c9374ff515

View File

@ -61,6 +61,14 @@ export function encodeCQCode(data: OB11MessageData) {
let result = '[CQ:' + data.type
for (const name in data.data) {
const value = data.data[name]
try {
// Check if the value can be converted to a string
value.toString();
} catch (error) {
// If it can't be converted, skip this name-value pair
// console.warn(`Skipping problematic name-value pair. Name: ${name}, Value: ${value}`);
continue;
}
result += `,${name}=${CQCodeEscape(value)}`
}
result += ']'