Skip to content

Commit

Permalink
1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
idanran committed Apr 5, 2024
1 parent d7d50bc commit 507985f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 72 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"url": "https://github.com/idanran"
},
"peerDependencies": {
"koishi": "^4.15.2"
"koishi": "^4.16.0"
},
"devDependencies": {
"@koishijs/cache": "^2.1.0"
Expand Down
7 changes: 6 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@
[![npm](https://img.shields.io/npm/v/koishi-plugin-waifu?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-waifu)

娶群友!来寻找你今日的群友老婆吧!
现在已支持强娶和过滤不活跃用户!~

## 可选功能

* 避免用户抽中他人的老婆
* 只让用户抽中活跃的群友
* 强娶
113 changes: 55 additions & 58 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare module '@koishijs/cache' {
interface Tables {
waifu_marriages: Universal.GuildMember & { marriageDate: number }
[key: `waifu_members_${string}`]: Universal.GuildMember
[key: `waifu_members_active_${string}`]: Universal.GuildMember
[key: `waifu_members_active_${string}`]: string
}
}

Expand All @@ -27,12 +27,12 @@ export interface Config {
export const Config: Schema<Config> = Schema.object({
avoidNtr: Schema.boolean().default(false),
onlyActiveUser: Schema.boolean().default(false),
activeDays: Schema.number().default(7),
activeDays: Schema.natural().default(7),
forceMarry: Schema.boolean().default(false),
excludeUsers: Schema.array(Schema.object({
uid: Schema.string().required(),
note: Schema.string()
})).default([{ uid: 'red:2854196310', note: 'Q群管家' }])
})).role('table').default([{ uid: 'red:2854196310', note: 'Q群管家' }])
}).i18n({
'zh-CN': require('./locales/zh-CN'),
})
Expand All @@ -47,15 +47,15 @@ export function apply(ctx: Context, cfg: Config) {
ctx.guild().on('message-created', async (session) => {
const member: Universal.GuildMember = session.event.member || { user: session.event.user }
await ctx.cache.set(`waifu_members_${session.gid}`, session.userId, member, 2 * Time.day)
await ctx.cache.set(`waifu_members_active_${session.gid}`, session.userId, member, cfg.activeDays * Time.day)
await ctx.cache.set(`waifu_members_active_${session.gid}`, session.userId, '', cfg.activeDays * Time.day)
})

ctx.on('guild-member-removed', (session) => {
ctx.cache.delete(`waifu_members_${session.gid}`, session.userId)
ctx.cache.delete(`waifu_members_active_${session.gid}`, session.userId)
})

ctx.command('waifu', '娶群友')
ctx.command('waifu')
.alias('marry', '娶群友', '今日老婆')
.action(async ({ session }) => {
if (!session.guildId) {
Expand All @@ -82,7 +82,7 @@ export function apply(ctx: Context, cfg: Config) {
}
} catch { }
if (!memberList?.length) {
for await (const [, value] of ctx.cache.entries(`waifu_members_${session.gid}`)) {
for await (const value of ctx.cache.values(`waifu_members_${session.gid}`)) {
memberList.push(value)
}
}
Expand All @@ -91,16 +91,13 @@ export function apply(ctx: Context, cfg: Config) {
excludes.push(session.uid, session.sid)

let list = memberList.filter(v => !excludes.includes(`${session.platform}:${v.user.id}`) && !v.user.isBot)
if (list.length === 0) {
return session.text('.members-too-few')
}

if (cfg.onlyActiveUser) {
let activeList = []
for await (const [, value] of ctx.cache.entries(`waifu_members_active_${session.gid}`)) {
let activeList: string[] = []
for await (const value of ctx.cache.keys(`waifu_members_active_${session.gid}`)) {
activeList.push(value)
}
list = list.filter(v => activeList.find(active => active.user.id === v.user.id))
list = list.filter(v => activeList.find(active => active === v.user.id))
}

if (list.length === 0) return session.text('.members-too-few')
Expand Down Expand Up @@ -137,56 +134,56 @@ export function apply(ctx: Context, cfg: Config) {
})
})

ctx.command('divorce', '闹离婚')
.alias('分手', '闹离婚', '离婚')
.action(async ({ session }) => {
const marriage = await ctx.cache.get('waifu_marriages', session.fid)
if (marriage && isSameDay(Date.now(), marriage.marriageDate)) {
await ctx.cache.delete('waifu_marriages', session.fid)
let selectedFid = `${session.platform}:${session.guildId}:${marriage.user?.id}`
await ctx.cache.delete('waifu_marriages', selectedFid)
return session.text('.divorce', {
quote: h.quote(session.messageId)
})
}
})

if (cfg.forceMarry) {
ctx.command('force_marry', '强娶')
.alias('force_marry', '强娶')
.action(async ({ session }) => {
const message = session.event?.message
const target = message?.content.match(/id="(\d+?)"/)
if (target == null) {
return session.text('.no_target', {
quote: h.quote(session.messageId)
})
}
ctx.command('force-marry <target:user>')
.alias('强娶')
.action(async ({ session }, target) => {
if (!session.guildId) {
return session.text('.members-too-few')
}
if (!target) {
return session.text('.no-target', {
quote: h.quote(session.messageId)
})
}

let memberList: Universal.GuildMember[]
try {
const { data, next } = await session.bot.getGuildMemberList(session.guildId)
memberList = data
if (next) {
const { data } = await session.bot.getGuildMemberList(session.guildId, next)
memberList.push(...data)
const targetId = target.replace(session.platform + ':', '')
if (targetId === session.userId) return session.text('.target-self')

const marriage = await ctx.cache.get('waifu_marriages', session.fid)
if (marriage && isSameDay(Date.now(), marriage.marriageDate)) {
return session.text('.already-marriage', {
quote: h.quote(session.messageId)
})
}
} catch { }
if (!memberList?.length) {
for await (const [, value] of ctx.cache.entries(`waifu_members_${session.gid}`)) {
memberList.push(value)

let memberList: Universal.GuildMember[]
try {
const { data, next } = await session.bot.getGuildMemberList(session.guildId)
memberList = data
if (next) {
const { data } = await session.bot.getGuildMemberList(session.guildId, next)
memberList.push(...data)
}
} catch { }
if (!memberList?.length) {
for await (const value of ctx.cache.values(`waifu_members_${session.gid}`)) {
memberList.push(value)
}
}
}

const targetId = target[1]
const marriageDate = Date.now()
let selected = memberList.find(u => u.user.id == targetId)
await ctx.cache.set('waifu_marriages', session.fid, { ...selected, marriageDate }, Time.day)
return session.text('.force_marry', {
quote: h.quote(session.messageId),
name: selected.nick || selected.user.nick || selected.user.name,
avatar: h.image(selected.avatar || selected.user.avatar)

let selected = memberList.find(u => u.user.id == targetId)
if (!selected) return session.text('.members-too-few')

const selectedFid = `${session.platform}:${session.guildId}:${selected.user.id}`
const marriageDate = Date.now()
await ctx.cache.set('waifu_marriages', session.fid, { ...selected, marriageDate }, Time.day)
await ctx.cache.set('waifu_marriages', selectedFid, { ...session.event.member, marriageDate }, Time.day)
return session.text('.force-marry', {
quote: h.quote(session.messageId),
name: selected.nick || selected.user.nick || selected.user.name,
avatar: h.image(selected.avatar || selected.user.avatar)
})
})
})
}
}
23 changes: 11 additions & 12 deletions src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
avoidNtr: 是否避免用户抽中他人的老婆
onlyActiveUser: 是否只娶活跃天数内发言过的群友
activeDays: 活跃天数
forceMarry: 启用强娶
onlyActiveUser: 是否只让用户抽中活跃的群友
activeDays: 活跃天数最小值 (onlyActiveUser 判断活跃的标准)
forceMarry: 是否启用强娶指令
excludeUsers:
$description: 排除的用户
$value:
uid: 群组平台与用户 ID, 格式:`platform:userId`
note: 备注 (可不填此项)
uid: '平台名与用户 ID, 格式: platform:userId'
note: 备注 (可不填)

commands:
waifu:
description: 娶群友
messages:
marriages: '{quote}你今日的群友老婆是:{name} {avatar}'
members-too-few: 潜在的老婆太少啦,快去拐骗(?)点回来吧!或者骗出来聊天!
divorce:
description: 闹离婚
messages:
divorce: '{quote}别闹啦,好啦,你已经单身啦!'
force_marry:
force-marry:
description: 强娶群友
messages:
force_marry: '{quote}好啦好啦,群友{name}已经被你强娶回家了 {avatar}'
no_target: '{quote}强娶谁呀~'
force-marry: '{quote}好啦好啦,群友「{name}」已经被你强娶回家了 {avatar}'
no-target: '{quote}强娶谁呀~'
already-marriage: '{quote}你已经娶了老婆,今天别再娶啦!'
members-too-few: 能娶的群友没有这位,或许可以先把对方骗出来聊天?!
target-self: '{quote}娶自己干嘛!'

0 comments on commit 507985f

Please sign in to comment.