Skip to content

Commit

Permalink
Fixed modularization
Browse files Browse the repository at this point in the history
  • Loading branch information
SinoAHpx committed Oct 3, 2022
1 parent 9c67d5a commit 4e68531
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
13 changes: 12 additions & 1 deletion Mirai.Net.Test/Module1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@ public class Module1 : IModule
public async void Execute(MessageReceiverBase @base)
{
var receiver = @base.Concretize<GroupMessageReceiver>();
if (receiver.Sender.Id != "2933170747")
{
return;
}
var plain = receiver.MessageChain.GetPlainMessage();

if (plain == "/off")
{
IsEnable = false;
await receiver.SendMessageAsync("Current module will be turned off");
return;
}

await receiver.SendMessageAsync(plain);
}

public bool? IsEnable { get; set; }
Expand Down
27 changes: 2 additions & 25 deletions Mirai.Net.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,8 @@ private static async Task Main()

await bot.LaunchAsync();

bot.EventReceived.OfType<NewInvitationRequestedEvent>().Subscribe(async e =>
{
if (e.FromId == "2933170747")
{
await e.ApproveAsync();
}
});

bot.MessageReceived
.SubscribeGroupMessageAsync(async r =>
{
if (r.MessageChain.GetPlainMessage() == "/t")
{
await r.SendMessageAsync(ForwardMessage.FromChains("2933170747", "破小", new MessageChain[]
{
"Hello, World!",
new PlainMessage("This is actually a message chain") + new ImageMessage
{
Url = "https://picsum.photos/200/300"
},
"Today is a beautiful day!",
"My name is Li Hua"
}));
}
});
var modules = new Module1().GetModules();
bot.MessageReceived.WithModules(modules);

Console.WriteLine("launched");
exit.WaitOne();
Expand Down
14 changes: 14 additions & 0 deletions Mirai.Net/Utils/Scaffolds/MiraiScaffold.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
Expand All @@ -9,6 +10,7 @@
using Mirai.Net.Data.Messages.Receivers;
using Mirai.Net.Data.Sessions;
using Mirai.Net.Data.Shared;
using Mirai.Net.Modules;
using Mirai.Net.Sessions;
using Mirai.Net.Sessions.Http.Managers;
using Mirai.Net.Utils.Internal;
Expand Down Expand Up @@ -140,6 +142,18 @@ public static T Concretize<T>(this MessageReceiverBase @base) where T : MessageR
return (T)@base;
}

/// <summary>
/// 使用模块
/// </summary>
/// <param name="source"></param>
/// <param name="modules"></param>
/// <returns></returns>
public static IObservable<MessageReceiverBase> WithModules(this IObservable<MessageReceiverBase> source, List<IModule> modules)
{
source.Subscribe(modules.Raise);
return source;
}

#endregion

#region Message extension
Expand Down
7 changes: 3 additions & 4 deletions Mirai.Net/Utils/Scaffolds/ModuleScaffold.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class ModuleScaffold
/// 获取泛型参数同一个命名空间下的所有模块
/// </summary>
/// <returns></returns>
public static IEnumerable<IModule> GetModules<T>(this T module) where T : IModule
public static List<IModule> GetModules<T>(this T module) where T : IModule
{
var basic = typeof(T);

Expand All @@ -26,16 +26,15 @@ public static IEnumerable<IModule> GetModules<T>(this T module) where T : IModul
.Where(x => x!.FullName!.Contains(basic.Namespace!))
.ToList();

foreach (var type in types)
yield return Activator.CreateInstance(type) as IModule;
return types.Select(t => Activator.CreateInstance(t) as IModule).ToList();
}

/// <summary>
/// 传播订阅到模块
/// </summary>
/// <param name="modules"></param>
/// <param name="base"></param>
public static void Raise(this IEnumerable<IModule> modules, MessageReceiverBase @base)
public static void Raise(this List<IModule> modules, MessageReceiverBase @base)
{
foreach (var module in modules)
{
Expand Down

0 comments on commit 4e68531

Please sign in to comment.