fix: time

This commit is contained in:
ikechan8370 2025-04-07 00:33:35 +08:00
parent 7be0e61e14
commit 3318ac04d8
3 changed files with 65 additions and 1 deletions

View file

@ -3,6 +3,7 @@ import { Chaite } from 'chaite'
import { intoUserMessage, toYunzai } from '../utils/message.js'
import common from '../../../lib/common/common.js'
import { getGroupContextPrompt } from '../utils/group.js'
import {formatTimeToBeiJing} from '../utils/common.js'
export class bym extends plugin {
constructor () {
@ -66,6 +67,7 @@ export class bym extends plugin {
}
sendMessageOption.systemOverride = ChatGPTConfig.bym.presetPrefix + sendMessageOption.systemOverride
}
sendMessageOption.systemOverride = `Current Time: ${formatTimeToBeiJing(new Date().getTime())}\n` + sendMessageOption.systemOverride
if (ChatGPTConfig.bym.temperature >= 0) {
sendMessageOption.temperature = ChatGPTConfig.bym.temperature
}

View file

@ -2,3 +2,64 @@ import * as crypto from 'node:crypto'
export function md5 (str) {
return crypto.createHash('md5').update(str).digest('hex')
}
/**
* Converts a timestamp to Beijing time (UTC+8)
* @param {number|string} timestamp - Timestamp in milliseconds or seconds
* @param {string} [format='YYYY-MM-DD HH:mm:ss'] - Output format
* @returns {string} Formatted Beijing time
*/
export function formatTimeToBeiJing (timestamp, format = 'YYYY-MM-DD HH:mm:ss') {
// Handle string timestamp
if (typeof timestamp === 'string') {
timestamp = parseInt(timestamp)
}
// Automatically determine if timestamp is in seconds or milliseconds
// If timestamp represents a date before 2000, assume it's in milliseconds
if (timestamp.toString().length <= 10) {
// Convert seconds to milliseconds
timestamp = timestamp * 1000
}
// Create date object with the timestamp
const date = new Date(timestamp)
// Calculate Beijing time (UTC+8)
const beijingTime = new Date(date.getTime() + 8 * 60 * 60 * 1000)
// Format the date according to the specified format
return formatDate(beijingTime, format)
}
/**
* Formats a Date object according to the specified format
* @param {Date} date - Date object to format
* @param {string} format - Format string (YYYY-MM-DD HH:mm:ss)
* @returns {string} Formatted date string
*/
function formatDate (date, format) {
const year = date.getUTCFullYear()
const month = padZero(date.getUTCMonth() + 1)
const day = padZero(date.getUTCDate())
const hours = padZero(date.getUTCHours())
const minutes = padZero(date.getUTCMinutes())
const seconds = padZero(date.getUTCSeconds())
return format
.replace('YYYY', year)
.replace('MM', month)
.replace('DD', day)
.replace('HH', hours)
.replace('mm', minutes)
.replace('ss', seconds)
}
/**
* Pads a number with leading zero if needed
* @param {number} num - Number to pad
* @returns {string} Padded number string
*/
function padZero (num) {
return num < 10 ? '0' + num : num.toString()
}

View file

@ -1,5 +1,6 @@
import { getBotFramework } from './bot.js'
import ChatGPTConfig from '../config/config.js'
import {formatTimeToBeiJing} from './common.js'
export class GroupContextCollector {
/**
@ -132,7 +133,7 @@ export async function getGroupContextPrompt (e, length) {
// eslint-disable-next-line no-template-curly-in-string
.replace('${message.sender.title}', sender.title || '-')
// eslint-disable-next-line no-template-curly-in-string
.replace('${message.time}', chat.time || '-')
.replace('${message.time}', formatTimeToBeiJing(chat.time) || '-')
// eslint-disable-next-line no-template-curly-in-string
.replace('${message.messageId}', chat.messageId || '-')
// eslint-disable-next-line no-template-curly-in-string