Skip to content

Commit

Permalink
Merge pull request #3 from Amarillys/main
Browse files Browse the repository at this point in the history
新增强娶和过滤不活跃用户功能
  • Loading branch information
idranme authored Apr 2, 2024
2 parents 92818c2 + e61209d commit d7d50bc
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-waifu",
"description": "Marry with your groupmate",
"version": "1.3.2",
"version": "1.4.0",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
[![npm](https://img.shields.io/npm/v/koishi-plugin-waifu?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-waifu)

娶群友!来寻找你今日的群友老婆吧!
现在已支持强娶和过滤不活跃用户!~
74 changes: 73 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ 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
}
}

export interface Config {
avoidNtr: boolean
onlyActiveUser: boolean
activeDays: number
forceMarry: boolean
excludeUsers: {
uid: string
note?: string
Expand All @@ -22,6 +26,9 @@ 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),
forceMarry: Schema.boolean().default(false),
excludeUsers: Schema.array(Schema.object({
uid: Schema.string().required(),
note: Schema.string()
Expand All @@ -40,10 +47,12 @@ 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)
})

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', '娶群友')
Expand Down Expand Up @@ -81,11 +90,21 @@ export function apply(ctx: Context, cfg: Config) {
const excludes = cfg.excludeUsers.map(({ uid }) => uid)
excludes.push(session.uid, session.sid)

const list = memberList.filter(v => !excludes.includes(`${session.platform}:${v.user.id}`) && !v.user.isBot)
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}`)) {
activeList.push(value)
}
list = list.filter(v => activeList.find(active => active.user.id === v.user.id))
}

if (list.length === 0) return session.text('.members-too-few')

let selected = randomSelect(list)
let selectedFid = `${session.platform}:${session.guildId}:${selected.user.id}`
const selectedMarriage = await ctx.cache.get('waifu_marriages', selectedFid)
Expand Down Expand Up @@ -117,4 +136,57 @@ export function apply(ctx: Context, cfg: Config) {
avatar: h.image(selected.avatar || selected.user.avatar)
})
})

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)
})
}

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.entries(`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)
})
})
}
}
14 changes: 13 additions & 1 deletion src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
avoidNtr: 是否避免用户抽中他人的老婆
onlyActiveUser: 是否只娶活跃天数内发言过的群友
activeDays: 活跃天数
forceMarry: 启用强娶
excludeUsers:
$description: 排除的用户
$value:
Expand All @@ -10,4 +13,13 @@ commands:
description: 娶群友
messages:
marriages: '{quote}你今日的群友老婆是:{name} {avatar}'
members-too-few: 潜在的老婆太少啦,快去拐骗(?)点回来吧!
members-too-few: 潜在的老婆太少啦,快去拐骗(?)点回来吧!或者骗出来聊天!
divorce:
description: 闹离婚
messages:
divorce: '{quote}别闹啦,好啦,你已经单身啦!'
force_marry:
description: 强娶群友
messages:
force_marry: '{quote}好啦好啦,群友{name}已经被你强娶回家了 {avatar}'
no_target: '{quote}强娶谁呀~'

0 comments on commit d7d50bc

Please sign in to comment.