Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
K
Koishi Target Def
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
3rdeye
Koishi Target Def
Commits
7fafdfc1
Commit
7fafdfc1
authored
Jul 03, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix private detect
parent
764b8fff
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
112 additions
and
18 deletions
+112
-18
index.ts
index.ts
+31
-16
tests/sample.spec.ts
tests/sample.spec.ts
+81
-2
No files found.
index.ts
View file @
7fafdfc1
...
...
@@ -2,12 +2,17 @@ import { Adapter, Bot, Context, Filter } from 'koishi';
import
_
from
'
lodash
'
;
import
{
RegisterSchema
,
SchemaProperty
}
from
'
schemastery-gen
'
;
export
interface
ChannelTargetLike
{
channelId
:
string
;
guildId
?:
string
;
}
@
RegisterSchema
()
export
class
ChannelTarget
{
export
class
ChannelTarget
implements
ChannelTargetLike
{
@
SchemaProperty
({
description
:
'
Channel ID.
'
,
required
:
true
})
channelId
:
string
;
@
SchemaProperty
({
description
:
'
Guild ID.
'
})
guildId
:
string
;
guildId
?
:
string
;
toDesc
():
string
|
[
string
,
string
]
{
return
this
.
guildId
?
[
this
.
channelId
,
this
.
guildId
]
:
this
.
channelId
;
...
...
@@ -18,7 +23,7 @@ export class ChannelTarget {
if
(
this
.
channelId
!==
s
.
channelId
)
{
return
false
;
}
if
(
this
.
guildId
&&
this
.
guildId
!==
s
.
guildId
)
{
if
(
this
.
guildId
&&
this
.
guildId
!==
s
.
guildId
)
{
return
false
;
}
return
true
;
...
...
@@ -26,27 +31,35 @@ export class ChannelTarget {
}
}
export
interface
SendTargetLike
{
bot
?:
string
;
users
?:
string
[];
channels
?:
ChannelTargetLike
[];
}
@
RegisterSchema
()
export
class
SendTarget
{
constructor
(
_
:
SendTargetLike
)
{}
@
SchemaProperty
({
description
:
'
Bot identifier. eg. onebot:123456789. Will use first bot if not specified. s
'
,
})
bot
:
string
;
bot
?
:
string
;
@
SchemaProperty
({
type
:
String
,
description
:
'
Private chat send targets.
'
,
default
:
[],
})
users
:
string
[];
users
?
:
string
[];
@
SchemaProperty
({
type
:
ChannelTarget
,
description
:
'
Channel send targets.
'
,
default
:
[],
})
channels
:
ChannelTarget
[];
channels
?
:
ChannelTarget
[];
getBot
(
bots
:
Adapter
.
BotList
)
{
if
(
this
.
bot
)
{
...
...
@@ -63,23 +76,25 @@ export class SendTarget {
);
}
getContext
(
ctx
:
Context
)
{
if
(
this
.
bot
)
{
const
[
platform
,
id
]
=
this
.
bot
.
split
(
'
:
'
);
ctx
=
ctx
.
platform
(
platform
).
self
(
id
);
}
getFilter
():
Filter
{
let
filters
:
Filter
[]
=
[];
if
(
this
.
users
?.
length
)
{
const
userSet
=
new
Set
(
this
.
users
);
filters
.
push
((
s
)
=>
s
.
subtype
===
'
private
'
&&
userSet
.
has
(
s
.
userId
));
filters
.
push
((
s
)
=>
!
s
.
guildId
&&
userSet
.
has
(
s
.
userId
));
}
if
(
this
.
channels
?.
length
)
{
filters
=
filters
.
concat
(
this
.
channels
.
map
((
c
)
=>
c
.
toFilter
()));
}
if
(
filters
.
length
)
{
ctx
=
ctx
.
intersect
((
s
)
=>
filters
.
some
((
f
)
=>
f
(
s
)));
}
return
ctx
;
return
(
s
)
=>
{
if
(
this
.
bot
&&
`
${
s
.
platform
}
:
${
s
.
selfId
}
`
!==
this
.
bot
)
{
return
false
;
}
return
filters
.
some
((
f
)
=>
f
(
s
));
};
}
getContext
(
ctx
:
Context
)
{
return
ctx
.
intersect
(
this
.
getFilter
());
}
async
send
(
bots
:
Adapter
.
BotList
,
content
:
string
)
{
...
...
tests/sample.spec.ts
View file @
7fafdfc1
import
{
Session
}
from
'
koishi
'
;
import
{
SendTarget
}
from
'
..
'
;
describe
(
'
Target def.
'
,
()
=>
{
it
(
'
register correct context
'
,
()
=>
{
expect
(
true
).
toBe
(
true
);
const
def
=
new
SendTarget
({
bot
:
'
onebot:123456789
'
,
users
:
[
'
1111111111
'
],
channels
:
[{
channelId
:
'
1111111111
'
}],
});
const
filter
=
def
.
getFilter
();
it
(
'
should detect platform
'
,
()
=>
{
expect
(
filter
({
platform
:
'
onebot
'
,
selfId
:
'
123456789
'
,
userId
:
'
1111111111
'
,
}
as
Session
),
).
toBe
(
true
);
expect
(
filter
({
platform
:
'
onebot1
'
,
selfId
:
'
123456789
'
,
userId
:
'
1111111111
'
,
}
as
Session
),
).
toBe
(
false
);
expect
(
filter
({
platform
:
'
onebot
'
,
selfId
:
'
123456782
'
,
userId
:
'
1111111111
'
,
}
as
Session
),
).
toBe
(
false
);
});
it
(
'
should detect private
'
,
()
=>
{
expect
(
filter
({
platform
:
'
onebot
'
,
selfId
:
'
123456789
'
,
userId
:
'
1111111111
'
,
}
as
Session
),
).
toBe
(
true
);
expect
(
filter
({
platform
:
'
onebot
'
,
selfId
:
'
123456789
'
,
userId
:
'
1111111112
'
,
}
as
Session
),
).
toBe
(
false
);
expect
(
filter
({
platform
:
'
onebot
'
,
selfId
:
'
123456789
'
,
userId
:
'
1111111111
'
,
guildId
:
'
111
'
,
}
as
Session
),
).
toBe
(
false
);
});
it
(
'
should detect channel
'
,
()
=>
{
expect
(
filter
({
platform
:
'
onebot
'
,
selfId
:
'
123456789
'
,
channelId
:
'
1111111111
'
,
}
as
Session
),
).
toBe
(
true
);
expect
(
filter
({
platform
:
'
onebot
'
,
selfId
:
'
123456789
'
,
channelId
:
'
1111111112
'
,
}
as
Session
),
).
toBe
(
false
);
expect
(
filter
({
platform
:
'
onebot
'
,
selfId
:
'
123456789
'
,
channelId
:
'
1111111111
'
,
guildId
:
'
1111111112
'
,
}
as
Session
),
).
toBe
(
true
);
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment