From e7dfe19d452edac02e979b84f469ee020c63cb02 Mon Sep 17 00:00:00 2001 From: Guimc Date: Sat, 30 Nov 2024 11:02:34 +0800 Subject: [PATCH] feat: blacklist Closes #8 --- YounBot/Command/Implements/YounkooCommand.cs | 58 ++++++++++++-------- YounBot/Config/YounBotConfig.cs | 4 +- YounBot/YounBotApp.cs | 3 +- YounBot/YounBotAppBuilder.cs | 6 +- 4 files changed, 42 insertions(+), 29 deletions(-) diff --git a/YounBot/Command/Implements/YounkooCommand.cs b/YounBot/Command/Implements/YounkooCommand.cs index 77e40e8..1feddd1 100644 --- a/YounBot/Command/Implements/YounkooCommand.cs +++ b/YounBot/Command/Implements/YounkooCommand.cs @@ -26,6 +26,7 @@ public async Task AddAdmin(BotContext context, MessageChain chain, BotGroupMembe YounBotApp.Config!.BotAdmins!.Add(member.Uin); SaveConfig("younbot-config.json", YounBotApp.Config, true); await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value) + .Forward(chain) .Text("[滥权小助手] ").Mention(member.Uin) .Text(" 被提升为管理员! ").Build()); } @@ -40,6 +41,7 @@ public async Task RemoveAdmin(BotContext context, MessageChain chain, BotGroupMe YounBotApp.Config!.BotAdmins!.Remove(member.Uin); SaveConfig("younbot-config.json", YounBotApp.Config, true); await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value) + .Forward(chain) .Text("[滥权小助手] ").Mention(member.Uin) .Text(" 被取消管理员! ").Build()); } @@ -66,6 +68,7 @@ public async Task Stop(BotContext context, MessageChain chain) if (IsBotOwner(chain)) { await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value) + .Forward(chain) .Text("Stopping YounBot " + YounBotApp.VERSION).Build()); SaveConfig("younbot-config.json", YounBotApp.Config!, true); SaveConfig(YounBotApp.Configuration["ConfigPath:Keystore"] ?? "keystore.json", YounBotApp.Client!.UpdateKeystore(), true); @@ -97,6 +100,7 @@ public async Task Status(BotContext context, MessageChain chain) return; } await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value) + .Forward(chain) .Text($"Uptime: {DateTimeOffset.Now.ToUnixTimeSeconds() - YounBotApp.UpTime!.Value}s\n") .Text($"Bot Version: {YounBotApp.VERSION}\n") .Text($"Receive pre min (1m/5m/10m): {MessageCounter.GetReceivedMessageLastMinutes()}/{Math.Round(MessageCounter.GetReceivedMessageLastMinutes(5)/5d, 2)}/{Math.Round(MessageCounter.GetReceivedMessageLastMinutes(10) / 10d, 2)}\n") @@ -117,26 +121,36 @@ public async Task Ban(BotContext context, MessageChain chain, BotGroupMember mem } } - // [Command("blacklist", "黑名单")] - // public async Task Blacklist(BotContext context, MessageChain chain, BotGroupMember member, uint group = 0, string reason = "No reason") - // { - // if (HasPermission(chain)) - // { - // ILiteCollection? collection = YounBotApp.Db!.GetCollection("blacklist"); - // // find if the user is in the blacklist - // if (collection.Exists(x => x == new BsonValue(member.Uin.ToString()))) - // { - // collection.Delete(new BsonValue(member.Uin.ToString())); - // await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value) - // .Text("已移除 ").Build()); - // } - // else - // { - // collection.Insert(new BsonValue(member.Uin.ToString())); - // await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value) - // .Text("[滥权小助手] ").Mention(member.Uin) - // .Text("已添加").Build()); - // } - // } - // } + [Command("blacklist", "黑名单")] + public async Task Blacklist(BotContext context, MessageChain chain, BotGroupMember member) + { + if (HasPermission(chain)) + { + if (HasPermission(member)) + { + await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value) + .Forward(chain) + .Text("无法将机器人管理员添加到黑名单").Build()); + return; + } + + // find if the user is in the blacklist + if (YounBotApp.Config!.BlackLists!.Contains(member.Uin)) + { + YounBotApp.Config!.BlackLists!.Remove(member.Uin); + await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value) + .Forward(chain) + .Text("[滥权小助手] 已移除").Build()); + } + else + { + YounBotApp.Config!.BlackLists!.Add(member.Uin); + await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value) + .Forward(chain) + .Text("[滥权小助手] 已添加").Build()); + } + + SaveConfig("younbot-config.json", YounBotApp.Config!, true); + } + } } \ No newline at end of file diff --git a/YounBot/Config/YounBotConfig.cs b/YounBot/Config/YounBotConfig.cs index c732cff..8e3e4ae 100644 --- a/YounBot/Config/YounBotConfig.cs +++ b/YounBot/Config/YounBotConfig.cs @@ -10,6 +10,7 @@ public class YounBotConfig public int MaxMessageCache { get; set; } public int MaxGroupMessageCache { get; set; } public List? BotAdmins { get; set; } + public List? BlackLists { get; set; } public static YounBotConfig NewConfig() => new() { @@ -19,6 +20,7 @@ public class YounBotConfig WorkersAiBasicAuth = "username:password", MaxMessageCache = 12, MaxGroupMessageCache = 25, - BotAdmins = new List() + BotAdmins = new List(), + BlackLists = new List() }; } \ No newline at end of file diff --git a/YounBot/YounBotApp.cs b/YounBot/YounBotApp.cs index 9911711..a1f89cf 100644 --- a/YounBot/YounBotApp.cs +++ b/YounBot/YounBotApp.cs @@ -138,8 +138,7 @@ public Task Run() if (!Config!.WorkersAiUrl!.Equals("http://0.0.0.0/")) await AntiAd.OnGroupMessage(context, @event); - // ILiteCollection? collection = Db!.GetCollection("blacklist"); - // if (collection.Exists(x => x == new BsonValue(@event.Chain.FriendUin.ToString()))) return; + if (Config!.BlackLists!.Contains(@event.Chain.FriendUin)) return; string text = MessageUtils.GetPlainText(@event.Chain); string commandPrefix = Configuration["CommandPrefix"] ?? "/"; // put here for auto reload if (text.StartsWith(commandPrefix)) diff --git a/YounBot/YounBotAppBuilder.cs b/YounBot/YounBotAppBuilder.cs index 8e9d7c4..50c4dc2 100644 --- a/YounBot/YounBotAppBuilder.cs +++ b/YounBot/YounBotAppBuilder.cs @@ -49,10 +49,8 @@ public void ConfigureBots() SaveConfig(configPath, _younBotConfig); _younBotConfig = JsonSerializer.Deserialize(File.ReadAllText(configPath)) ?? YounBotConfig.NewConfig(); - if (_younBotConfig.BotAdmins == null) { - _younBotConfig.BotAdmins = new(); - } - + if (_younBotConfig.BotAdmins == null) _younBotConfig.BotAdmins = new(); + if (_younBotConfig.BlackLists == null) _younBotConfig.BlackLists = new(); } public YounBotApp Build() => new(this);