diff --git a/apps/entertainment.js b/apps/entertainment.js new file mode 100644 index 0000000..5feb6cb --- /dev/null +++ b/apps/entertainment.js @@ -0,0 +1,68 @@ +import plugin from '../../../lib/plugins/plugin.js' +import { Config } from '../utils/config.js' +import { generateHello } from '../utils/randomMessage.js' +import { segment } from 'oicq' +import { generateAudio } from '../utils/tts.js' + +export class Entertainment extends plugin { + constructor (e) { + super({ + name: 'ChatGPT-Plugin娱乐小功能', + dsc: 'ChatGPT-Plugin娱乐小功能', + event: 'message', + priority: 500, + task: [ + { + // 每半小时 + cron: '*/30 * * * *', + name: 'ChatGPT主动随机说话', + fnc: 'sendRandomMessage' + } + ], + rule: [ + { + reg: '^#(chatgpt|ChatGPT)打招呼', + fnc: 'sendMessage' + } + ] + }) + } + + async sendMessage (e) { + let groupId = e.msg.replace(/^#(chatgpt|ChatGPT)打招呼/, '') + groupId = parseInt(groupId) + let message = await generateHello() + let sendable = message + logger.info(`打招呼给群聊${groupId}:` + message) + if (Config.defaultUseTTS) { + let audio = await generateAudio(message, '随机') + console.log(audio) + sendable = segment.record(audio) + } + if (!groupId) { + await e.reply(sendable) + } else { + await Bot.sendGroupMsg(groupId, sendable) + } + } + + async sendRandomMessage () { + let toSend = Config.initiativeChatGroups || [] + for (let i = 0; i < toSend.length; i++) { + let groupId = parseInt(toSend[i]) + if (Bot.gl[groupId]) { + if (Math.floor(Math.random() * 100) < 10) { + let message = await generateHello() + if (Config.defaultUseTTS) { + let audio = await generateAudio(message, '随机') + await Bot.sendGroupMsg(groupId, segment.record(audio)) + } else { + await Bot.sendGroupMsg(groupId, message) + } + } + } else { + logger.warn('机器人不在要发送的群组里,忽略群' + groupId) + } + } + } +} diff --git a/guoba.support.js b/guoba.support.js index e9ad930..a33b919 100644 --- a/guoba.support.js +++ b/guoba.support.js @@ -351,6 +351,12 @@ export function supportGuoba () { min: 0, max: 2 } + }, + { + field: 'initiativeChatGroups', + label: '主动发起聊天群聊', + bottomHelpMessage: '在这些群聊里会不定时主动说一些随机的打招呼的话,用英文逗号隔开', + component: 'Input' } ], // 获取配置数据方法(用于前端填充显示数据) @@ -361,7 +367,7 @@ export function supportGuoba () { setConfigData (data, { Result }) { for (let [keyPath, value] of Object.entries(data)) { // 处理黑名单 - if (keyPath === 'blockWords' || keyPath === 'promptBlockWords') { value = value.toString().split(/[,,;;\|]/) } + if (keyPath === 'blockWords' || keyPath === 'promptBlockWords' || keyPath === 'initiativeChatGroups') { value = value.toString().split(/[,,;;\|]/) } if (Config[keyPath] != value) { Config[keyPath] = value } } return Result.ok({}, '保存成功~') diff --git a/utils/config.js b/utils/config.js index 8c08bef..8daf9b5 100644 --- a/utils/config.js +++ b/utils/config.js @@ -45,7 +45,8 @@ const defaultConfig = { noiseScale: 0.6, noiseScaleW: 0.668, lengthScale: 1.2, - version: 'v2.0.18' + initiativeChatGroups: [], + version: 'v2.0.19' } const _path = process.cwd() let config = {} diff --git a/utils/randomMessage.js b/utils/randomMessage.js new file mode 100644 index 0000000..8704dc5 --- /dev/null +++ b/utils/randomMessage.js @@ -0,0 +1,37 @@ +import { Config } from './config.js' +import { ChatGPTAPI } from 'chatgpt' +import { getMessageById, upsertMessage } from './common.js' +import fetch from 'node-fetch' +let proxy +if (Config.proxy) { + try { + proxy = (await import('https-proxy-agent')).default + } catch (e) { + console.warn('未安装https-proxy-agent,请在插件目录下执行pnpm add https-proxy-agent') + } +} +const newFetch = (url, options = {}) => { + const defaultOptions = Config.proxy + ? { + agent: proxy(Config.proxy) + } + : {} + + const mergedOptions = { + ...defaultOptions, + ...options + } + + return fetch(url, mergedOptions) +} +const question = '写一段话让大家来找我聊天。类似于“有人找我聊天吗?"这种风格,轻松随意一点控制在20个字以内' + +export async function generateHello () { + let api = new ChatGPTAPI({ + apiBaseUrl: Config.openAiBaseUrl, + apiKey: Config.apiKey, + fetch: newFetch + }) + const res = await api.sendMessage(question) + return res.text +}