Skip to content

Commit

Permalink
fix: 热更新.env
Browse files Browse the repository at this point in the history
  • Loading branch information
sj817 committed Jan 16, 2025
1 parent 9a80029 commit 90c4eb8
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 90 deletions.
32 changes: 26 additions & 6 deletions packages/core/src/adapter/input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const botID = 'console'
export class AdapterConsole extends AdapterBase implements AdapterType {
constructor () {
super()
listeners.on('karin:adapter:open', () => process.stdin.on('data', data => this.createEvent(data)))
listeners.on('karin:adapter:open', () =>
process.stdin.on('data', data => this.createEvent(data)),
)
listeners.on('karin:adapter:close', () => process.stdin.removeAllListeners('data'))

this.adapter.name = '@karinjs/console'
Expand All @@ -44,10 +46,25 @@ export class AdapterConsole extends AdapterBase implements AdapterType {
return this.account.selfId
}

createEvent (data: Buffer) {
async createEvent (data: Buffer) {
const text = data.toString().trim()
const seq = Math.floor(Math.random() * 1000000000)
const time = Date.now()

/** 如果是日志等级更新命令 */
if (text.startsWith('log')) {
const level = text.replace(/^log/, '').trim()
if (level) {
const list = ['trace', 'debug', 'info', 'warn', 'error', 'fatal']
if (list.includes(level)) {
const { updateLevel } = await import('@/utils/config/config')
updateLevel(level)
logger.info(`日志等级已更新为: ${level}`)
return
}
}
}

/** 如果带`group`前缀 则视为群环境 */
if (text.startsWith('group')) {
const contact = contactGroup('10010')
Expand All @@ -61,7 +78,7 @@ export class AdapterConsole extends AdapterBase implements AdapterType {
rawEvent: { data },
sender: senderGroup(botID, 'member'),
time,
srcReply: (elements) => this.sendMsg(contact, elements),
srcReply: elements => this.sendMsg(contact, elements),
})
return
}
Expand All @@ -77,11 +94,15 @@ export class AdapterConsole extends AdapterBase implements AdapterType {
rawEvent: { data },
sender: senderFriend(botID, ''),
time,
srcReply: (elements) => this.sendMsg(contact, elements),
srcReply: elements => this.sendMsg(contact, elements),
})
}

async sendMsg (contact: Contact, elements: Array<Elements>, retryCount?: number): Promise<SendMsgResults> {
async sendMsg (
contact: Contact,
elements: Array<Elements>,
retryCount?: number,
): Promise<SendMsgResults> {
const time = Date.now()
const messageId = (++index).toString()
const result: SendMsgResults = {
Expand All @@ -105,7 +126,6 @@ export class AdapterConsole extends AdapterBase implements AdapterType {
continue
}

// if (['image', 'record', 'video'].includes(v.type)) {
if (v.type === 'image' || v.type === 'record' || v.type === 'video') {
const url = v.file.startsWith('http') ? v.file : await this.getUrl(v.file, '.png')
msg.push(`[${v.type}: ${logger.blue(url)} ]`)
Expand Down
116 changes: 79 additions & 37 deletions packages/core/src/event/handler/other/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ import { config as cfg, getFriendCfg, getGroupCfg } from '@/utils/config'

import type { Message, Event } from '@/types/event'
import type { AdapterProtocol } from '@/types/adapter'
import type {
GroupMessage,
GroupTempMessage,
GuildMessage,
} from '../../message'
import type { GroupMessage, GroupTempMessage, GuildMessage } from '../../message'

/**
* @description 日志
* @param userId 用户ID
* @param text 日志内容
*/
export const log = (
userId: string,
text: string
) => logger.debug(`[消息过滤][${userId}] ${text}`)
export const log = (userId: string, text: string) => logger.debug(`[消息过滤][${userId}] ${text}`)

/**
* @description 初始化`msg` `rawMessage`
Expand All @@ -38,15 +31,15 @@ export const initMsg = (ctx: Message) => {
* @param ctx 事件对象
* @param config 基本配置
*/
export const initRole = (
ctx: Event,
config: ReturnType<typeof cfg>
) => {
export const initRole = (ctx: Event, config: ReturnType<typeof cfg>) => {
/** 主人 */
if (config.master.includes(`${ctx.selfId}@${ctx.userId}`) || config.master.includes(ctx.userId)) {
ctx.isMaster = true
ctx.isAdmin = true
} else if (config.admin.includes(`${ctx.selfId}@${ctx.userId}`) || config.admin.includes(ctx.userId)) {
} else if (
config.admin.includes(`${ctx.selfId}@${ctx.userId}`) ||
config.admin.includes(ctx.userId)
) {
/** 管理员 */
ctx.isAdmin = true
ctx.isMaster = false
Expand Down Expand Up @@ -91,8 +84,8 @@ export const initEmit = (ctx: Event) => {
* @returns `true` 表示通过
*/
export const disableViaAdapter = (
plugin: typeof cache.command[number] | typeof cache.accept[number],
protocol: AdapterProtocol
plugin: (typeof cache.command)[number] | (typeof cache.accept)[number],
protocol: AdapterProtocol,
): boolean => {
/** 插件指定只允许特定适配器使用 */
if (plugin.adapter.length && !plugin.adapter.includes(protocol)) {
Expand All @@ -114,8 +107,8 @@ export const disableViaAdapter = (
* @returns `true` 表示通过
*/
export const disableViaPluginWhitelist = (
plugin: typeof cache.command[number] | typeof cache.accept[number],
config: ReturnType<typeof getFriendCfg> | ReturnType<typeof getGroupCfg>
plugin: (typeof cache.command)[number] | (typeof cache.accept)[number],
config: ReturnType<typeof getFriendCfg> | ReturnType<typeof getGroupCfg>,
) => {
/** 未设置白名单 */
if (!config.enable.length) return true
Expand Down Expand Up @@ -148,8 +141,8 @@ export const disableViaPluginWhitelist = (
* @returns `true` 表示通过
*/
export const disableViaPluginBlacklist = (
plugin: typeof cache.command[number] | typeof cache.accept[number],
config: ReturnType<typeof getFriendCfg> | ReturnType<typeof getGroupCfg>
plugin: (typeof cache.command)[number] | (typeof cache.accept)[number],
config: ReturnType<typeof getFriendCfg> | ReturnType<typeof getGroupCfg>,
) => {
/** 未设置黑名单 */
if (!config.disable.length) return true
Expand Down Expand Up @@ -184,8 +177,20 @@ export const privateFilterEvent = (
ctx: Event,
config: ReturnType<typeof cfg>,
friend: ReturnType<typeof getFriendCfg>,
cd: boolean
cd: boolean,
): boolean => {
if (ctx.isFriend) {
if (!config?.user?.enable) {
log(ctx.userId, `当前好友事件未启用: ${ctx.eventId}`)
return false
}
} else {
if (!config?.directs?.enable) {
log(ctx.userId, `当前频道私信事件未启用: ${ctx.eventId}`)
return false
}
}

if (!cd) {
log(ctx.userId, `当前处于CD中: ${ctx.eventId}`)
return false
Expand Down Expand Up @@ -256,8 +261,20 @@ export const groupFilterEvent = (
ctx: Event,
config: ReturnType<typeof cfg>,
group: ReturnType<typeof getGroupCfg>,
cd: boolean
cd: boolean,
): boolean => {
if (ctx.isGroup) {
if (!config?.group?.enable) {
log(ctx.groupId, `当前群事件未启用: ${ctx.eventId}`)
return false
}
} else if (ctx.isGuild) {
if (!config?.guilds?.enable) {
log(ctx.guildId, `当前频道事件未启用: ${ctx.eventId}`)
return false
}
}

if (!cd) {
log(ctx.userId, `当前处于CD中: ${ctx.eventId}`)
return false
Expand Down Expand Up @@ -301,25 +318,35 @@ export const groupFilterEvent = (
log(ctx.userId, `用户处于群成员黑名单: ${ctx.eventId}`)
return false
}
} if (ctx.isGuild) {
}
if (ctx.isGuild) {
/** 频道白名单 */
if (config?.guilds?.enable_list?.length && !config?.guilds?.enable_list.includes(ctx.guildId)) {
log(ctx.guildId, `频道未处于白名单: ${ctx.eventId}`)
return false
}

/** 频道黑名单 */
if (config?.guilds?.disable_list?.length && config?.guilds?.disable_list.includes(ctx.guildId)) {
if (
config?.guilds?.disable_list?.length &&
config?.guilds?.disable_list.includes(ctx.guildId)
) {
log(ctx.guildId, `频道处于黑名单: ${ctx.eventId}`)
return false
}

if (config?.channels?.enable_list?.length && !config?.channels?.enable_list.includes(ctx.channelId)) {
if (
config?.channels?.enable_list?.length &&
!config?.channels?.enable_list.includes(ctx.channelId)
) {
log(ctx.channelId, `子频道未处于白名单: ${ctx.eventId}`)
return false
}

if (config?.channels?.disable_list?.length && config?.channels?.disable_list.includes(ctx.channelId)) {
if (
config?.channels?.disable_list?.length &&
config?.channels?.disable_list.includes(ctx.channelId)
) {
log(ctx.channelId, `子频道处于黑名单: ${ctx.eventId}`)
return false
}
Expand Down Expand Up @@ -399,14 +426,20 @@ export const groupFilterEvent = (
*/
export const groupPrint = (
ctx: GroupMessage | GroupTempMessage,
config: ReturnType<typeof cfg>
config: ReturnType<typeof cfg>,
): boolean => {
if (config?.group?.log_enable_list?.length && !config?.group?.log_enable_list.includes(ctx.groupId)) {
if (
config?.group?.log_enable_list?.length &&
!config?.group?.log_enable_list.includes(ctx.groupId)
) {
log(ctx.groupId, `群未处于白名单: ${ctx.eventId}`)
return false
}

if (config?.group?.log_disable_list?.length && config?.group?.log_disable_list.includes(ctx.groupId)) {
if (
config?.group?.log_disable_list?.length &&
config?.group?.log_disable_list.includes(ctx.groupId)
) {
log(ctx.groupId, `群处于黑名单: ${ctx.eventId}`)
return false
}
Expand All @@ -420,26 +453,35 @@ export const groupPrint = (
* @param config 基本配置对象
* @returns `true` 表示通过
*/
export const guildPrint = (
ctx: GuildMessage,
config: ReturnType<typeof cfg>
): boolean => {
if (config?.guilds?.log_enable_list?.length && !config?.guilds?.log_enable_list.includes(ctx.guildId)) {
export const guildPrint = (ctx: GuildMessage, config: ReturnType<typeof cfg>): boolean => {
if (
config?.guilds?.log_enable_list?.length &&
!config?.guilds?.log_enable_list.includes(ctx.guildId)
) {
log(ctx.guildId, `频道日志未处于白名单: ${ctx.eventId}`)
return false
}

if (config?.guilds?.log_disable_list?.length && config?.guilds?.log_disable_list.includes(ctx.guildId)) {
if (
config?.guilds?.log_disable_list?.length &&
config?.guilds?.log_disable_list.includes(ctx.guildId)
) {
log(ctx.guildId, `频道日志处于黑名单: ${ctx.eventId}`)
return false
}

if (config?.channels?.log_enable_list?.length && !config?.channels?.log_enable_list.includes(ctx.channelId)) {
if (
config?.channels?.log_enable_list?.length &&
!config?.channels?.log_enable_list.includes(ctx.channelId)
) {
log(ctx.channelId, `子频道日志未处于白名单: ${ctx.eventId}`)
return false
}

if (config?.channels?.log_disable_list?.length && config?.channels?.log_disable_list.includes(ctx.channelId)) {
if (
config?.channels?.log_disable_list?.length &&
config?.channels?.log_disable_list.includes(ctx.channelId)
) {
log(ctx.channelId, `子频道日志处于黑名单: ${ctx.eventId}`)
return false
}
Expand Down
Loading

0 comments on commit 90c4eb8

Please sign in to comment.