Skip to content

Commit

Permalink
feat: ✨ Add self control
Browse files Browse the repository at this point in the history
  • Loading branch information
b2r66sun committed Jan 2, 2025
1 parent 01b3c10 commit ecad959
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions src/wechaty/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const aliasWhiteList = env.ALIAS_WHITELIST ? env.ALIAS_WHITELIST.split(',') : []
// 从环境变量中导入群聊白名单
const roomWhiteList = env.ROOM_WHITELIST ? env.ROOM_WHITELIST.split(',') : []

// Start and Shutdown prompt
const startPrompt = env.START_PROMPT? env.START_PROMPT: "/start"
const endPrompt = env.END_PROMPT? env.END_PROMPT: "/end"

let enabled = true

import { getServe } from './serve.js'

/**
Expand All @@ -40,25 +46,41 @@ export async function defaultMessage(msg, bot, ServiceType = 'GPT') {
const isBotSelf = botName === `@${remarkName}` || botName === `@${name}` // 是否是机器人自己
// TODO 你们可以根据自己的需求修改这里的逻辑
if (isBotSelf || !isText) return // 如果是机器人自己发送的消息或者消息类型不是文本则不处理
try {
// 区分群聊和私聊
// 群聊消息去掉艾特主体后,匹配自动回复前缀
if (isRoom && room && content.replace(`${botName}`, '').trimStart().startsWith(`${autoReplyPrefix}`)) {
const question = (await msg.mentionText()) || content.replace(`${botName}`, '').replace(`${autoReplyPrefix}`, '') // 去掉艾特的消息主体
console.log('🌸🌸🌸 / question: ', question)
const response = await getReply(question)
await room.say(response)

if (isBotSelf) {
console.log('👑👑👑 / self content: ', content)
if (content == startPrompt) {
enabled = true
await contact.say("🔥 Bot Enabled.")
}
if (content == endPrompt) {
enabled = false
await contact.say("🔵 Bot Disabled.")
}
// 私人聊天,白名单内的直接发送
// 私人聊天直接匹配自动回复前缀
if (isAlias && !room && content.trimStart().startsWith(`${autoReplyPrefix}`)) {
const question = content.replace(`${autoReplyPrefix}`, '')
console.log('🌸🌸🌸 / content: ', question)
const response = await getReply(question)
await contact.say(response)
return
}

if (enabled){
try {
// 区分群聊和私聊
// 群聊消息去掉艾特主体后,匹配自动回复前缀
if (isRoom && room && content.replace(`${botName}`, '').trimStart().startsWith(`${autoReplyPrefix}`)) {
const question = (await msg.mentionText()) || content.replace(`${botName}`, '').replace(`${autoReplyPrefix}`, '') // 去掉艾特的消息主体
console.log('🌸🌸🌸 / question: ', question)
const response = await getReply(question)
await room.say(response)
}
// 私人聊天,白名单内的直接发送
// 私人聊天直接匹配自动回复前缀
if (isAlias && !room && content.trimStart().startsWith(`${autoReplyPrefix}`)) {
const question = content.replace(`${autoReplyPrefix}`, '')
console.log('🌸🌸🌸 / content: ', question)
const response = await getReply(question)
await contact.say(response)
}
} catch (e) {
console.error(e)
}
} catch (e) {
console.error(e)
}
}

Expand Down

0 comments on commit ecad959

Please sign in to comment.