Skip to content

Commit

Permalink
[OneBot] Ignore Self Message Option
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Dec 7, 2023
1 parent a338914 commit dd6726f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Lagrange.OneBot/Core/Message/MessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Lagrange.OneBot.Core.Entity.Message;
using Lagrange.OneBot.Core.Network;
using Lagrange.OneBot.Database;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace Lagrange.OneBot.Core.Message;

Expand All @@ -17,6 +19,8 @@ public sealed class MessageService
{
private readonly LagrangeWebSvcCollection _service;
private readonly ContextBase _context;
private readonly IConfiguration _config;

private static readonly Dictionary<Type, (string, ISegment)> EntityToSegment;

static MessageService()
Expand All @@ -32,11 +36,12 @@ static MessageService()
}
}

public MessageService(BotContext bot, LagrangeWebSvcCollection service, ContextBase context)
public MessageService(BotContext bot, LagrangeWebSvcCollection service, ContextBase context, IConfiguration config)
{
_service = service;
_context = context;

_config = config;

var invoker = bot.Invoker;

invoker.OnFriendMessageReceived += OnFriendMessageReceived;
Expand All @@ -62,9 +67,11 @@ private void OnFriendMessageReceived(BotContext bot, FriendMessageEvent e)

private void OnGroupMessageReceived(BotContext bot, GroupMessageEvent e)
{
var request = new OneBotGroupMsg(bot.UpdateKeystore().Uin, e.Chain.GroupUin ?? 0,Convert(e.Chain),
e.Chain.GroupMemberInfo ?? throw new Exception("Group member not found"));
if (_config.GetValue<bool>("Message:IgnoreSelf") && e.Chain.FriendUin == bot.BotUin) return; // ignore self message

var request = new OneBotGroupMsg(bot.UpdateKeystore().Uin, e.Chain.GroupUin ?? 0, Convert(e.Chain),
e.Chain.GroupMemberInfo ?? throw new Exception("Group member not found"));

_ = _service.SendJsonAsync(request);
}

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ Please use Lagrange.Core responsibly and in accordance with the law.
"AutoReconnect": true,
"GetOptimumServer": true
},
"Message": {
"IgnoreSelf": true
},
"Implementation": {
"ForwardWebSocket": {
"Host": "127.0.0.1",
Expand Down

0 comments on commit dd6726f

Please sign in to comment.