RedProtocolSharp 0.1.2

dotnet add package RedProtocolSharp --version 0.1.2
                    
NuGet\Install-Package RedProtocolSharp -Version 0.1.2
                    
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="RedProtocolSharp" Version="0.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RedProtocolSharp" Version="0.1.2" />
                    
Directory.Packages.props
<PackageReference Include="RedProtocolSharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RedProtocolSharp --version 0.1.2
                    
#r "nuget: RedProtocolSharp, 0.1.2"
                    
#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.
#:package RedProtocolSharp@0.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RedProtocolSharp&version=0.1.2
                    
Install as a Cake Addin
#tool nuget:?package=RedProtocolSharp&version=0.1.2
                    
Install as a Cake Tool

RedProtocolSharp

--基于RedProtocol的C# SDK,应用于某猫

警告:当前RedProtocol与RedProtocolSharp均处于开发阶段,未来结构可能产生较大变化,请做好随时变更代码的准备

我们强烈建议您升级至0.1.0,该版本弃用了直接链式调用发送消息在高并发的情况下可能会导致消息元素重叠的Bug,并添加了对应的ChainBuilder作为新的消息发送方式

介绍

RedProtocolSharp将RedProtocol基础的消息格式进行了封装,并将收到的消息封为Event,可供开发者快速开发Bot框架

功能表

  • 接收源消息

  • 源消息文本解析

  • 源消息图片解析

  • 源消息at解析

  • 源消息回复解析

  • 源消息发送者信息解析

  • json消息解析(Red尚未提供实现)


  • 发送文本消息

  • 发送图片消息

  • 发送at消息

  • 发送回复消息

  • 发送语音消息 无需安装其他外置库,感谢silk-rs提供的dll,语音仅支持mp3格式,直接填写filePath即可

  • 撤回消息


  • 获取Bot自身消息

  • 获取Bot好友列表(Bug)

  • 获取Bot所在某群群员列表(Bug)


  • 踢人

  • 禁言人

  • 全体禁言

已知问题/feature

  1. GetMemberList返回空数组(打开UI可能可以解决问题)
  2. 现在不会,未来也不可能支持发送Json,xml等消息,也不可能支持主动添加群/好友

安装

Nuget搜索RedProtocolSharp安装

引入Release下的Dll,并自行安装WebSocketSharp,NewtonSoft.JSON,NAudio相关依赖(不推荐)

示例

可去Sample查看基础用法,下面的内容可能并不会及时更新
启动Bot
var bot = new Bot("localhost:16530","yourToken");
bot.Start();
注册Logger
bot.Logger.BotLog.OnLogger += BotLogOnHandler;
private static void BotLogOnHandler(BotLogger.Levels levels, string content)
{
    Console.WriteLine($"{levels} : {content}");
}
订阅消息
bot.Invoker.OnGroupMessageReceived += InvokerOnOnGroupMessageReceived;
    
private static void InvokerOnOnGroupMessageReceived(MessageEventArgs e)
{
    Console.WriteLine(e.chain.PeerUin);
    foreach (var item in e.chain)
    {
        if (item is TextElement textElement)
        {
            Console.WriteLine(textElement.content);
        }
    }
}
发送消息

原链式调用的方法已弃用!改为Builder创建chain发送消息!

await bot.Send.SendMessageChain(new MessageBuilder()
.SetTarget("114514", ChatTypes.GroupMessage)
.AddText("test")
.Build());
//请使用Builder创建chain对象,不要也不能直接new MessageChain
撤回消息
//假设上一步发送返回了reply
await bot.Action.Revoke(new []{reply.msgId}, reply.chatTypes, reply.peerUin);
获取Bot自身信息
var selfInfo = bot.Get.SelfInfo();
踢人
await bot.Action.Kick("114514", "1919810", false, "test");
//从群114514踢出用户1919810,不永拒,理由为test

其他操作请自行探索/类比

案例

控制台项目RedProtocolSharp.Sample

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
0.1.2 325 11/7/2023
0.1.1 237 10/24/2023
0.1.0 230 10/22/2023
0.0.4 209 10/21/2023
0.0.3 214 10/21/2023
0.0.2 225 10/20/2023
0.0.1 209 10/13/2023

删除了reply中不必要的参数