Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在链式调用中增加.If()方法 #86

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Mirai.Net/Data/Messages/Concretes/ImageMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,22 @@ public record ImageMessage : MessageBase
/// </summary>
[JsonProperty("height")]
public string Height { get; set; }

/// <summary>
/// 图片的大小,单位为字节
/// </summary>
[JsonProperty("size")]
public string Size { get; set; }

/// <summary>
/// 图片的类型
/// </summary>
[JsonProperty("imageType")]
public string ImageType { get; set; }

/// <summary>
/// 图片是否作为表情发送,QQ会根据该属性决定是否自动缩小显示的尺寸
/// </summary>
[JsonProperty("isEmoji")]
public string IsEmoji { get; set; }
}
174 changes: 106 additions & 68 deletions Mirai.Net/Utils/Scaffolds/MessageChainBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Mirai.Net.Data.Messages;
using Mirai.Net.Data.Messages.Concretes;
using Mirai.Net.Data.Shared;
using System;

namespace Mirai.Net.Utils.Scaffolds;

Expand All @@ -10,14 +11,15 @@ namespace Mirai.Net.Utils.Scaffolds;
public class MessageChainBuilder
{
private readonly MessageChain _chain = new();
private bool _skipNext = false;

/// <summary>
/// 清除已追加的消息
/// </summary>
/// <returns></returns>
public MessageChainBuilder Clear()
{
_chain.Clear();
ExecuteOrSkip(_chain.Clear);
return this;
}

Expand All @@ -28,7 +30,7 @@ public MessageChainBuilder Clear()
/// <returns></returns>
public MessageChainBuilder Append(MessageBase messageBase)
{
_chain.Add(messageBase);
ExecuteOrSkip(() => _chain.Add(messageBase));
return this;
}

Expand All @@ -39,7 +41,7 @@ public MessageChainBuilder Append(MessageBase messageBase)
/// <returns></returns>
public MessageChainBuilder Plain(string text)
{
_chain.Add(new PlainMessage(text));
ExecuteOrSkip(() => _chain.Add(new PlainMessage(text)));
return this;
}

Expand All @@ -50,7 +52,7 @@ public MessageChainBuilder Plain(string text)
/// <returns></returns>
public MessageChainBuilder At(string qq)
{
_chain.Add(new AtMessage(qq));
ExecuteOrSkip(() => _chain.Add(new AtMessage(qq)));
return this;
}

Expand All @@ -71,9 +73,11 @@ public MessageChainBuilder At(Member member)
/// <returns></returns>
public MessageChainBuilder App(string content)
{
_chain.Add(new AppMessage
{
Content = content
ExecuteOrSkip(() => {
_chain.Add(new AppMessage
{
Content = content
});
});
return this;
}
Expand All @@ -84,8 +88,7 @@ public MessageChainBuilder App(string content)
/// <returns></returns>
public MessageChainBuilder AtAll()
{
_chain.Add(new AtAllMessage());

ExecuteOrSkip(() => _chain.Add(new AtAllMessage()));
return this;
}

Expand All @@ -96,11 +99,12 @@ public MessageChainBuilder AtAll()
/// <returns></returns>
public MessageChainBuilder Dice(string value)
{
_chain.Add(new DiceMessage
{
Value = value
ExecuteOrSkip(() => {
_chain.Add(new DiceMessage
{
Value = value
});
});

return this;
}

Expand All @@ -111,11 +115,12 @@ public MessageChainBuilder Dice(string value)
/// <returns></returns>
public MessageChainBuilder ImageFromUrl(string url)
{
_chain.Add(new ImageMessage
{
Url = url
ExecuteOrSkip(() => {
_chain.Add(new ImageMessage
{
Url = url
});
});

return this;
}

Expand All @@ -126,11 +131,12 @@ public MessageChainBuilder ImageFromUrl(string url)
/// <returns></returns>
public MessageChainBuilder ImageFromBase64(string base64)
{
_chain.Add(new ImageMessage
{
Base64 = base64
ExecuteOrSkip(() => {
_chain.Add(new ImageMessage
{
Base64 = base64
});
});

return this;
}

Expand All @@ -141,11 +147,12 @@ public MessageChainBuilder ImageFromBase64(string base64)
/// <returns></returns>
public MessageChainBuilder ImageFromPath(string file)
{
_chain.Add(new ImageMessage
{
Path = file
ExecuteOrSkip(() => {
_chain.Add(new ImageMessage
{
Path = file
});
});

return this;
}

Expand All @@ -156,11 +163,12 @@ public MessageChainBuilder ImageFromPath(string file)
/// <returns></returns>
public MessageChainBuilder ImageFromId(string id)
{
_chain.Add(new ImageMessage
{
ImageId = id
ExecuteOrSkip(() => {
_chain.Add(new ImageMessage
{
ImageId = id
});
});

return this;
}

Expand All @@ -171,11 +179,12 @@ public MessageChainBuilder ImageFromId(string id)
/// <returns></returns>
public MessageChainBuilder FlashImageFromUrl(string url)
{
_chain.Add(new FlashImageMessage
{
Url = url
ExecuteOrSkip(() => {
_chain.Add(new FlashImageMessage
{
Url = url
});
});

return this;
}

Expand All @@ -186,11 +195,12 @@ public MessageChainBuilder FlashImageFromUrl(string url)
/// <returns></returns>
public MessageChainBuilder FlashImageFromBase64(string base64)
{
_chain.Add(new FlashImageMessage
{
Base64 = base64
ExecuteOrSkip(() => {
_chain.Add(new FlashImageMessage
{
Base64 = base64
});
});

return this;
}

Expand All @@ -201,11 +211,12 @@ public MessageChainBuilder FlashImageFromBase64(string base64)
/// <returns></returns>
public MessageChainBuilder FlashImageFromPath(string file)
{
_chain.Add(new FlashImageMessage
{
Path = file
ExecuteOrSkip(() => {
_chain.Add(new FlashImageMessage
{
Path = file
});
});

return this;
}

Expand All @@ -216,11 +227,13 @@ public MessageChainBuilder FlashImageFromPath(string file)
/// <returns></returns>
public MessageChainBuilder FlashImageFromId(string id)
{
_chain.Add(new FlashImageMessage
ExecuteOrSkip(() =>
{
ImageId = id
_chain.Add(new FlashImageMessage
{
ImageId = id
});
});

return this;
}

Expand All @@ -231,11 +244,12 @@ public MessageChainBuilder FlashImageFromId(string id)
/// <returns></returns>
public MessageChainBuilder Json(string json)
{
_chain.Add(new JsonMessage
{
Json = json
ExecuteOrSkip(() => {
_chain.Add(new JsonMessage
{
Json = json
});
});

return this;
}

Expand All @@ -246,11 +260,12 @@ public MessageChainBuilder Json(string json)
/// <returns></returns>
public MessageChainBuilder Xml(string xml)
{
_chain.Add(new XmlMessage
{
Xml = xml
ExecuteOrSkip(() => {
_chain.Add(new XmlMessage
{
Xml = xml
});
});

return this;
}

Expand All @@ -261,11 +276,12 @@ public MessageChainBuilder Xml(string xml)
/// <returns></returns>
public MessageChainBuilder VoiceFromPath(string path)
{
_chain.Add(new VoiceMessage
{
Path = path
ExecuteOrSkip(() => {
_chain.Add(new VoiceMessage
{
Path = path
});
});

return this;
}

Expand All @@ -276,11 +292,12 @@ public MessageChainBuilder VoiceFromPath(string path)
/// <returns></returns>
public MessageChainBuilder VoiceFromBase64(string base64)
{
_chain.Add(new VoiceMessage
{
Base64 = base64
ExecuteOrSkip(() => {
_chain.Add(new VoiceMessage
{
Base64 = base64
});
});

return this;
}

Expand All @@ -291,11 +308,12 @@ public MessageChainBuilder VoiceFromBase64(string base64)
/// <returns></returns>
public MessageChainBuilder VoiceFromId(string id)
{
_chain.Add(new VoiceMessage
{
VoiceId = id
ExecuteOrSkip(() => {
_chain.Add(new VoiceMessage
{
VoiceId = id
});
});

return this;
}

Expand All @@ -306,11 +324,12 @@ public MessageChainBuilder VoiceFromId(string id)
/// <returns></returns>
public MessageChainBuilder VoiceFromUrl(string url)
{
_chain.Add(new VoiceMessage
{
Url = url
ExecuteOrSkip(() => {
_chain.Add(new VoiceMessage
{
Url = url
});
});

return this;
}

Expand All @@ -320,6 +339,25 @@ public MessageChainBuilder VoiceFromUrl(string url)
/// <returns></returns>
public MessageChain Build()
{
//Build方法无视_skipNext的值,直接返回消息链
return _chain;
}

/// <summary>
/// 判断条件,如果为false,则跳过下一个元素
/// </summary>
/// <returns></returns>
public MessageChainBuilder If(bool condition)
{
_skipNext = !condition;
return this;
}

// 中间方法,根据_skipNext的值决定是否执行传入的操作
private void ExecuteOrSkip(Action action)
{
if (!_skipNext)
action.Invoke();
_skipNext = false; // Reset the skip flag after each operation
}
}