Skip to content

Commit

Permalink
细节优化
Browse files Browse the repository at this point in the history
  • Loading branch information
TimeRainStarSky committed Apr 7, 2024
1 parent 4f93dad commit bce3d70
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,32 @@ export default class Yunzai extends EventEmitter {
return map
}

StringOrNull(data) {
if (typeof data == "object" && typeof data.toString != "function")
return "[object null]"
return String(data)
}

getCircularReplacer() {
const _this_ = this, ancestors = []
return function (key, value) {
switch (typeof value) {
case "function":
return String(value)
case "object":
if (value !== null) break
default:
return value
}
while (ancestors.length > 0 && ancestors.at(-1) !== this)
ancestors.pop()
if (ancestors.includes(value))
return `[Circular ${_this_.StringOrNull(value)}]`
ancestors.push(value)
return value
}
}

String(data) {
switch (typeof data) {
case "string":
Expand All @@ -255,22 +281,15 @@ export default class Yunzai extends EventEmitter {
}

try {
return JSON.stringify(data)
return JSON.stringify(data, this.getCircularReplacer())
} catch (err) {
if (typeof data.toString == "function")
return String(data)
else
return "[object null]"
return this.StringOrNull(data)
}
}

Loging(data) {
if (typeof data == "string") return data
if (!cfg.bot.log_object && typeof data == "object")
if (typeof data.toString == "function")
return String(data)
else
return "[object null]"
if (!cfg.bot.log_object) return this.StringOrNull(data)

return util.inspect(data, {
depth: 10,
Expand Down

0 comments on commit bce3d70

Please sign in to comment.