diff --git a/apps/bym.js b/apps/bym.js index 34196ae..7400d91 100644 --- a/apps/bym.js +++ b/apps/bym.js @@ -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 } diff --git a/utils/common.js b/utils/common.js index 7f1ecf4..34f9821 100644 --- a/utils/common.js +++ b/utils/common.js @@ -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() +} diff --git a/utils/group.js b/utils/group.js index 40aceb0..ed6c029 100644 --- a/utils/group.js +++ b/utils/group.js @@ -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