Skip to content

Commit

Permalink
更新README以及相关的示例
Browse files Browse the repository at this point in the history
  • Loading branch information
Executor-Cheng committed Aug 29, 2022
1 parent 1c20ca3 commit 430274e
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 59 deletions.
23 changes: 19 additions & 4 deletions Mirai-CSharp.Example/HttpApiPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Mirai.CSharp.HttpApi.Handlers;
using Mirai.CSharp.HttpApi.Models.ChatMessages;
using Mirai.CSharp.HttpApi.Models.EventArgs;
using Mirai.CSharp.HttpApi.Parsers;
using Mirai.CSharp.HttpApi.Parsers.Attributes;
Expand All @@ -9,14 +12,26 @@ namespace Mirai.CSharp.Example
{
// 为此消息处理类标定所需要使用到的消息解析器
// 标定的特性仅在使用 IMessageFrameworkBuilder.AddHandler 和 IMessageFrameworkBuilder.ResolveParser 时才会被解析
[RegisterMiraiHttpParser(typeof(DefaultMappableMiraiHttpMessageParser<IGroupMessageEventArgs, GroupMessageEventArgs>))]
public class HttpApiPlugin : MiraiHttpMessageHandler<IGroupMessageEventArgs>, // .NET Framework 只能继承 MiraiHttpMessageHandler<TMessage> / DedicateMiraiHttpMessageHandler<TMessage>
IMiraiHttpMessageHandler<IGroupMessageEventArgs> // .NET Core 起, 你应该直接实现 IMiraiHttpMessageHandler<TMessage> / IDedicateMiraiHttpMessageHandler<TMessage> 接口
[RegisterMiraiHttpParser(typeof(DefaultMappableMiraiHttpMessageParser<IFriendMessageEventArgs, FriendMessageEventArgs>))]
public partial class HttpApiPlugin : MiraiHttpMessageHandler<IFriendMessageEventArgs>, // .NET Framework 只能继承 MiraiHttpMessageHandler<TMessage> / DedicateMiraiHttpMessageHandler<TMessage>
IMiraiHttpMessageHandler<IFriendMessageEventArgs> // .NET Core 起, 你应该直接实现 IMiraiHttpMessageHandler<TMessage> / IDedicateMiraiHttpMessageHandler<TMessage> 接口
{
private readonly ILogger<HttpApiPlugin> _logger;

public HttpApiPlugin(ILogger<HttpApiPlugin> logger)
{
_logger = logger;
}

// 使用 .NET Core 时, 删去 override 和 基类继承
public override Task HandleMessageAsync(IMiraiHttpSession session, IGroupMessageEventArgs message)
public override Task HandleMessageAsync(IMiraiHttpSession session, IFriendMessageEventArgs message)
{
LogFriendMessage(_logger, message.Sender.Name, message.Sender.Id, string.Join(null, (IEnumerable<IChatMessage>)message.Chain));
// / 来源QQ昵称 / / 来源QQ号 / / 消息链的字符串表示 /
return Task.CompletedTask;
}

[LoggerMessage(EventId = 0, Level = LogLevel.Information, Message = "{name}[{fromQQ}]:{message}")]
protected static partial void LogFriendMessage(ILogger logger, string name, long fromQQ, string message);
}
}
19 changes: 17 additions & 2 deletions Mirai-CSharp.Example/MiraiPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Mirai.CSharp.Handlers;
using Mirai.CSharp.Models.ChatMessages;
using Mirai.CSharp.Models.EventArgs;
using Mirai.CSharp.Session;

namespace Mirai.CSharp.Example
{
// 从 (I)MiraiMessageHandler<IMiraiSession, TMessage> 继承(实现), 且 TMessage 位于 Mirai.CSharp.Models.EventArgs 时, 将处理任何实现框架的消息, 包括但不限于 HttpApi, Native
// 意味着你无需引用 Mirai-CSharp.HttpApi
public class MiraiPlugin : MiraiMessageHandler<IMiraiSession, IGroupMessageEventArgs>, // .NET Framework 只能继承 MiraiMessageHandler<TClient, TMessage>
IMiraiMessageHandler<IMiraiSession, IGroupMessageEventArgs> // .NET Core 起, 你应该直接实现 IMiraiMessageHandler<TClient, TMessage> 接口
public partial class MiraiPlugin : MiraiMessageHandler<IMiraiSession, IGroupMessageEventArgs>, // .NET Framework 只能继承 MiraiMessageHandler<TClient, TMessage>
IMiraiMessageHandler<IMiraiSession, IGroupMessageEventArgs> // .NET Core 起, 你应该直接实现 IMiraiMessageHandler<TClient, TMessage> 接口
{
private readonly ILogger<MiraiPlugin> _logger;

public MiraiPlugin(ILogger<MiraiPlugin> logger)
{
_logger = logger;
}

// 使用 .NET Core 时, 删去 override 和 基类继承
public override Task HandleMessageAsync(IMiraiSession session, IGroupMessageEventArgs message)
{
LogGroupMessage(_logger, message.Sender.Group.Id, message.Sender.Name, message.Sender.Id, string.Join(null, (IEnumerable<IChatMessage>)message.Chain));
// / 来源群号 / / 来源QQ昵称 / / 来源QQ号 / / 消息链的字符串表示 /
return Task.CompletedTask;
}

[LoggerMessage(EventId = 0, Level = LogLevel.Information, Message = "[{groupNumber}] {name}[{fromQQ}]:{message}")]
protected static partial void LogGroupMessage(ILogger logger, long groupNumber, string name, long fromQQ, string message);
}
}
Loading

0 comments on commit 430274e

Please sign in to comment.