Skip to content

Commit

Permalink
feat: 适配karin
Browse files Browse the repository at this point in the history
  • Loading branch information
XasYer committed Nov 20, 2024
1 parent 20e7795 commit a80de7e
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 27 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,32 @@

## 介绍

这是一个基于 [Miao-Yunzai](https://gitee.com/yoimiya-kokomi/Miao-Yunzai)&[Trss-Yunzai](https://gitee.com/TimeRainStarSky/Yunzai)的扩展插件, 提供 steam 群友状态播报, steam 库存, steam 愿望单 等功能
这是一个基于 [Miao-Yunzai](https://github.com/yoimiya-kokomi/Miao-Yunzai)&[Trss-Yunzai](https://github.com/TimeRainStarSky/Yunzai)&[Karin](https://github.com/KarinJS/Karin)的扩展插件, 提供 steam 群友状态播报, steam 库存, steam 愿望单 等功能

## 安装

### 使用github
### Yunzai使用

#### 使用github
```bash
git clone --depth=1 https://github.com/XasYer/steam-plugin.git ./plugins/steam-plugin
```

#### 使用gitee
```bash
git clone --depth=1 https://gitee.com/xiaoye12123/steam-plugin.git ./plugins/steam-plugin
```

### Karin使用

#### 使用github
```bash
git clone --depth=1 https://github.moeyy.xyz/https://github.com/XasYer/steam-plugin.git ./plugins/steam-plugin
git clone --depth=1 https://github.com/XasYer/steam-plugin.git ./plugins/karin-plugin-steam
```

### 使用gitee
#### 使用gitee
```bash
git clone --depth=1 https://gitee.com/xiaoye12123/steam-plugin.git ./plugins/steam-plugin
git clone --depth=1 https://gitee.com/xiaoye12123/steam-plugin.git ./plugins/karin-plugin-steam
```

### 安装依赖
Expand Down
8 changes: 8 additions & 0 deletions apps/bind.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { utils, db } from '#models'
import { App } from '#components'

const app = {
id: 'bind',
name: '绑定Steam'
}

export const rule = {
bind: {
Expand Down Expand Up @@ -67,3 +73,5 @@ export const rule = {
}
}
}

export const bind = new App(app, rule).create()
12 changes: 10 additions & 2 deletions apps/info.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { db, utils, api } from '#models'
import { segment } from '#lib'
import moment from 'moment'
import { segment } from '#lib'
import { App } from '#components'
import { db, utils, api } from '#models'

const app = {
id: 'info',
name: 'Steam信息'
}

export const rule = {
info: {
Expand Down Expand Up @@ -43,6 +49,8 @@ export const rule = {
}
}

export const info = new App(app, rule).create()

/**
* 将用户状态码转换为中文
* @param {number} state
Expand Down
11 changes: 9 additions & 2 deletions apps/inventory.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { utils, db, api } from '#models'
import { segment } from '#lib'
import { Render } from '#components'
import { Render, App } from '#components'
import moment from 'moment'
import _ from 'lodash'

const app = {
id: 'inventory',
name: '库存'
}

export const rule = {
inventory: {
reg: /^#?steam(?:库存|游戏列表|(?:最近|近期)(?:游?玩|运行|启动)|愿望单)\s*(\d+)?$/i,
Expand Down Expand Up @@ -105,7 +110,9 @@ export const rule = {
* 将游戏时长(单位:分)转换小时
* @param {number} time
* @returns {string}
*/
*/
function getTime (time) {
return (time / 60).toFixed(1) + 'h'
}

export const inventory = new App(app, rule).create()
3 changes: 1 addition & 2 deletions components/Version.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'fs'
import { fileURLToPath } from 'url'
import { exec } from 'child_process'
import { join, dirname, basename } from 'path'

const __filename = fileURLToPath(import.meta.url)
Expand All @@ -24,7 +23,7 @@ const BotName = (() => {
if (/^karin/i.test(pluginName)) {
return 'Karin'
} else if (BotPackage.dependencies.react) {
exec(`rm -rf plugins/${pluginName}`)
fs.rmdirSync(pluginPath, { recursive: true })
return 'Yunzai-Next'
} else if (Array.isArray(global.Bot?.uin)) {
return 'Trss-Yunzai'
Expand Down
2 changes: 1 addition & 1 deletion lib/Bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Version } from '#components'
const Bot = await (async () => {
switch (Version.BotName) {
case 'Karin':
return (await import('node-karin')).Bot
return (await import('node-karin')).default
default:
return global.Bot
}
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Bot from './Bot.js'
import redis from './redis.js'
import logger from './logger.js'
import plugin from './plugin.js'
import segment from './segment.js'
import puppeteer from './puppeteer.js'

export {
Bot,
redis,
logger,
plugin,
segment,
Expand Down
2 changes: 1 addition & 1 deletion lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Version } from '#components'
const plugin = await (async () => {
switch (Version.BotName) {
case 'Karin':
return (await import('node-karin')).plugin
return (await import('node-karin')).Plugin
default:
return global.plugin
}
Expand Down
2 changes: 1 addition & 1 deletion lib/puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const puppeteer = await (async () => {
options.file = options.tplFile
options.type = options.imgType || 'jpeg'
options.fileID = options.saveId
options.screensEval = '#containter'
options.screensEval = '#container'
const img = await Renderer.render(options)
return segment.image(img)
}
Expand Down
14 changes: 14 additions & 0 deletions lib/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Version } from '#components'

const redis = await (async () => {
switch (Version.BotName) {
case 'Karin':
return (await import('node-karin')).redis
default:
return global.redis
}
})()

redis.get('test11').then(console.log)

export default redis
10 changes: 7 additions & 3 deletions models/task/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Config } from '#components'
import { Config, Version } from '#components'
import { Bot, logger } from '#lib'
import { api, db, utils } from '#models'
import _ from 'lodash'
Expand Down Expand Up @@ -37,7 +37,11 @@ export function startTimer () {
const iconUrl = utils.getHeaderImgUrlByAppid(player.gameid)
const iconBuffer = await utils.getImgUrlBuffer(iconUrl)
for (const i of pushGroups) {
if (!Bot[i.botId]) {
if (Version.BotName === 'Karin') {
if (!Bot.getBot(i.botId)) {
continue
}
} else if (!Bot[i.botId]) {
continue
}
const nickname = await utils.getUserName(i.botId, i.userId, i.groupId)
Expand Down Expand Up @@ -66,7 +70,7 @@ export function startTimer () {
continue
}
try {
await Bot[i.botId].pickGroup(i.groupId).sendMsg(msg)
await utils.sendGroupMsg(i.botId, i.groupId, msg)
} catch (error) {
logger.error(`群消息发送失败: ${i.groupId}`, error)
}
Expand Down
51 changes: 41 additions & 10 deletions models/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Version } from '#components'
import * as request from './request.js'
import moment from 'moment'
import { Bot } from '#lib'

export { request }

Expand Down Expand Up @@ -66,16 +67,26 @@ export function formatDuration (inp, unit = 'seconds') {
*/
export async function getUserName (botId, uid, gid) {
try {
if (gid) {
gid = Number(gid) || gid
const group = Bot[botId].pickGroup(gid)
const member = (await group.pickMember(uid)).getInfo()
return member.card || member.nickname || member.user_id || uid
if (Version.BotName === 'Karin') {
if (gid) {
const bot = Bot.getBot(botId)
const info = await bot.GetGroupMemberInfo(gid, uid)
return info.card || info.nick || info.uid || uid
} else {
return uid
}
} else {
uid = Number(uid) || uid
const user = Bot[botId].pickUser(uid)
const info = await user.getInfo()
return info.nickname || info.user_id || uid
if (gid) {
gid = Number(gid) || gid
const group = Bot[botId].pickGroup(gid)
const member = (await group.pickMember(uid)).getInfo()
return member.card || member.nickname || member.user_id || uid
} else {
uid = Number(uid) || uid
const user = Bot[botId].pickUser(uid)
const info = await user.getInfo()
return info.nickname || info.user_id || uid
}
}
} catch {
return uid
Expand All @@ -100,6 +111,21 @@ export function getAtUid (at, id) {
}
}

/**
* 主动发送群消息
* @param {string} botId
* @param {string} gid
* @param {any} msg
* @returns {Promise<any>}
*/
export async function sendGroupMsg (botId, gid, msg) {
if (Version.BotName === 'Karin') {
return await Bot.sendMsg(botId, { scene: 'group', peer: gid }, msg)
} else {
return await Bot[botId].pickGroup(gid).sendMsg(msg)
}
}

/**
* 获取appid对应的header图片url
* @param {string} appid
Expand All @@ -117,7 +143,12 @@ export function getHeaderImgUrlByAppid (appid) {
export async function getImgUrlBuffer (url) {
for (let i = 0; i < 3; i++) {
try {
return await request.get(url, { responseType: 'arraybuffer', baseUrl: '' }).then(res => res.data)
const buffer = await request.get(url, { responseType: 'arraybuffer', baseUrl: '' })
if (Version.BotName === 'Karin') {
return `base64://${buffer.toString('base64')}`
} else {
return buffer
}
} catch { }
}
return null
Expand Down

0 comments on commit a80de7e

Please sign in to comment.