OneBot-CommandRoute 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package OneBot-CommandRoute --version 1.0.0                
NuGet\Install-Package OneBot-CommandRoute -Version 1.0.0                
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.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OneBot-CommandRoute --version 1.0.0                
#r "nuget: OneBot-CommandRoute, 1.0.0"                
#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.0.0

// Install OneBot-CommandRoute as a Cake Tool
#tool nuget:?package=OneBot-CommandRoute&version=1.0.0                

OneBot - Command Route Project Template

使用说明

  1. 克隆本项目。
  2. appsettings-example.Development.json 重命名为 appsettings.Development.json
  3. appsettings-example.json 重命名为 appsettings.json
  4. 根据需要编辑配置文件中的信息。
  5. 编译运行。

简单的功能说明

指令路由

  • 见类 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] 来将本函数定义为一个指令函数。若接收到合适的消息,本方法会被调用。

智能类型转换

  • 见类 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 容器中的服务填充。
  • 指令方法的参数填充优先级:
    1. 指令参数(指令定义的参数名和形参列表保持一致,若不一致可使用 CommandParameter 指定)
    2. 指令参数数组([ParsedArguments] object[] args
    3. 原始事件信息 BaseSoraEventArgsPrivateMessageEventArgsGroupMessageEventArgs
    4. Scope 对象 IServiceScope
    5. 剩余的未被上述可能成功填充的参数均会被从 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}");
            };
        }
    
  • 若不需要指令路由,依然可以通过监听事件来添加自己的处理逻辑。
Product 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.

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 160 11/19/2022
3.2.4 124 10/29/2022
3.2.3 144 5/10/2022
3.2.2 152 3/4/2022
3.2.1 351 2/1/2022
3.2.0 367 1/30/2022
3.1.0 354 1/22/2022
3.0.0 380 1/19/2022
2.0.2 350 1/17/2022
2.0.1 375 1/17/2022 2.0.1 is deprecated because it has critical bugs.
2.0.0 359 1/17/2022
1.5.1 219 8/18/2021
1.5.0 175 8/15/2021
1.4.1 198 4/16/2021
1.4.0 236 4/8/2021
1.3.2 323 3/15/2021
1.3.1 380 3/15/2021
1.3.0 500 3/15/2021 1.3.0 is deprecated because it has critical bugs.
1.2.1 356 3/9/2021
1.2.0 321 2/26/2021
1.1.0 326 2/23/2021
1.0.0 309 2/16/2021