Skip to content

Commit

Permalink
Merge pull request #78 from Mliybs/2.4
Browse files Browse the repository at this point in the history
BUG修复 属性添加 方法添加 支持输入控制台指令 注释修改
  • Loading branch information
SinoAHpx committed Jul 15, 2023
2 parents 913d89b + 68b29dc commit b2bb0f3
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 57 deletions.
24 changes: 22 additions & 2 deletions Mirai.Net/Data/Messages/Concretes/ForwardMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public record ForwardMessage : MessageBase
/// 点进消息前的显示内容
/// </summary>
[JsonProperty("display")]
public ForwardDisplay Display { get; set; } = default;
public ForwardDisplay Display { get; set; }

/// <summary>
/// 消息节点
Expand Down Expand Up @@ -92,6 +92,27 @@ public record ForwardNode
/// </summary>
public record ForwardDisplay
{
/// <summary>
/// 生成转发消息的外层显示
/// </summary>
/// <param name="title"></param>
/// <param name="brief"></param>
/// <param name="source"></param>
/// <param name="preview"></param>
/// <param name="summary"></param>
public ForwardDisplay(string title = default, string brief = default, string source = default, IEnumerable<string> preview = default, string summary = default)
{
this.Title = title;

this.Brief = brief;

this.Source = source;

this.Preview = preview;

this.Summary = summary;
}

/// <summary>
/// 标题
/// </summary>
Expand Down Expand Up @@ -121,6 +142,5 @@ public record ForwardDisplay
/// </summary>
[JsonProperty("summary")]
public string Summary { get; set; }

}
}
2 changes: 1 addition & 1 deletion Mirai.Net/Data/Messages/Concretes/MusicShareMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public record MusicShareMessage : MessageBase
public override Messages Type { get; set; } = Messages.MusicShare;

/// <summary>
/// 类型
/// 类型,即分享自的软件平台
/// </summary>
[JsonProperty("kind")] public string Kind { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion Mirai.Net/Data/Messages/Concretes/PokeMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Mirai.Net.Data.Messages.Concretes;

/// <summary>
/// 我也不知道是啥玩意
/// 戳一戳系列消息
/// </summary>
public record PokeMessage : MessageBase
{
Expand Down
10 changes: 5 additions & 5 deletions Mirai.Net/Data/Messages/MessageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public string SerializeToMiraiCode()
case ImageMessage msg:

if (string.IsNullOrEmpty(msg.ImageId))
throw new("ImageId为空!");
throw new System.ArgumentNullException("ImageId为空!");

else
return $"[mirai:image:{msg.ImageId}]";
Expand All @@ -61,7 +61,7 @@ public string SerializeToMiraiCode()
case FaceMessage msg:

if (string.IsNullOrEmpty(msg.FaceId))
throw new("FaceId为空!");
throw new System.ArgumentNullException("FaceId为空!");

else
return $"[mirai:face:{msg.FaceId}]";
Expand All @@ -82,7 +82,7 @@ public string SerializeToMiraiCode()

"FangDaZhao" => "放大招,6,-1",

_ => throw new("该Poke消息还未支持!")
_ => throw new System.NotSupportedException("该Poke消息还未支持!")
};

return $"[mirai:poke:{poke}]";
Expand All @@ -91,7 +91,7 @@ public string SerializeToMiraiCode()

return $"[mirai:dice:{msg.Value}]";

case MusicShareMessage msg: // MusicShare在mirai的2.15.0-M1版本中显示为[mirai:origin:MUSIC_SHARE]MusicShare(*这里有一长串省略的内容) 与https://github.com/mamoe/mirai/blob/dev/docs/Messages.md中所说的[mirai:musicshare:$args]不符 因此这里先采用后者的方式 使用半角逗号分隔 至少保证序列化的稳定
case MusicShareMessage msg: // MusicShare在mirai的2.15.0-M1版本中显示为[mirai:origin:MUSIC_SHARE]MusicShare(*这里有一长串省略的内容) 与https://github.com/mamoe/mirai/blob/dev/docs/Messages.md 中所说的[mirai:musicshare:$args]不符 因此这里先采用后者的方式 使用半角逗号分隔 至少保证序列化的稳定

return $"[mirai:musicshare:{msg.Kind},{msg.Title},{msg.Summary},{msg.JumpUrl},{msg.PictureUrl},{msg.MusicUrl},{msg.Brief}]";

Expand All @@ -101,7 +101,7 @@ public string SerializeToMiraiCode()

default:

throw new("遇到了不支持序列化的消息!");
throw new System.NotSupportedException("遇到了不支持序列化的消息!");
}
}

Expand Down
2 changes: 1 addition & 1 deletion Mirai.Net/Mirai.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<LangVersion>latest</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2.4.9</PackageVersion>
<PackageVersion>2.5.0</PackageVersion>
<Title>Mirai.Net</Title>
<LangVersion>latest</LangVersion>
<Description>基于mirai-api-http的轻量级mirai社区sdk</Description>
Expand Down
154 changes: 123 additions & 31 deletions Mirai.Net/Sessions/Http/Managers/ConsoleManager.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,128 @@
using Manganese.Array;
using Mirai.Net.Data.Events;
using Mirai.Net.Data.Messages;
using Mirai.Net.Data.Messages.Concretes;
using Mirai.Net.Data.Sessions;
using Mirai.Net.Utils.Internal;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Reactive;
using System.Threading.Tasks;
using System.Linq;

namespace Mirai.Net.Sessions.Http.Managers
{

/// <summary>
/// 控制台管理器
/// </summary>
/// 在功能稳定前保持可见性为 internal
[Experimental]
#if DEBUG
public static class ConsoleManager
#else
internal static class ConsoleManager
#endif
{
#nullable enable
/// <summary>
/// <para>登录指令</para>
/// <para>登录后的账号会生成新的SessionKey 使用已有的MiraiBot实例是无法连接到该账号的</para>
/// <para>如果配置相同(QQ Address VerifyKey)可以再次在同一个MiraiBot上使用LaunchAsync方法之后就可以正常使用了</para>
/// <para>在LaunchAsync之前使用的任何Subscribe方法都不会应用于新登录的账号 必须重新Subscribe</para>
/// </summary>
/// <param name="qq"></param>
/// <param name="password"></param>
/// <param name="protocol"></param>
/// <returns></returns>
public static async Task LoginAsync(string qq, string password, Protocol? protocol = null)
{
var command = protocol is null
? new PlainMessage[] { $"/login {qq} {password}" }
: new PlainMessage[] { $"/login {qq} {password} {protocol.ToString()}" };

await HttpEndpoints.ExecuteCommand.PostJsonAsync(new
{
command
});
}
#nullable restore

/// <summary>
/// 内部小把戏
/// 关闭指令
/// </summary>
/// 在功能稳定前使用此方法加入内部事件类型
static ConsoleManager()
/// <returns></returns>
public static async Task StopAsync()
{
var field = typeof(ReflectionUtils).GetField("EventBases", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
if (field != null)
var command = new PlainMessage[] { "/stop" };

await HttpEndpoints.ExecuteCommand.PostJsonAsync(new
{
if (field.GetValue(null) == null)
{
field.SetValue(null, ReflectionUtils.GetDefaultInstances<EventBase>("Mirai.Net.Data.Events.Concretes"));
}
var eventBases = field.GetValue(null);
((IEnumerable<EventBase>)eventBases).Add(new EventBase[] { new CommandExecutedEvent() });
}
command
});
}

/// <summary>
/// 登出指令
/// </summary>
/// <param name="qq"></param>
/// <returns></returns>
public static async Task LogoutAsync(string qq)
{
var command = new PlainMessage[] { $"/logout {qq}" };

await HttpEndpoints.ExecuteCommand.PostJsonAsync(new
{
command
});
}

/// <summary>
/// 执行命令
/// </summary>
/// <param name="command">
/// <param name="commands">
/// <para>命令与参数</para>
/// </param>
/// <returns></returns>
public static async Task ExecuteCommandAsync(string commands)
{
var command = new PlainMessage[] { commands };

await HttpEndpoints.ExecuteCommand.PostJsonAsync(new
{
command
});
}

/// <summary>
/// 执行命令
/// </summary>
/// <param name="commands">
/// <para>命令与参数</para>
/// </param>
/// <returns></returns>
public static async Task ExecuteCommandAsync(params string[] commands)
{
var builder = new System.Text.StringBuilder();

foreach (var item in commands)
{
builder.Append(item);

builder.Append(' ');
}

var command = new PlainMessage[] { builder.ToString().Trim() };

await HttpEndpoints.ExecuteCommand.PostJsonAsync(new
{
command
});
}

/// <summary>
/// 使用消息执行命令
/// </summary>
/// <param name="command">
/// <para>命令与参数 如果使用纯文本的话需要将整条命令写在同一个PlainMessage中</para>
/// <para>控制台支持以不同消息类型作为指令的参数, 执行命令需要以消息类型作为参数, 若执行纯文本的命令, 构建多个文本格式的消息控制台会将第一个消息作为指令名, 后续消息作为参数</para>
/// </param>
/// <returns></returns>
public static async Task ExecuteCommandAsync(MessageBase[] command)
public static async Task ExecuteMessageCommandAsync(IEnumerable<MessageBase> command)
{
await HttpEndpoints.ExecuteCommand.PostJsonAsync(new
{
Expand Down Expand Up @@ -87,9 +160,8 @@ public static async Task RegisterCommandAsync(Command command)
}

/// <summary>
/// 命令
/// 注册用的命令
/// </summary>
/// 在功能稳定前保持此类为内部类
public record Command
{

Expand All @@ -103,7 +175,7 @@ public record Command
/// 指令别名
/// </summary>
[JsonProperty("alias")]
public string[] Alias { get; set; }
public IEnumerable<string> Alias { get; set; }

/// <summary>
/// 使用说明
Expand All @@ -121,7 +193,6 @@ public record Command
/// <summary>
/// 命令被执行
/// </summary>
/// 在功能稳定前保持此类为内部类
public record CommandExecutedEvent : EventBase
{
/// <summary>
Expand All @@ -132,23 +203,44 @@ public record CommandExecutedEvent : EventBase
/// <summary>
/// 命令名称
/// </summary>
[JsonProperty("name")] public string Name { get; private set; }

[JsonProperty("name")] public string Name { get; set; }
#nullable enable
/// <summary>
/// 发送命令的好友, 从控制台发送为 null
/// </summary>
[JsonProperty("friend")] public string FriendId { get; private set; }
[JsonProperty("friend")] public string? FriendId { get; set; }

/// <summary>
/// 发送命令的群成员, 从控制台发送为 nul
/// 发送命令的群成员, 从控制台发送为 null
/// </summary>
[JsonProperty("member")] public string MemberId { get; private set; }

[JsonProperty("member")] public string? MemberId { get; set; }
#nullable restore
/// <summary>
/// 指令的参数, 以消息类型传递
/// </summary>
[JsonProperty("args")] public dynamic Args { get; private set; }
[JsonProperty("args")] public dynamic Args { get; set; }
}

}

/// <summary>
/// 可选的登录协议
/// </summary>
public enum Protocol
{
/// <summary>
/// 安卓手机
/// </summary>
ANDROID_PHONE,

/// <summary>
/// 安卓平板
/// </summary>
ANDROID_PAD,

/// <summary>
/// 安卓手表
/// </summary>
ANDROID_WATCH
}
}
Loading

0 comments on commit b2bb0f3

Please sign in to comment.