Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
K
Koishi Plugin Thesaurus
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 Plugin Thesaurus
Commits
b36db486
Commit
b36db486
authored
Jul 25, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bump to Koishi 4.8
parent
67d71753
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
1582 additions
and
595 deletions
+1582
-595
dev/index.ts
dev/index.ts
+5
-5
package-lock.json
package-lock.json
+1498
-485
package.json
package.json
+8
-7
src/index.ts
src/index.ts
+60
-95
tests/sample.spec.ts
tests/sample.spec.ts
+3
-3
webpack.config.js
webpack.config.js
+8
-0
No files found.
dev/index.ts
View file @
b36db486
import
{
App
}
from
'
koishi
'
;
import
{
Context
}
from
'
koishi
'
;
import
TargetPlugin
from
'
../src
'
;
import
ConsolePlugin
from
'
@koishijs/plugin-console
'
;
import
SandboxPlugin
from
'
@koishijs/plugin-sandbox
'
;
import
*
as
DatabasePlugin
from
'
@koishijs/plugin-database-memory
'
;
import
CachePlugin
from
'
@koishijs/plugin-cache-lru
'
;
import
DatabasePlugin
from
'
@koishijs/plugin-database-memory
'
;
import
AragamiPlugin
from
'
koishi-plugin-cache-aragami
'
;
import
ExtrasInDev
from
'
./extras
'
;
const
app
=
new
App
({
const
app
=
new
Context
({
port
:
14514
,
host
:
'
localhost
'
,
prefix
:
'
.
'
,
...
...
@@ -19,7 +19,7 @@ app.plugin(ConsolePlugin, {
});
// Some services
app
.
plugin
(
Cache
Plugin
);
app
.
plugin
(
Aragami
Plugin
);
app
.
plugin
(
DatabasePlugin
);
// Some extras
...
...
package-lock.json
View file @
b36db486
This source diff could not be displayed because it is too large. You can
view the blob
instead.
package.json
View file @
b36db486
...
...
@@ -21,26 +21,26 @@
"qqbot"
,
"cqhttp"
,
"onebot"
,
"required:cache"
"required:cache
-aragami
"
],
"bugs"
:
{
"url"
:
"
https://code.mycard.moe/3rdeye/koishi-plugin-thesaurus/issues
"
},
"homepage"
:
"
https://code.mycard.moe/3rdeye/koishi-plugin-thesaurus
"
,
"dependencies"
:
{
"
koishi-thirdeye
"
:
"
^1
0.3.2
"
,
"
koishi-thirdeye
"
:
"
^1
1.0.6
"
,
"
lodash
"
:
"
^4.17.21
"
},
"devDependencies"
:
{
"
@koishijs/plugin-cache-lru
"
:
"
^1.0.0-rc.0
"
,
"
@koishijs/plugin-console
"
:
"
^3.3.2
"
,
"
@koishijs/plugin-database-memory
"
:
"
^1.3.0
"
,
"
@koishijs/plugin-sandbox
"
:
"
^1.1.3
"
,
"
@koishijs/plugin-console
"
:
"
^4.1.1
"
,
"
@koishijs/plugin-database-memory
"
:
"
^1.4.1
"
,
"
@koishijs/plugin-sandbox
"
:
"
^2.0.1
"
,
"
@types/jest
"
:
"
^27.4.0
"
,
"
@types/lodash
"
:
"
^4.14.175
"
,
"
@types/node
"
:
"
^16.11.1
"
,
"
@typescript-eslint/eslint-plugin
"
:
"
^4.33.0
"
,
"
@typescript-eslint/parser
"
:
"
^4.33.0
"
,
"
esbuild-loader
"
:
"
^2.19.0
"
,
"
eslint
"
:
"
^7.32.0
"
,
"
eslint-config-prettier
"
:
"
^8.3.0
"
,
"
eslint-plugin-prettier
"
:
"
^3.4.1
"
,
...
...
@@ -56,7 +56,8 @@
"
ws
"
:
"
^8.3.0
"
},
"peerDependencies"
:
{
"koishi"
:
"^4.7.5"
"koishi"
:
"^4.8.2"
,
"koishi-plugin-cache-aragami"
:
"^1.0.4"
},
"jest"
:
{
"moduleFileExtensions"
:
[
...
...
src/index.ts
View file @
b36db486
// import 'source-map-support/register';
import
{
Random
,
sleep
,
Cache
}
from
'
koishi
'
;
import
{
Random
,
sleep
,
Session
}
from
'
koishi
'
;
import
{
ThesaurusPluginConfig
}
from
'
./config
'
;
import
{
promises
as
fs
}
from
'
fs
'
;
import
_
from
'
lodash
'
;
import
{
Base
Plugin
,
Starter
Plugin
,
DefinePlugin
,
Inject
,
LifecycleEvents
,
UseMiddleware
,
UseCommand
,
CommandUsage
,
CommandExample
,
PutOption
,
PutUserName
,
PutSession
,
}
from
'
koishi-thirdeye
'
;
import
AragamiPlugin
,
{
CacheKey
}
from
'
koishi-plugin-cache-aragami
'
;
import
{
Next
}
from
'
koa
'
;
export
interface
ChatSession
{
class
ChatSession
{
@
CacheKey
()
sessionId
:
string
;
name
:
string
;
playedWords
:
string
[];
}
export
*
from
'
./config
'
;
declare
module
'
koishi
'
{
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace
Cache
{
interface
Tables
{
thesaurusChatSession
:
ChatSession
;
}
}
}
@
DefinePlugin
({
name
:
'
thesaurus
'
,
schema
:
ThesaurusPluginConfig
})
export
default
class
ThesaurusPlugin
extends
BasePlugin
<
ThesaurusPluginConfig
>
extends
StarterPlugin
(
ThesaurusPluginConfig
)
implements
LifecycleEvents
{
@
Inject
(
true
)
private
cache
:
Cache
;
private
aragami
:
AragamiPlugin
;
private
wordData
:
Record
<
string
,
string
[]
>
=
{};
private
async
loadWords
():
Promise
<
void
>
{
...
...
@@ -63,31 +65,16 @@ export default class ThesaurusPlugin
if
(
!
sessionId
)
{
return
;
}
return
this
.
cache
.
get
(
'
thesaurusChatSession
'
,
sessionId
);
return
this
.
aragami
.
get
(
ChatSession
,
sessionId
);
}
private
async
setToCache
(
sessionId
:
string
,
chatSession
:
ChatSession
)
{
if
(
!
sessionId
)
{
return
;
}
return
this
.
cache
.
set
(
'
thesaurusChatSession
'
,
sessionId
,
chatSession
,
this
.
config
.
chatTimeout
,
);
private
async
setToCache
(
chatSession
:
ChatSession
)
{
return
this
.
aragami
.
set
(
ChatSession
,
chatSession
);
}
private
async
clearCacheOf
(
sessionId
:
string
)
{
if
(
!
sessionId
)
{
return
;
}
return
this
.
cache
.
del
(
'
thesaurusChatSession
'
,
sessionId
);
return
this
.
aragami
.
del
(
ChatSession
,
sessionId
);
}
private
async
replyWith
(
word
:
string
,
sessionId
:
string
,
chatSession
:
ChatSession
,
)
{
private
async
replyWith
(
word
:
string
,
chatSession
:
ChatSession
)
{
if
(
!
word
)
{
return
;
}
...
...
@@ -95,7 +82,7 @@ export default class ThesaurusPlugin
if
(
chatSession
.
playedWords
.
length
>
this
.
config
.
trackingLength
)
{
chatSession
.
playedWords
.
shift
();
}
await
this
.
setToCache
(
sessionId
,
chatSession
);
await
this
.
setToCache
(
chatSession
);
return
word
.
replace
(
/你/g
,
chatSession
.
name
).
trim
();
}
...
...
@@ -114,11 +101,7 @@ export default class ThesaurusPlugin
return
Random
.
pick
(
availableWords
);
}
private
async
replyChat
(
word
:
string
,
sessionId
:
string
,
chatSession
:
ChatSession
,
)
{
private
async
replyChat
(
word
:
string
,
chatSession
:
ChatSession
)
{
const
matchingPatterns
=
Object
.
keys
(
this
.
wordData
).
filter
((
w
)
=>
word
.
includes
(
w
),
);
...
...
@@ -130,18 +113,14 @@ export default class ThesaurusPlugin
if
(
!
remainingAllWords
.
length
)
{
remainingAllWords
=
allWords
;
}
return
this
.
replyWith
(
Random
.
pick
(
remainingAllWords
),
sessionId
,
chatSession
,
);
return
this
.
replyWith
(
Random
.
pick
(
remainingAllWords
),
chatSession
);
}
const
replyWord
=
this
.
tryPatterns
(
matchingPatterns
,
new
Set
<
string
>
(
chatSession
.
playedWords
),
)
||
this
.
tryPatterns
(
matchingPatterns
,
new
Set
<
string
>
());
return
this
.
replyWith
(
replyWord
,
sessionId
,
chatSession
);
return
this
.
replyWith
(
replyWord
,
chatSession
);
}
private
formatName
(
name
:
string
)
{
if
(
name
.
match
(
/^
[
a-zA-Z0-9
]
/
))
{
...
...
@@ -153,56 +132,42 @@ export default class ThesaurusPlugin
return
name
;
}
async
onConnect
()
{
const
ctx
=
this
.
ctx
;
this
.
cache
.
table
(
'
thesaurusChatSession
'
,
{
maxAge
:
this
.
config
.
chatTimeout
,
});
await
this
.
loadWords
();
ctx
.
middleware
(
async
(
session
,
next
)
=>
{
const
sessionId
=
`
${
session
.
platform
}
.
${
session
.
selfId
}
.
${
session
.
userId
}
`
;
const
chatSession
=
await
this
.
getFromCache
(
sessionId
);
if
(
chatSession
)
{
//ctx
// .logger('thesaurus')
// .warn(`${session.userId} in chat ${JSON.stringify(chatSession)}`);
if
(
session
.
content
===
'
quit
'
)
{
await
this
.
clearCacheOf
(
sessionId
);
await
session
.
send
(
'
记得下次再找我聊喔~
'
);
return
;
}
const
reply
=
await
this
.
replyChat
(
session
.
content
,
sessionId
,
chatSession
,
);
await
session
.
send
(
reply
);
@
UseMiddleware
()
async
replyMessage
(
session
:
Session
,
next
:
Next
)
{
const
sessionId
=
`
${
session
.
platform
}
.
${
session
.
selfId
}
.
${
session
.
userId
}
`
;
const
chatSession
=
await
this
.
getFromCache
(
sessionId
);
if
(
chatSession
)
{
if
(
session
.
content
===
'
quit
'
)
{
await
this
.
clearCacheOf
(
sessionId
);
return
'
记得下次再找我聊喔~
'
;
}
//ctx.logger('thesaurus').warn(`${session.userId} outside session`);
return
next
();
return
await
this
.
replyChat
(
session
.
content
,
chatSession
);
}
return
next
();
}
@
UseCommand
(
'
startchat
'
,
'
开始聊天
'
)
@
CommandUsage
(
'
聊天开始后, quit 停止聊天。
'
)
@
CommandExample
(
'
startchat --name Shigma 可以使用 Shigma 作为自己的名字来聊天。
'
,
)
async
startChat
(
@
PutOption
(
'
name
'
,
'
--name <name:string>
'
)
optionName
:
string
,
@
PutUserName
()
databaseName
:
string
,
@
PutSession
()
session
:
Session
,
)
{
const
sessionId
=
`
${
session
.
platform
}
.
${
session
.
selfId
}
.
${
session
.
userId
}
`
;
const
name
=
optionName
||
databaseName
;
const
formattedName
=
this
.
formatName
(
name
);
await
this
.
setToCache
({
sessionId
,
name
:
formattedName
,
playedWords
:
[],
});
ctx
.
command
(
'
startchat
'
,
'
开始聊天
'
)
.
usage
(
'
聊天开始后, quit 停止聊天。
'
)
.
example
(
'
startchat --name Shigma 可以使用 Shigma 作为自己的名字来聊天。
'
)
.
option
(
'
name
'
,
'
--name <name:string>
'
)
.
userFields
([
'
name
'
])
.
action
(
async
(
argv
)
=>
{
const
{
session
,
options
}
=
argv
;
const
sessionId
=
`
${
session
.
platform
}
.
${
session
.
selfId
}
.
${
session
.
userId
}
`
;
let
name
=
session
.
username
;
if
(
options
.
name
)
{
name
=
options
.
name
;
}
if
(
!
name
)
{
name
=
'
你
'
;
}
const
formattedName
=
this
.
formatName
(
name
);
await
this
.
setToCache
(
sessionId
,
{
name
:
formattedName
,
playedWords
:
[],
});
return
`
${
formattedName
}
,开始和我聊天吧。聊累了的话,输入 quit 就可以结束聊天哦。`
.
trim
();
});
return
`
${
formattedName
}
,开始和我聊天吧。聊累了的话,输入 quit 就可以结束聊天哦。`
.
trim
();
}
async
onConnect
()
{
await
this
.
loadWords
();
}
}
tests/sample.spec.ts
View file @
b36db486
import
{
App
}
from
'
koishi
'
;
import
{
Context
}
from
'
koishi
'
;
import
TargetPlugin
from
'
../src
'
;
describe
(
'
Test of plugin.
'
,
()
=>
{
let
app
:
App
;
let
app
:
Context
;
beforeEach
(
async
()
=>
{
app
=
new
App
();
app
=
new
Context
();
// app.plugin(TargetPlugin);
await
app
.
start
();
});
...
...
webpack.config.js
View file @
b36db486
const
path
=
require
(
'
path
'
);
const
packgeInfo
=
require
(
'
./package.json
'
);
const
{
ESBuildMinifyPlugin
}
=
require
(
'
esbuild-loader
'
);
function
externalsFromDep
()
{
return
Object
.
fromEntries
(
...
...
@@ -43,4 +44,11 @@ module.exports = {
koishi
:
'
koishi
'
,
...(
packAll
?
{}
:
externalsFromDep
()),
},
optimization
:
{
minimizer
:
[
new
ESBuildMinifyPlugin
({
keepNames
:
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