Mirai-CSharp 2.1.4-preview.1

This is a prerelease version of Mirai-CSharp.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Mirai-CSharp --version 2.1.4-preview.1                
NuGet\Install-Package Mirai-CSharp -Version 2.1.4-preview.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="Mirai-CSharp" Version="2.1.4-preview.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Mirai-CSharp --version 2.1.4-preview.1                
#r "nuget: Mirai-CSharp, 2.1.4-preview.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 Mirai-CSharp as a Cake Addin
#addin nuget:?package=Mirai-CSharp&version=2.1.4-preview.1&prerelease

// Install Mirai-CSharp as a Cake Tool
#tool nuget:?package=Mirai-CSharp&version=2.1.4-preview.1&prerelease                

<div align="center"> <h1>Mirai-CSharp</h1> </div> <div align="center"> <a href="https://www.nuget.org/packages/Mirai-CSharp"> <img src="https://img.shields.io/nuget/v/Mirai-CSharp"/ ></a> <a href="https://www.nuget.org/packages/Mirai-CSharp"> <img src="https://img.shields.io/nuget/vpre/Mirai-CSharp"></a> <a href="https://www.nuget.org/packages/Mirai-CSharp"> <img src="https://img.shields.io/nuget/dt/Mirai-CSharp"></a> <img src="https://img.shields.io/github/last-commit/Executor-Cheng/Mirai-CSharp"> <img src="https://img.shields.io/github/stars/Executor-Cheng/Mirai-CSharp"> </div>

关于本项目

这是一个帮助C#开发者与 Mirai 交互的项目
它通过调用 mirai-api-http 提供的 http-api 与其交互

开始使用

安装

最简单的方式是从 nuget 上获取 Mirai-CSharp 包, 并且我们也推荐你在 nuget 包管理器中为项目安装它, 不过你也可以手动克隆项目, 编译, 并直接引用链接库.

在使用 nuget 安装包时, 如若要使用最新功能, 请勾选 "包括发行版"

注意, 最新版本已将包分离为 Mirai-CSharp 以及 Mirai-CSharp.HttpApi, 其中第一个中只包含程序接口之类的, 第二个中包含的是其实现, 并且在该预览版中, 与正式版发布的内容差异较大, 项目结构有巨大改变

示例

下面以一个最简单的控制台程序为示例, 对 QQ 内的任何 at 自己了的群聊消息响应 "Hello world" 文本消息

在目前已正式发布的最新版本中, Mirai-CSharp 的常用核心组件位于 Mirai_CSharp 以及 Mirai_CSharp.Models 命名空间中.

首先我们可以引用它, 下面是基础框架:

using System;
using System.Linq;
using Mirai.CSharp;
using Mirai.CSharp.Models;

namespace TestProj
{
    class Program
    {
        static void Main(string[] args)
        {
            
        }
    }
}

Mirai-CSharp 是要与 Mirai 的 mira-http-api 进行交互的, 所以我们接下来创建一个会话(Session), 并连接到在 Mirai 中已经登录的 QQ

// 下面是位于 Main 方法的代码
MiraiHttpSession session = new MiraiHttpSession();               // 创建会话
session.ConnectAsync(                                            // 连接并等待
    new MiraiHttpSessionOptions("localhost", 1234, "authKey"),   // 连接选项, 地址, 端口, 以及验证密钥, 这些均位于 mirai-http-api 配置文件中
    1234567890).Wait();                                          // Mirai 中已经登录的 QQ 机器人的 QQ 号码	

下面为 session 添加群聊成员消息时间的处理方法:

// 下面是位于 Main 方法的代码
session.GroupMessageEvt += async (sender, e) =>      
{
    await sender.SendGroupMessageAsync(e.Sender.Group.Id, new PlainMessage("Hello world"));   // 在消息发送者所在的群聊内发送 Hello world
    return false;
};
session.GroupMessageEvt += async (sender, e) =>      // Mirai-CSharp 的事件处理应该是纯异步的, 我们应该使用异步方法(返回Task<bool>)
{
    if (e.Chain.Where(v => v is AtMessage atMsg && atMsg.Target == session.QQNumber).Any())       // 判断是否 at 自己
        await sender.SendGroupMessageAsync(e.Sender.Group.Id, new PlainMessage("Hello world"));   // 发送 "Hello world"
    
    // PlainMessage 位于 Mirai_CSharp.Models 命名空间下, 基于 IMessage 

    return false;    // Task 的返回结果标识当前事件是否被阻断, 如果返回 true, 那么后面的事件订阅者将不会收到事件 (这里返回false表示不阻断)
};

改动

在当前的最新版本(预览版)中, 包已经分离开来, 由旧的只有一个 "Mirai-CSharp" 就包含所有功能, 变更为 "Mirai-CSharp" 提供基本接口, "Mirai-CSharp.HttpApi" 提供接口实现.

并且一些类型的命名空间也有所改动, 例如原来的 MiraiHttpSession 被移动到 Mirai.CSharp.HttpApi.Models 命名空间. 所以在使用最新预览版时应注意命名空间的更改.

注意事项

  • 本项目使用C# 9.0编写, 你需要至少.NET Core 2.0.NET Framework 4.6.1才能使用本项目, 其中所有的api均为异步方法

使用例子

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 is compatible.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on Mirai-CSharp:

Package Downloads
CocoaFramework

Use this project to develop QQ robots in a brand new way.

Mirai-CSharp.HttpApi

Using this project to interop with mirai-api-http

ProcedureOrientedSessionFramework

To develop mirai bots in a procedure oriented manner.

Mirai-CSharp.ImageConverter

Provides default IImageConverter implementation.

Mirai-CSharp.VoiceConverter

Provides default IVoiceConverter implementation.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on Mirai-CSharp:

Repository Stars
GardenHamster/Theresa3rd-Bot
一个QQ群聊机器人,基于Mirai和GoCQHttp,包括 Pixiv搜索、Pixiv推送、Pixiv日榜、词云、定时提醒、复读机等功能
TRKS-Team/WFBot
一个 Warframe 的多聊天平台机器人
Version Downloads Last updated
2.1.9 1,177 12/31/2022
2.1.9-preview.1 224 10/18/2022
2.1.8 503 8/24/2022
2.1.7 653 5/22/2022
2.1.7-preview.1 131 5/16/2022
2.1.6 429 5/15/2022
2.1.6-preview.1 178 3/22/2022
2.1.5 468 3/14/2022
2.1.4 815 1/30/2022
2.1.4-preview.1 135 1/24/2022
2.1.3 739 1/18/2022
2.1.2 441 12/14/2021
2.1.1 717 12/4/2021
2.1.0 407 11/29/2021
2.0.0 467 11/9/2021
2.0.0-rc.2 157 11/2/2021
2.0.0-rc.1 185 9/23/2021
2.0.0-preview.2 224 9/10/2021
1.0.2.2-preview.4 1,422 1/3/2021
1.0.2.2-preview.3 249 12/31/2020
1.0.2.2-preview.2 269 12/19/2020
1.0.2.2-preview.1 262 12/7/2020
1.0.2.1 3,651 9/10/2020
1.0.2 571 8/18/2020
1.0.1.9 444 8/17/2020 1.0.1.9 is deprecated because it has critical bugs.