Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
feat!: move IAdapter.GetBots() method to IAdapter.Bots property
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdayo committed Apr 19, 2023
1 parent a71f5d7 commit c3c2c36
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/Flandre.Adapters.Discord/DiscordAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace Flandre.Adapters.Discord;
/// </summary>
public sealed class DiscordAdapter : IAdapter
{
/// <inheritdoc cref="IAdapter.Bots"/>
public IEnumerable<Bot> Bots => _bots.AsReadOnly();

private readonly List<DiscordBot> _bots = new();

/// <summary>
Expand All @@ -31,9 +34,6 @@ public Task StartAsync()
public async Task StopAsync()
{
}

/// <inheritdoc cref="IAdapter.GetBots"/>
public IEnumerable<Bot> GetBots() => _bots;
}

/// <summary>
Expand Down
8 changes: 3 additions & 5 deletions src/Flandre.Adapters.Konata/KonataAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace Flandre.Adapters.Konata;
/// </summary>
public class KonataAdapter : IAdapter
{
/// <inheritdocs/>
public IEnumerable<Bot> Bots => _bots.AsReadOnly();

private readonly List<KonataBot> _bots = new();
private readonly KonataAdapterConfig _config;

Expand All @@ -31,11 +34,6 @@ public KonataAdapter(KonataAdapterConfig config)
/// 停止适配器
/// </summary>
public Task StopAsync() => Task.WhenAll(_bots.ConvertAll(bot => bot.StopAsync()));

/// <summary>
/// 获取 bot 列表
/// </summary>
public IEnumerable<Bot> GetBots() => _bots;
}

/// <summary>
Expand Down
7 changes: 2 additions & 5 deletions src/Flandre.Adapters.Mock/MockAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ namespace Flandre.Adapters.Mock;

public class MockAdapter : IAdapter
{
public IEnumerable<Bot> Bots => new[] { Bot };

internal readonly MockBot Bot = new();

public Task StartAsync() => Task.CompletedTask;

public Task StopAsync() => Task.CompletedTask;

public IEnumerable<Bot> GetBots()
{
return new[] { Bot };
}
}
4 changes: 2 additions & 2 deletions src/Flandre.Adapters.OneBot/OneBotAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace Flandre.Adapters.OneBot;

public class OneBotAdapter : IAdapter
{
public IEnumerable<Bot> Bots => _bots.AsReadOnly();

private readonly List<Bot> _bots = new();

private readonly OneBotAdapterConfig _config;
Expand Down Expand Up @@ -36,8 +38,6 @@ public async Task StopAsync()
{
await Task.WhenAll(_bots.Select(bot => bot.StopAsync()));
}

public IEnumerable<Bot> GetBots() => _bots;
}

public class OneBotAdapterConfig
Expand Down
10 changes: 5 additions & 5 deletions src/Flandre.Core/Common/IAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
/// </summary>
public interface IAdapter
{
/// <summary>
/// 适配器机器人列表
/// </summary>
public IEnumerable<Bot> Bots { get; }

/// <summary>
/// 启动适配器
/// </summary>
Expand All @@ -14,9 +19,4 @@ public interface IAdapter
/// 停止适配器
/// </summary>
public Task StopAsync();

/// <summary>
/// 获取适配器 bot 列表
/// </summary>
public IEnumerable<Bot> GetBots();
}
13 changes: 7 additions & 6 deletions src/Flandre.Framework/FlandreApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public sealed partial class FlandreApp : IHost
private readonly List<IAdapter> _adapters;
private readonly List<Type> _pluginTypes;
private readonly List<Func<MiddlewareContext, Func<Task>, Task>> _middleware = new();
private readonly List<Bot> _bots = new();

/// <summary>
/// 机器人实例
/// 所有机器人实例
/// </summary>
public List<Bot> Bots { get; } = new();
public IEnumerable<Bot> Bots => _bots.AsReadOnly();

/// <summary>
/// 服务
Expand Down Expand Up @@ -71,7 +72,7 @@ internal FlandreApp(IHost hostApp, List<Type> pluginTypes, List<IAdapter> adapte
Logger = Services.GetRequiredService<ILogger<FlandreApp>>();

foreach (var adapter in _adapters)
Bots.AddRange(adapter.GetBots());
_bots.AddRange(adapter.Bots);
}

#region 初始化步骤
Expand Down Expand Up @@ -99,7 +100,7 @@ void WithCatch(Type pluginType, Func<Plugin, Task> subscriber, string? eventName
}
});

foreach (var bot in Bots)
foreach (var bot in _bots)
{
bot.OnMessageReceived += (_, e) => Task.Run(async () =>
{
Expand Down Expand Up @@ -132,7 +133,7 @@ void WithCatch(Type pluginType, Func<Plugin, Task> subscriber, string? eventName
foreach (var adapter in _adapters)
{
var adapterType = adapter.GetType();
foreach (var bot in adapter.GetBots())
foreach (var bot in adapter.Bots)
bot.OnLogging += (_, e) =>
Services.GetRequiredService<ILoggerFactory>()
.CreateLogger(adapterType.FullName ?? adapterType.Name)
Expand Down Expand Up @@ -212,7 +213,7 @@ private async Task StartAsyncCore(bool withDefaults, CancellationToken cancellat
var cmdService = Services.GetRequiredService<CommandService>();

Logger.LogInformation("App started");
Logger.LogDebug("Total {AdapterCount} adapters, {BotCount} bots", _adapters.Count, Bots.Count);
Logger.LogDebug("Total {AdapterCount} adapters, {BotCount} bots", _adapters.Count, _bots.Count);
Logger.LogDebug(
"Total {PluginCount} plugins, {CommandCount} commands, {StringShortcutCount} string shortcuts, {RegexShortcutCount} regex shortcuts, {MiddlewareCount} middleware",
_pluginTypes.Count, cmdService.RootCommandNode.CountCommands(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void TestObserveMessageReceived()
var adapter = new MockAdapter();
var client1 = adapter.GetChannelClient("testG1", "testC1", "123");
var client2 = adapter.GetChannelClient("testG2", "testC2", "456");
var bot = adapter.GetBots().First();
var bot = adapter.Bots.First();

var messageCountFrom1 = 0;
var messageCountFrom2 = 0;
Expand Down

0 comments on commit c3c2c36

Please sign in to comment.