OneBot-CommandRoute
1.3.1
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package OneBot-CommandRoute --version 1.3.1
NuGet\Install-Package OneBot-CommandRoute -Version 1.3.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OneBot-CommandRoute" Version="1.3.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OneBot-CommandRoute --version 1.3.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: OneBot-CommandRoute, 1.3.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install OneBot-CommandRoute as a Cake Addin #addin nuget:?package=OneBot-CommandRoute&version=1.3.1 // Install OneBot-CommandRoute as a Cake Tool #tool nuget:?package=OneBot-CommandRoute&version=1.3.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
OneBot - Command Route Project Template
使用说明
使用反向 WS
- 克隆项目:https://github.com/ParaParty/OneBot-CommandRoute-Template
- 将
appsettings-reverse_ws-example.Development.json
重命名为appsettings.Development.json
。 - 将
appsettings-reverse_ws-example.json
重命名为appsettings.json
。 - 根据需要编辑配置文件中的信息。
- 编译运行。
使用正向 WS
- 克隆项目:https://github.com/ParaParty/OneBot-CommandRoute-Template
- 将
appsettings-ws-example.Development.json
重命名为appsettings.Development.json
。 - 将
appsettings-ws-example.json
重命名为appsettings.json
。 - 根据需要编辑配置文件中的信息。
- 编译运行。
简单的功能说明
指令路由
- 见类
OneBot.FrameworkDemo.Modules.TestModule
。/// <summary> /// 再定义一个指令: /// /// <para>参数不必要按顺序、参数会触发依赖注入</para> /// </summary> /// <param name="gid">操作群号</param> /// <param name="uid">被禁言用户</param> /// <param name="duration">禁言时长</param> [Command("mute <gid> <uid> [duration]", Alias = "禁言 <gid> <uid> [duration], 口球 <gid> <uid> [duration],", EventType = EventType.GroupMessage | EventType.PrivateMessage)] public void MuteInGroupWithGroupId(Group gid, User uid, Duration duration) { if (duration == null) duration = new Duration(600); Console.WriteLine($"禁言 {gid.Id} 群里的 {uid.Id} 用户 {duration.Seconds} 秒。"); }
- 使用属性
[Command]
来将本函数定义为一个指令函数。若接收到合适的消息,本方法会被调用。 - 方法参数会被服务容器进行依赖注入,注入环境为 Scope。
小程序信息监听
- 见类
OneBot.FrameworkDemo.Modules.TestModule
。/// <summary> /// 小程序简单监听方法, /// 这里是监听了群签到。 /// </summary> /// <param name="e"></param> [CQJson("com.tencent.qq.checkin", EventType = EventType.GroupMessage)] public void CheckInListener(GroupMessageEventArgs e) { _logger.LogInformation($"{e.Sender.Id} 签到成功!"); // 当然这里也是可以返回 0 或 1 的。 }
- 使用属性
[CQJson]
来将本函数定义为一个小程序监听函数。若接收到合适的消息,本方法会被调用。 - 方法参数会被服务容器进行依赖注入,注入环境为 Scope。
智能类型转换
- 见类
OneBot.FrameworkDemo.Models.Duration
/// <summary> /// 实现一个从字符串到 Duration 的隐式转换。 /// 1d2h3m4s -> 1天2小时3分钟4秒 /// </summary> /// <param name="value">时长</param> public static implicit operator Duration(string value)
- 实现一个从
string
到对应的类型的隐式转换即可让指令路由系统支持这个类型的转换。 - 若无法转换可抛出异常使得指令路由系统不处理该消息。
指令触发前事件
- 见类
OneBot.FrameworkDemo.Attributes.DemoBeforeCommandAttribute
。 -
- 若有需要可以自行定义一个
BeforeCommandAttribute
的子类来实现在解析指令后触发指令前进行拦截。(如实现权限判断和指令冷却时间等)
- 若有需要可以自行定义一个
依赖注入
- 每一条消息都在
Microsoft.Extensions.DependencyInjection
的一个Scope
中。可以根据需要添加自己的服务。 - 指令参数中多余的参数会被 scope 容器中的服务填充。
- 指令方法的参数填充优先级:
- 指令参数(指令定义的参数名和形参列表保持一致,若不一致可使用
CommandParameter
指定) - 指令参数数组(
[ParsedArguments] object[] args
) - 原始事件信息
BaseSoraEventArgs
、PrivateMessageEventArgs
、GroupMessageEventArgs
- Scope 对象
IServiceScope
- 剩余的未被上述可能成功填充的参数均会被从 Scope 容器中获取的对象填充。
- 指令参数(指令定义的参数名和形参列表保持一致,若不一致可使用
基本事件
- 见类
OneBot.FrameworkDemo.Modules.TestModule
。public TestModule(ICommandService commandService, ILogger<TestModule> logger) { // 通过构造函数获得指令路由服务对象 // 添加自己的处理事件方法 commandService.Event.OnGroupMessage += (scope, args) => { // 在控制台中复读群里的信息 logger.LogInformation($"{args.SourceGroup.Id} : {args.Sender.Id} : {args.Message.RawText}"); return 0; }; // 全局异常处理 commandService.Event.OnException += (scope, args, exception) => { logger.LogError($"{exception.Message}"); }; }
- 若不需要指令路由,依然可以通过监听事件来添加自己的处理逻辑。
消息处理链
- 指令函数可以返回
int
类型,基本事件返回int
类型。若返回值为 1 则该事件阻断,不再传递给后续指令或事件监听函数;若返回值为 0 则继续传递。
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net5.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 5.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 5.0.0)
- Microsoft.Extensions.Options (>= 5.0.0)
- Sora (>= 0.9.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
3.3.0 | 161 | 11/19/2022 | |
3.2.4 | 125 | 10/29/2022 | |
3.2.3 | 145 | 5/10/2022 | |
3.2.2 | 153 | 3/4/2022 | |
3.2.1 | 352 | 2/1/2022 | |
3.2.0 | 369 | 1/30/2022 | |
3.1.0 | 355 | 1/22/2022 | |
3.0.0 | 381 | 1/19/2022 | |
2.0.2 | 353 | 1/17/2022 | |
2.0.1 | 376 | 1/17/2022 | |
2.0.0 | 360 | 1/17/2022 | |
1.5.1 | 220 | 8/18/2021 | |
1.5.0 | 176 | 8/15/2021 | |
1.4.1 | 199 | 4/16/2021 | |
1.4.0 | 237 | 4/8/2021 | |
1.3.2 | 324 | 3/15/2021 | |
1.3.1 | 381 | 3/15/2021 | |
1.3.0 | 501 | 3/15/2021 | |
1.2.1 | 357 | 3/9/2021 | |
1.2.0 | 322 | 2/26/2021 | |
1.1.0 | 327 | 2/23/2021 | |
1.0.0 | 310 | 2/16/2021 |