From e1279906c1415ae7269cb1c3444e38913b0e4479 Mon Sep 17 00:00:00 2001 From: milimoe Date: Fri, 22 Mar 2024 23:11:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=80=A6=E5=90=88MasterCommand,=20Bot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build-linux-x64.bat | 3 +- build-release-x64.bat | 5 + publish-linux-x64.bat | 5 + publish-release-x64.bat | 5 + src/Command/MasterCommand.cs | 203 ++++++++- src/ListeningTask/FriendMessageTask.cs | 244 +---------- src/ListeningTask/GroupBanTask.cs | 5 +- src/ListeningTask/GroupMessageTask.cs | 563 +++++-------------------- src/Main.cs | 45 +- src/Settings/Bot.cs | 233 +++++++++- src/Settings/Music.cs | 4 +- src/Settings/OSMCore.cs | 4 +- 12 files changed, 586 insertions(+), 733 deletions(-) create mode 100644 build-release-x64.bat create mode 100644 publish-linux-x64.bat create mode 100644 publish-release-x64.bat diff --git a/build-linux-x64.bat b/build-linux-x64.bat index 9479878..b262d7b 100644 --- a/build-linux-x64.bat +++ b/build-linux-x64.bat @@ -1,4 +1,5 @@ @echo off call cd src -call dotnet publish -c Release -r linux-x64 +call dotnet clean +call dotnet build -c Release -r linux-x64 pause \ No newline at end of file diff --git a/build-release-x64.bat b/build-release-x64.bat new file mode 100644 index 0000000..2bc0cd6 --- /dev/null +++ b/build-release-x64.bat @@ -0,0 +1,5 @@ +@echo off +call cd src +call dotnet clean +call dotnet build -c Release -r win-x64 +pause \ No newline at end of file diff --git a/publish-linux-x64.bat b/publish-linux-x64.bat new file mode 100644 index 0000000..e6d0fce --- /dev/null +++ b/publish-linux-x64.bat @@ -0,0 +1,5 @@ +@echo off +call cd src +call dotnet clean +call dotnet publish -c Release -r linux-x64 +pause \ No newline at end of file diff --git a/publish-release-x64.bat b/publish-release-x64.bat new file mode 100644 index 0000000..18f789b --- /dev/null +++ b/publish-release-x64.bat @@ -0,0 +1,5 @@ +@echo off +call cd src +call dotnet clean +call dotnet publish -c Release -r win-x64 +pause \ No newline at end of file diff --git a/src/Command/MasterCommand.cs b/src/Command/MasterCommand.cs index 760339c..dce3140 100644 --- a/src/Command/MasterCommand.cs +++ b/src/Command/MasterCommand.cs @@ -1,10 +1,195 @@ -using Milimoe.RainBOT.Settings; +using System.Text.RegularExpressions; +using Milimoe.OneBot.Framework; +using Milimoe.OneBot.Framework.Utility; +using Milimoe.OneBot.Model.Content; +using Milimoe.OneBot.Model.Message; +using Milimoe.RainBOT.Settings; namespace Milimoe.RainBOT.Command { public class MasterCommand { - public static string Execute(string command, string part, params string[] args) + public static void Execute(string command, long user_id, bool onOSMCore, long target_id, bool send_group) + { + // OSM指令 + if (GeneralSettings.IsRun && command.Contains(".osm info")) + { + // OSM核心状态 + string msg = "OSM插件运行状态:"; + if ((send_group && onOSMCore) || !send_group) + { + if (onOSMCore) + { + msg += "\r\n本群已启用OSM核心"; + } + if (GeneralSettings.IsRepeat) + { + msg += "\r\n随机复读:开启"; + msg += $"\r\n随机复读概率:{GeneralSettings.PRepeat}%" + + $"\r\n随机复读延迟区间:{GeneralSettings.RepeatDelay[0]}至{GeneralSettings.RepeatDelay[1]}秒"; + } + else msg += "\r\n随机复读:关闭"; + if (GeneralSettings.IsOSM) + { + msg += "\r\n随机OSM:开启"; + msg += $"\r\n随机OSM概率:{GeneralSettings.POSM}%"; + } + else msg += "\r\n随机OSM:关闭"; + if (GeneralSettings.IsSayNo) + { + msg += "\r\n随机反驳不:开启"; + msg += $"\r\n随机反驳不概率:{GeneralSettings.PSayNo}%"; + } + else msg += "\r\n随机反驳不:关闭"; + if (GeneralSettings.IsMute) + { + msg += "\r\n禁言抽奖:开启"; + msg += $"\r\n禁言抽奖时长区间:{GeneralSettings.MuteTime[0]}至{GeneralSettings.MuteTime[1]}秒"; + } + else msg += "\r\n禁言抽奖:关闭"; + } + else + { + msg += "\r\n本群未启用OSM核心"; + } + _ = send_group ? Bot.SendGroupMessage(target_id, "OSM状态", msg) : Bot.SendFriendMessage(target_id, "OSM状态", msg); + } + else if (GeneralSettings.IsRun && command.Contains(".osm stop")) + { + if (user_id == GeneralSettings.Master) + { + string result = Execute_Worker(".osm stop", ""); + _ = send_group ? Bot.SendGroupMessage(target_id, "OSM指令", result) : Bot.SendFriendMessage(target_id, "OSM指令", result); + } + else Access_Denied(send_group, target_id); + } + else if (!GeneralSettings.IsRun && command.Contains(".osm start")) + { + if (user_id == GeneralSettings.Master) + { + string result = Execute_Worker(".osm start", ""); + _ = send_group ? Bot.SendGroupMessage(target_id, "OSM指令", result) : Bot.SendFriendMessage(target_id, "OSM指令", result); + } + else Access_Denied(send_group, target_id); + } + else if ((command.Contains(".osm set admin") || command.Contains(".osm set unadmin")) && send_group) + { + if (user_id == GeneralSettings.Master) + { + bool enable = !command.Contains("unadmin"); + string str = command.Replace(".osm set admin", "").Replace(".osm set unadmin", "").Trim(); + string[] strs = Regex.Split(str, @"\s+"); + if (strs.Length > 0) + { + str = strs[0].Replace(@"@", "").Trim(); + if (long.TryParse(str, out long qq)) + { + SetGroupAdminContent content = new(target_id, qq, enable); + _ = Bot.SendMessage(SupportedAPI.set_group_admin, target_id, "OSM指令", content, send_group); + } + else _ = Bot.SendGroupMessage(target_id, "OSM指令", Execute_Worker(".osm set", "admin", strs.Length > 1 ? strs[1..] : [])); + } + else _ = Bot.SendGroupMessage(target_id, "OSM指令", Execute_Worker(".osm set", "admin", strs.Length > 1 ? strs[1..] : [])); + } + else Access_Denied(send_group, target_id); + } + else if (command.Contains(".osm mutegroup")) + { + TaskUtility.NewTask(async () => await Bot.MuteGroup(user_id, target_id, command)); + } + else if (command.Contains(".osm mute") && send_group) + { + TaskUtility.NewTask(async () => await Bot.Mute(user_id, target_id, command)); + } + else if (command.Contains(".osm refresh")) + { + if (user_id == GeneralSettings.Master) + { + TaskUtility.NewTask(async () => + { + await Bot.GetGroups(); + await Bot.GetGroupMembers(); + string text = "刷新缓存完成。请注意,刷新缓存会导致正在禁言中的成员无法通过私聊忏悔命令解禁。"; + _ = send_group ? Bot.SendGroupMessage(target_id, "OSM指令", text) : Bot.SendFriendMessage(target_id, "OSM指令", text); + }); + } + else Access_Denied(send_group, target_id); + } + else if (command.Contains(".osm reload")) + { + if (user_id == GeneralSettings.Master) + { + GeneralSettings.LoadSetting(); + string text = "参数设定以及权限组重新加载完成。"; + _ = send_group ? Bot.SendGroupMessage(target_id, "OSM指令", text) : Bot.SendFriendMessage(target_id, "OSM指令", text); + } + else Access_Denied(send_group, target_id); + } + else if (command.Contains(".osm set")) + { + if (user_id == GeneralSettings.Master) + { + string str = command.Replace(".osm set", "").Trim(); + string[] strs = Regex.Split(str, @"\s+"); + string result = Execute_Worker(".osm set", strs[0], strs.Length > 1 ? strs[1..] : []); + _ = send_group ? Bot.SendGroupMessage(target_id, "OSM指令", result) : Bot.SendFriendMessage(target_id, "OSM指令", result); + } + else Access_Denied(send_group, target_id); + } + else if (command.Contains(".osm sendall")) + { + TaskUtility.NewTask(async () => + { + if (user_id == GeneralSettings.Master) + { + string str = command.Replace(".osm sendall", "").Trim(); + foreach (long group_id in Bot.Groups.Select(g => g.group_id)) + { + await Bot.SendGroupMessage(group_id, "OSM指令", str); + } + } + else Access_Denied(send_group, target_id); + }); + } + else if (command.Contains(".osm send")) + { + TaskUtility.NewTask(async () => + { + if (user_id == GeneralSettings.Master) + { + string str = command.Replace(".osm send", "").Trim(); + string[] strs = Regex.Split(str, @"\s+"); + if (long.TryParse(strs[0], out long group_id) && strs.Length > 1) + { + await Bot.SendGroupMessage(group_id, "OSM指令", string.Join(" ", strs[1..])); + } + } + else Access_Denied(send_group, target_id); + }); + } + else + { + // OSM核心信息 + if (send_group) + { + GroupMessageContent content = new(target_id); + content.message.Add(new TextMessage(OSMCore.Info)); + content.message.Add(new ImageMessage("file:///" + AppDomain.CurrentDomain.BaseDirectory.ToString() + @"img\raincandy.jpg")); + _ = Bot.SendGroupMessage(target_id, "OSM核心", content); + } + else + { + FriendMessageContent content = new(target_id); + content.message.Add(new TextMessage(OSMCore.Info)); + content.message.Add(new ImageMessage("file:///" + AppDomain.CurrentDomain.BaseDirectory.ToString() + @"img\raincandy.jpg")); + _ = Bot.SendFriendMessage(target_id, "OSM核心", content); + } + return; + } + return; + } + + public static string Execute_Worker(string command, string part, params string[] args) { string msg; bool isadd; @@ -18,14 +203,6 @@ namespace Milimoe.RainBOT.Command case ".osm start": GeneralSettings.IsRun = true; return "OSM Core:服务已启动。"; - case ".osm send": - if (long.TryParse(part, out long _) && args.Length > 0) - { - return string.Join(" ", args); - } - break; - case ".osm sendall": - return part; case ".osm set": bool status; switch (part) @@ -295,6 +472,12 @@ namespace Milimoe.RainBOT.Command return "OSM Core:指令格式不正确或传入的参数不支持。\r\n格式:.osm [part] [args...]"; } + public static void Access_Denied(bool send_group, long target_id) + { + string access_denied = "你没有权限使用此指令。"; + _ = send_group ? Bot.SendGroupMessage(target_id, "OSM指令", access_denied) : Bot.SendFriendMessage(target_id, "OSM指令", access_denied); + } + public static string UpdateValue(string part, string old_value, string new_value, ConsoleColor color = ConsoleColor.Cyan) { string msg = "OSM Core:" + part + "已调整为:" + new_value + "(原始值:" + old_value + ")。"; diff --git a/src/ListeningTask/FriendMessageTask.cs b/src/ListeningTask/FriendMessageTask.cs index 83cd453..b05839e 100644 --- a/src/ListeningTask/FriendMessageTask.cs +++ b/src/ListeningTask/FriendMessageTask.cs @@ -1,13 +1,9 @@ -using System.Text.RegularExpressions; -using Milimoe.OneBot.Framework; -using Milimoe.OneBot.Framework.Interface; +using Milimoe.OneBot.Framework; using Milimoe.OneBot.Framework.Utility; using Milimoe.OneBot.Model.Content; using Milimoe.OneBot.Model.Event; -using Milimoe.OneBot.Model.Message; using Milimoe.OneBot.Model.Other; using Milimoe.OneBot.Model.QuickReply; -using Milimoe.OneBot.Utility; using Milimoe.RainBOT.Command; using Milimoe.RainBOT.Settings; @@ -37,132 +33,19 @@ namespace Milimoe.RainBOT.ListeningTask { if (e.user_id != GeneralSettings.Master && e.CheckThrow(10, out dice)) { - ColorfulCheckPass(sender, "反驳是", dice, 40); - _ = Post(e, "随机反驳是", "是你的头"); + Bot.ColorfulCheckPass(sender, "反驳是", dice, 40); + _ = Bot.SendFriendMessage(e.user_id, "随机反驳是", "是你的头"); } else if (e.user_id == GeneralSettings.Master) { - _ = Post(e, "随机反驳是", "是你的头"); + _ = Bot.SendFriendMessage(e.user_id, "随机反驳是", "是你的头"); } } // OSM指令 if (e.detail.Length >= 4 && e.detail[..4] == ".osm") { - if (GeneralSettings.IsRun && e.detail.Contains(".osm info")) - { - // OSM核心状态 - string msg = "OSM插件运行状态:" + "\r\n本群已启用OSM核心"; - if (GeneralSettings.IsRepeat) - { - msg += "\r\n随机复读:开启"; - msg += $"\r\n随机复读概率:{GeneralSettings.PRepeat}%" + - $"\r\n随机复读延迟区间:{GeneralSettings.RepeatDelay[0]}至{GeneralSettings.RepeatDelay[1]}秒"; - } - else msg += "\r\n随机复读:关闭"; - if (GeneralSettings.IsOSM) - { - msg += "\r\n随机OSM:开启"; - msg += $"\r\n随机OSM概率:{GeneralSettings.POSM}%"; - } - else msg += "\r\n随机OSM:关闭"; - if (GeneralSettings.IsSayNo) - { - msg += "\r\n随机反驳不:开启"; - msg += $"\r\n随机反驳不概率:{GeneralSettings.PSayNo}%"; - } - else msg += "\r\n随机反驳不:关闭"; - if (GeneralSettings.IsMute) - { - msg += "\r\n禁言抽奖:开启"; - msg += $"\r\n禁言抽奖时长区间:{GeneralSettings.MuteTime[0]}至{GeneralSettings.MuteTime[1]}秒"; - } - else msg += "\r\n禁言抽奖:关闭"; - _ = Post(e, "OSM状态", msg); - } - else if (GeneralSettings.IsRun && e.detail.Contains(".osm stop")) - { - if (e.user_id == GeneralSettings.Master) - { - string result = MasterCommand.Execute(".osm stop", ""); - _ = Post(e, "OSM指令", result); - } - else _ = Post(e, "OSM指令", "你没有权限使用此指令。"); - } - else if (!GeneralSettings.IsRun && e.detail.Contains(".osm start")) - { - if (e.user_id == GeneralSettings.Master) - { - string result = MasterCommand.Execute(".osm start", ""); - _ = Post(e, "OSM指令", result); - } - else _ = Post(e, "OSM指令", "你没有权限使用此指令。"); - } - else if (e.detail.Contains(".osm refresh")) - { - if (e.user_id == GeneralSettings.Master) - { - TaskUtility.NewTask(async () => - { - await Bot.GetGroups(); - await Bot.GetGroupMembers(); - await Post(e, "OSM指令", "刷新缓存完成。"); - }); - } - else _ = Post(e, "OSM指令", "你没有权限使用此指令。"); - } - else if (e.detail.Contains(".osm set")) - { - if (e.user_id == GeneralSettings.Master) - { - string str = e.detail.Replace(".osm set", "").Trim(); - string[] strs = Regex.Split(str, @"\s+"); - string result = MasterCommand.Execute(".osm set", strs[0], strs.Length > 1 ? strs[1..] : []); - _ = Post(e, "OSM指令", result); - } - else _ = Post(e, "OSM指令", "你没有权限使用此指令。"); - } - else if (e.detail.Contains(".osm send")) - { - if (e.user_id == GeneralSettings.Master) - { - string str = e.detail.Replace(".osm send", "").Trim(); - string[] strs = Regex.Split(str, @"\s+"); - string result = MasterCommand.Execute(".osm send", strs[0], str.Replace(strs[0], "").Trim()); - if (long.TryParse(strs[0], out long group_id)) - { - GroupMessageContent content = new(group_id); - content.message.Add(new TextMessage(result)); - _ = Post(SupportedAPI.send_group_msg, group_id, "OSM指令", content); - } - } - else _ = Post(e, "OSM指令", "你没有权限使用此指令。"); - } - else if (e.detail.Contains(".osm sendall")) - { - if (e.user_id == GeneralSettings.Master) - { - string str = e.detail.Replace(".osm send", "").Trim(); - string[] strs = Regex.Split(str, @"\s+"); - string result = MasterCommand.Execute(".osm sendall", strs[0]); - foreach (long group_id in Bot.Groups.Select(g => g.group_id)) - { - GroupMessageContent content = new(group_id); - content.message.Add(new TextMessage(result)); - _ = Post(SupportedAPI.send_group_msg, group_id, "OSM指令", content); - } - } - else _ = Post(e, "OSM指令", "你没有权限使用此指令。"); - } - else - { - // OSM核心信息 - FriendMessageContent content = new(e.user_id); - content.message.Add(new TextMessage($"OSM Core {OSMCore.version} {OSMCore.version2}\r\nAuthor: Milimoe\r\nBuilt on {OSMCore.time}\r\nSee: https://github.com/milimoe")); - content.message.Add(new ImageMessage("file:///" + AppDomain.CurrentDomain.BaseDirectory.ToString() + @"img\raincandy.jpg")); - _ = Post(e, "OSM核心", content); - return; - } + MasterCommand.Execute(e.detail, e.user_id, false, e.user_id, false); return; } @@ -170,20 +53,20 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(false, e.user_id, e.user_id)) return; string msg = ""; foreach (long group_id in Bot.Groups.Select(g => g.group_id)) { if (Bot.BotIsAdmin(group_id) && MuteRecall.Muted[group_id].TryGetValue(e.user_id, out long operator_id) && operator_id == Bot.BotQQ) { MuteRecall.Muted[group_id].Remove(e.user_id); - await GroupMessageTask.Post(SupportedAPI.set_group_ban, group_id, "忏悔", new SetGroupBanContent(group_id, e.user_id, 0)); + await Bot.SendMessage(SupportedAPI.set_group_ban, group_id, "忏悔", new SetGroupBanContent(group_id, e.user_id, 0), true); if (msg != "") msg += "\r\n"; msg += $"[{group_id}] 忏悔成功!!希望你保持纯真,保持野性的美。"; } } if (msg == "") msg = "你无需忏悔。请注意:我不能帮你解除由管理员手动操作的禁言。"; - await Post(e, "忏悔", msg); + await Bot.SendFriendMessage(e.user_id, "忏悔", msg); }); return; } @@ -195,116 +78,5 @@ namespace Milimoe.RainBOT.ListeningTask Console.ForegroundColor = ConsoleColor.Gray; } } - - public static async Task CheckBlackList(FriendMessageEvent e) - { - // 黑名单 - if (e.user_id == GeneralSettings.Master) return true; - if (!BlackList.Times.ContainsKey(e.user_id)) - { - BlackList.Times.Add(e.user_id, 1); - return true; - } - else if (BlackList.Times.TryGetValue(e.user_id, out long bltimes) && bltimes > 5) - { - return false; - } - else if (++bltimes == 5) - { - BlackList.Times[e.user_id] = 6; - FriendMessageContent content = new(e.user_id); - content.message.Add(new AtMessage(e.user_id)); - content.message.Add(new TextMessage("警告:你已因短时间内频繁操作被禁止使用BOT指令" + (GeneralSettings.BlackFrozenTime / 60) + "分钟" + (GeneralSettings.BlackFrozenTime % 60) + "秒。")); - _ = Task.Run(async () => - { - await Task.Delay(1000 * GeneralSettings.BlackFrozenTime); - BlackList.Times.Remove(e.user_id); - }); - await Post(e, "黑名单", content); - return false; - } - else - { - BlackList.Times[e.user_id] = bltimes; - return true; - } - } - - public static void ColorfulCheckPass(Sender sender, string function, long dice, long probability, int delay = 0) - { - Console.ForegroundColor = ConsoleColor.Green; - if (delay > 0) - { - Console.Write($"{DateTime.Now:yyyy/MM/dd HH:mm:ss} "); - } - Console.Write($"{sender.user_id}({(sender.card != "" ? sender.card : sender.nickname)})的{function}检定通过:{dice} < {probability}"); - if (delay > 0) - { - Console.Write(" -> " + delay + "秒后执行"); - } - Console.WriteLine(); - Console.ForegroundColor = ConsoleColor.Gray; - } - - public static async Task Post(FriendMessageEvent e, string function, string text, int delay = 0) - { - string result = (await e.SendMessage(text, delay)).ReasonPhrase ?? ""; - Console.Write($"{DateTime.Now:yyyy/MM/dd HH:mm:ss} F/"); - Console.ForegroundColor = ConsoleColor.Yellow; - Console.Write(function); - Console.ForegroundColor = ConsoleColor.Gray; - Console.WriteLine($" P/{e.user_id} <- {text} {result}"); - } - - public static async Task Post(FriendMessageEvent e, string function, FriendMessageContent content, int delay = 0) - { - string result = (await e.SendMessage(content, delay)).ReasonPhrase ?? ""; - Console.Write($"{DateTime.Now:yyyy/MM/dd HH:mm:ss} F/"); - Console.ForegroundColor = ConsoleColor.Yellow; - Console.Write(function); - Console.ForegroundColor = ConsoleColor.Gray; - if (!GeneralSettings.IsDebug) - { - Console.WriteLine($" P/{e.user_id} <- {content.detail} {result}"); - } - else - { - Console.WriteLine($" P/{e.user_id} <- {JsonTools.GetString(content)} {result}"); - } - } - - public static async Task Post(string api, long user_id, string function, IContent content) - { - string result = (await HTTPPost.Post(api, content)).ReasonPhrase ?? ""; - Console.Write($"{DateTime.Now:yyyy/MM/dd HH:mm:ss} F/"); - Console.ForegroundColor = ConsoleColor.Yellow; - Console.Write(function); - Console.ForegroundColor = ConsoleColor.Gray; - if (!GeneralSettings.IsDebug) - { - Console.WriteLine($" P/{user_id} <- {content.detail} {result}"); - } - else - { - Console.WriteLine($" P/{user_id} <- {HTTPHelper.GetJsonString(api, content)} {result}"); - } - } - - public static async Task Post(string api, long user_id, string function, IEnumerable contents) - { - await HTTPPost.Post(api, contents); - Console.Write($"{DateTime.Now:yyyy/MM/dd HH:mm:ss} F/"); - Console.ForegroundColor = ConsoleColor.Yellow; - Console.Write(function); - Console.ForegroundColor = ConsoleColor.Gray; - if (!GeneralSettings.IsDebug) - { - Console.WriteLine($" P/{user_id} <- 已在后台执行"); - } - else - { - Console.WriteLine($" P/{user_id} <- 已在后台执行"); - } - } } } \ No newline at end of file diff --git a/src/ListeningTask/GroupBanTask.cs b/src/ListeningTask/GroupBanTask.cs index 41364a5..13ad829 100644 --- a/src/ListeningTask/GroupBanTask.cs +++ b/src/ListeningTask/GroupBanTask.cs @@ -1,5 +1,4 @@ -using System.Reflection; -using Milimoe.OneBot.Framework; +using Milimoe.OneBot.Framework; using Milimoe.OneBot.Model.Content; using Milimoe.OneBot.Model.Event; using Milimoe.OneBot.Model.Other; @@ -27,7 +26,7 @@ namespace Milimoe.RainBOT.ListeningTask MuteRecall.Muted[e.group_id].Add(e.user_id, GeneralSettings.Master); SetGroupBanContent content_unmute_master = new(e.group_id, GeneralSettings.Master, 0); SetGroupBanContent content_mute_operator = new(e.group_id, e.operator_id, 60); - await GroupMessageTask.Post(SupportedAPI.set_group_ban, e.group_id, "反制禁言", [content_unmute_master, content_mute_operator]); + await Bot.SendMessage(SupportedAPI.set_group_ban, e.group_id, "反制禁言", [content_unmute_master, content_mute_operator], true); Member sender = Bot.GetMember(e.group_id, e.operator_id); await e.SendMessage($"检测到主人被{sender.user_id}({(sender.card != "" ? sender.card : sender.nickname)})禁言!"); } diff --git a/src/ListeningTask/GroupMessageTask.cs b/src/ListeningTask/GroupMessageTask.cs index a3169f1..1bb1331 100644 --- a/src/ListeningTask/GroupMessageTask.cs +++ b/src/ListeningTask/GroupMessageTask.cs @@ -1,14 +1,10 @@ -using System.Linq; -using System.Text.RegularExpressions; -using Milimoe.OneBot.Framework; -using Milimoe.OneBot.Framework.Interface; +using Milimoe.OneBot.Framework; using Milimoe.OneBot.Framework.Utility; using Milimoe.OneBot.Model.Content; using Milimoe.OneBot.Model.Event; using Milimoe.OneBot.Model.Message; using Milimoe.OneBot.Model.Other; using Milimoe.OneBot.Model.QuickReply; -using Milimoe.OneBot.Utility; using Milimoe.RainBOT.Command; using Milimoe.RainBOT.Settings; @@ -42,189 +38,25 @@ namespace Milimoe.RainBOT.ListeningTask // OSM指令 if (e.detail.Length >= 4 && e.detail[..4] == ".osm") { - if (GeneralSettings.IsRun && e.detail.Contains(".osm info")) - { - // OSM核心状态 - string msg = "OSM插件运行状态:"; - if (onOSMCore) - { - msg += "\r\n本群已启用OSM核心"; - if (GeneralSettings.IsRepeat) - { - msg += "\r\n随机复读:开启"; - msg += $"\r\n随机复读概率:{GeneralSettings.PRepeat}%" + - $"\r\n随机复读延迟区间:{GeneralSettings.RepeatDelay[0]}至{GeneralSettings.RepeatDelay[1]}秒"; - } - else msg += "\r\n随机复读:关闭"; - if (GeneralSettings.IsOSM) - { - msg += "\r\n随机OSM:开启"; - msg += $"\r\n随机OSM概率:{GeneralSettings.POSM}%"; - } - else msg += "\r\n随机OSM:关闭"; - if (GeneralSettings.IsSayNo) - { - msg += "\r\n随机反驳不:开启"; - msg += $"\r\n随机反驳不概率:{GeneralSettings.PSayNo}%"; - } - else msg += "\r\n随机反驳不:关闭"; - if (GeneralSettings.IsMute) - { - msg += "\r\n禁言抽奖:开启"; - msg += $"\r\n禁言抽奖时长区间:{GeneralSettings.MuteTime[0]}至{GeneralSettings.MuteTime[1]}秒"; - } - else msg += "\r\n禁言抽奖:关闭"; - } - else - { - msg += "\r\n本群未启用OSM核心"; - } - _ = Post(e, "OSM状态", msg); - } - else if (GeneralSettings.IsRun && e.detail.Contains(".osm stop")) - { - if (e.user_id == GeneralSettings.Master) - { - string result = MasterCommand.Execute(".osm stop", ""); - _ = Post(e, "OSM指令", result); - } - else _ = Post(e, "OSM指令", "你没有权限使用此指令。"); - } - else if (!GeneralSettings.IsRun && e.detail.Contains(".osm start")) - { - if (e.user_id == GeneralSettings.Master) - { - string result = MasterCommand.Execute(".osm start", ""); - _ = Post(e, "OSM指令", result); - } - else _ = Post(e, "OSM指令", "你没有权限使用此指令。"); - } - else if (e.detail.Contains(".osm set admin") || e.detail.Contains(".osm set unadmin")) - { - if (e.user_id == GeneralSettings.Master) - { - bool enable = !e.detail.Contains("unadmin"); - string str = e.detail.Replace(".osm set admin", "").Replace(".osm set unadmin", "").Trim(); - string[] strs = Regex.Split(str, @"\s+"); - if (strs.Length > 0) - { - str = strs[0].Replace(@"@", "").Trim(); - if (long.TryParse(str, out long qq)) - { - SetGroupAdminContent content = new(e.group_id, qq, enable); - _ = Post(SupportedAPI.set_group_admin, e.group_id, "OSM指令", content); - } - else _ = Post(e, "OSM指令", MasterCommand.Execute(".osm set", "admin", strs.Length > 1 ? strs[1..] : [])); - } - else _ = Post(e, "OSM指令", MasterCommand.Execute(".osm set", "admin", strs.Length > 1 ? strs[1..] : [])); - } - else _ = Post(e, "OSM指令", "你没有权限使用此指令。"); - } - else if (e.detail.Contains(".osm mutegroup")) - { - TaskUtility.NewTask(async () => await MuteGroup(e)); - } - else if (e.detail.Contains(".osm mute")) - { - TaskUtility.NewTask(async () => await Mute(e)); - } - else if (e.detail.Contains(".osm refresh")) - { - if (e.user_id == GeneralSettings.Master) - { - TaskUtility.NewTask(async () => - { - await Bot.GetGroups(); - await Bot.GetGroupMembers(); - await Post(e, "OSM指令", "刷新缓存完成。请注意,刷新缓存会导致正在禁言中的成员无法通过私聊忏悔命令解禁。"); - }); - } - else _ = Post(e, "OSM指令", "你没有权限使用此指令。"); - } - else if (e.detail.Contains(".osm reload")) - { - if (e.user_id == GeneralSettings.Master) - { - GeneralSettings.LoadSetting(); - _ = Post(e, "OSM指令", "参数设定以及权限组重新加载完成。"); - } - else _ = Post(e, "OSM指令", "你没有权限使用此指令。"); - } - else if (e.detail.Contains(".osm set")) - { - if (e.user_id == GeneralSettings.Master) - { - string str = e.detail.Replace(".osm set", "").Trim(); - string[] strs = Regex.Split(str, @"\s+"); - string result = MasterCommand.Execute(".osm set", strs[0], strs.Length > 1 ? strs[1..] : []); - _ = Post(e, "OSM指令", result); - } - else _ = Post(e, "OSM指令", "你没有权限使用此指令。"); - } - else if (e.detail.Contains(".osm sendall")) - { - TaskUtility.NewTask(async () => - { - if (e.user_id == GeneralSettings.Master) - { - string str = e.detail.Replace(".osm sendall", "").Trim(); - string result = MasterCommand.Execute(".osm sendall", str); - foreach (long group_id in Bot.Groups.Select(g => g.group_id)) - { - GroupMessageContent content = new(group_id); - content.message.Add(new TextMessage(result)); - await Post(SupportedAPI.send_group_msg, group_id, "OSM指令", content); - } - } - else await Post(e, "OSM指令", "你没有权限使用此指令。"); - }); - } - else if (e.detail.Contains(".osm send")) - { - TaskUtility.NewTask(async () => - { - if (e.user_id == GeneralSettings.Master) - { - string str = e.detail.Replace(".osm send", "").Trim(); - string[] strs = Regex.Split(str, @"\s+"); - string result = MasterCommand.Execute(".osm send", strs[0], str.Replace(strs[0], "").Trim()); - if (long.TryParse(strs[0], out long group_id)) - { - GroupMessageContent content = new(group_id); - content.message.Add(new TextMessage(result)); - await Post(SupportedAPI.send_group_msg, group_id, "OSM指令", content); - } - } - else await Post(e, "OSM指令", "你没有权限使用此指令。"); - }); - } - else - { - // OSM核心信息 - GroupMessageContent content = new(e.group_id); - content.message.Add(new TextMessage(OSMCore.Info)); - content.message.Add(new ImageMessage("file:///" + AppDomain.CurrentDomain.BaseDirectory.ToString() + @"img\raincandy.jpg")); - _ = Post(e, "OSM核心", content); - return; - } + MasterCommand.Execute(e.detail, e.user_id, onOSMCore, e.group_id, true); return; } - if (e.detail.Length >= 5 && (e.detail[..5] == "禁言所有人" || e.detail[..5] == "解禁所有人") && e.user_id == GeneralSettings.Master && Bot.GroupMembers.TryGetValue(e.group_id, out List? members) && members != null) + if (e.detail.Length >= 5 && (e.detail[..5] == "禁言所有人" || e.detail[..5] == "解禁所有人") && (e.user_id == GeneralSettings.Master || GeneralSettings.UnMuteAccessGroup.Union(GeneralSettings.MuteAccessGroup).Contains(e.user_id)) && Bot.GroupMembers.TryGetValue(e.group_id, out List? members) && members != null) { - TaskUtility.NewTask(async () => await Mute(e, members.Where(m => m.user_id != GeneralSettings.Master).Select(m => m.user_id))); + TaskUtility.NewTask(async () => await Bot.Mute(e.user_id, e.group_id, e.detail, members.Where(m => m.user_id != GeneralSettings.Master).Select(m => m.user_id))); return; } if (e.detail != "禁言抽奖" && e.detail.Length >= 2 && MuteCommands.Any(e.detail[..2].Contains)) { - TaskUtility.NewTask(async () => await Mute(e)); + TaskUtility.NewTask(async () => await Bot.Mute(e.user_id, e.group_id, e.detail)); return; } if (e.detail.Length >= 4 && e.detail[..4] == "跨群禁言") { - TaskUtility.NewTask(async () => await MuteGroup(e)); + TaskUtility.NewTask(async () => await Bot.MuteGroup(e.user_id, e.group_id, e.detail)); return; } @@ -234,8 +66,8 @@ namespace Milimoe.RainBOT.ListeningTask ReplyMessage reply = (ReplyMessage)e.message.Where(m => m.type == "reply").First(); if (int.TryParse(reply.data.id, out int id)) { - TaskUtility.NewTask(async () => await Post(SupportedAPI.delete_msg, e.group_id, "撤回", new DeleteMsgContent(id))); - TaskUtility.NewTask(async () => await Post(SupportedAPI.delete_msg, e.group_id, "撤回", new DeleteMsgContent(e.real_id))); + TaskUtility.NewTask(async () => await Bot.SendMessage(SupportedAPI.delete_msg, e.group_id, "撤回", new DeleteMsgContent(id), true)); + TaskUtility.NewTask(async () => await Bot.SendMessage(SupportedAPI.delete_msg, e.group_id, "撤回", new DeleteMsgContent(e.real_id), true)); return; } } @@ -250,10 +82,10 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new ImageMessage("https://iw233.cn/api.php?sort=random")); - await Post(e, "Image", content); + await Bot.SendGroupMessage(e.group_id, "Image", content); return; }); } @@ -261,10 +93,10 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new ImageMessage("https://iw233.cn/api.php?sort=yin")); - await Post(e, "Image", content); + await Bot.SendGroupMessage(e.group_id, "Image", content); return; }); } @@ -272,10 +104,10 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new ImageMessage("https://iw233.cn/api.php?sort=cat")); - await Post(e, "Image", content); + await Bot.SendGroupMessage(e.group_id, "Image", content); return; }); } @@ -283,10 +115,10 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new ImageMessage("https://iw233.cn/api.php?sort=pc")); - await Post(e, "Image", content); + await Bot.SendGroupMessage(e.group_id, "Image", content); return; }); } @@ -294,10 +126,10 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new ImageMessage("https://api.03c3.cn/api/zb")); - await Post(e, "Image", content); + await Bot.SendGroupMessage(e.group_id, "Image", content); return; }); } @@ -305,10 +137,10 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new ImageMessage("file:///" + AppDomain.CurrentDomain.BaseDirectory.ToString() + @"img\long\long (" + (new Random().Next(1540) + 1) + ").jpg")); - await Post(e, "Image", content); + await Bot.SendGroupMessage(e.group_id, "Image", content); return; }); } @@ -316,10 +148,10 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new ImageMessage("file:///" + AppDomain.CurrentDomain.BaseDirectory.ToString() + @"img\dingzhen\dz" + (new Random().Next(82) + 1) + ".jpg")); - await Post(e, "Image", content); + await Bot.SendGroupMessage(e.group_id, "Image", content); return; }); } @@ -328,7 +160,7 @@ namespace Milimoe.RainBOT.ListeningTask if (BlackList.Times.TryGetValue(e.user_id, out long bltimes) && bltimes > 5) return; GroupMessageContent content = new(e.group_id); content.message.Add(new ImageMessage("file:///" + AppDomain.CurrentDomain.BaseDirectory.ToString() + @"img\ee.png")); - _ = Post(e, "Image", content); + _ = Bot.SendGroupMessage(e.group_id, "Image", content); return; } @@ -337,10 +169,10 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new RecordMessage(Music.MusicList["ikun"])); - await Post(e, "Record", content); + await Bot.SendGroupMessage(e.group_id, "Record", content); }); return; } @@ -348,10 +180,10 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new RecordMessage(Music.MusicList["懂CSGO"])); - await Post(e, "Record", content); + await Bot.SendGroupMessage(e.group_id, "Record", content); }); return; } @@ -359,20 +191,20 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new RecordMessage(Music.MusicList["令人沮丧的游戏"])); - await Post(e, "Record", content); + await Bot.SendGroupMessage(e.group_id, "Record", content); }); } if (e.detail.Contains("man", StringComparison.CurrentCultureIgnoreCase)) { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new RecordMessage(Music.MusicList["man"])); - await Post(e, "Record", content); + await Bot.SendGroupMessage(e.group_id, "Record", content); }); return; } @@ -380,10 +212,10 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new RecordMessage(Music.MusicList["马云"])); - await Post(e, "Record", content); + await Bot.SendGroupMessage(e.group_id, "Record", content); }); return; } @@ -391,10 +223,10 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new RecordMessage(Music.MusicList["电锯"])); - await Post(e, "Record", content); + await Bot.SendGroupMessage(e.group_id, "Record", content); }); return; } @@ -402,10 +234,10 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new RecordMessage(Music.MusicList["疤王"])); - await Post(e, "Record", content); + await Bot.SendGroupMessage(e.group_id, "Record", content); }); return; } @@ -413,10 +245,21 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new RecordMessage(Music.MusicList["终极"])); - await Post(e, "Record", content); + await Bot.SendGroupMessage(e.group_id, "Record", content); + }); + return; + } + if (e.detail.Contains("高考", StringComparison.CurrentCultureIgnoreCase)) + { + TaskUtility.NewTask(async () => + { + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; + GroupMessageContent content = new(e.group_id); + content.message.Add(new RecordMessage(Music.MusicList["高考"])); + await Bot.SendGroupMessage(e.group_id, "Record", content); }); return; } @@ -424,10 +267,10 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new RecordMessage(Music.MusicList[Music.MusicList.Keys.ToArray()[new Random().Next(Music.MusicList.Count)]])); - await Post(e, "Record", content); + await Bot.SendGroupMessage(e.group_id, "Record", content); }); return; } @@ -437,14 +280,14 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; GroupMessageContent content = new(e.group_id); content.message.Add(new AtMessage(e.user_id)); if (Daily.UserDailys.TryGetValue(e.user_id, out string? value) && value != null && value.Trim() != "") { content.message.Add(new TextMessage("你已看过你的今日运势:\r\n")); content.message.Add(new TextMessage(value)); - await Post(e, "我的运势", content); + await Bot.SendGroupMessage(e.group_id, "我的运势", content); } else { @@ -452,7 +295,7 @@ namespace Milimoe.RainBOT.ListeningTask string text = Daily.DailyContent[seq]; Daily.UserDailys.Add(e.user_id, text); content.message.Add(new TextMessage("你的今日运势是:\r\n" + text)); - await Post(e, "我的运势", content); + await Bot.SendGroupMessage(e.group_id, "我的运势", content); // 配图 content = new(e.group_id); string img = "file:///" + AppDomain.CurrentDomain.BaseDirectory.ToString() + @"img\zi\"; @@ -481,7 +324,7 @@ namespace Milimoe.RainBOT.ListeningTask img += "x" + (new Random().Next(2) + 1) + ".png"; } content.message.Add(new ImageMessage(img)); - await Post(e, "我的运势配图", content); + await Bot.SendGroupMessage(e.group_id, "我的运势配图", content); } }); return; @@ -490,12 +333,12 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; Daily.UserDailys.Remove(e.user_id); GroupMessageContent content = new(e.group_id); content.message.Add(new AtMessage(e.user_id)); content.message.Add(new TextMessage("你的今日运势已重置。")); - await Post(e, "重置运势", content); + await Bot.SendGroupMessage(e.group_id, "重置运势", content); }); return; } @@ -503,7 +346,7 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id)) return; string[] strs = e.detail.Replace("查看", "").Replace("运势", "").Trim().Split(' '); foreach (string str_qq in strs) { @@ -518,11 +361,11 @@ namespace Milimoe.RainBOT.ListeningTask { GroupMessageContent content = new(e.group_id); content.message.Add(new TextMessage(qq + "(" + Bot.GetMemberNickName(e.group_id, e.user_id) + ")的今日运势是:\r\n" + daily)); - await Post(e, "查看运势", content); + await Bot.SendGroupMessage(e.group_id, "查看运势", content); } else { - await Post(e, "查看运势", "TA今天还没有抽取运势哦,快去提醒TA!"); + await Bot.SendGroupMessage(e.group_id, "查看运势", "TA今天还没有抽取运势哦,快去提醒TA!"); } } } @@ -538,21 +381,21 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e) || !Bot.BotIsAdmin(e.group_id)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id) || !Bot.BotIsAdmin(e.group_id)) return; if (e.user_id != GeneralSettings.Master) { - await Post(e, "禁言抽奖", "2秒后开奖~\r\n如需要忏悔,请在开奖后3秒内发送忏悔,开奖前发送无效。"); + await Bot.SendGroupMessage(e.group_id, "禁言抽奖", "2秒后开奖~\r\n如需要忏悔,请在开奖后3秒内发送忏悔,开奖前发送无效。"); await Task.Delay(2000); if (!MuteRecall.WillMute.ContainsKey(e.user_id)) MuteRecall.WillMute.Add(e.user_id, e.user_id); long mute_time = GeneralSettings.MuteTime[0] + new Random().NextInt64(GeneralSettings.MuteTime[1] - GeneralSettings.MuteTime[0]); - await Post(e, "禁言抽奖", "开奖啦!禁言时长:" + (mute_time / 60) + "分钟" + (mute_time % 60) + "秒。\r\n" + "你现在有3秒时间发送忏悔拒绝领奖!"); + await Bot.SendGroupMessage(e.group_id, "禁言抽奖", "开奖啦!禁言时长:" + (mute_time / 60) + "分钟" + (mute_time % 60) + "秒。\r\n" + "你现在有3秒时间发送忏悔拒绝领奖!"); await Task.Delay(3200); - await Post(SupportedAPI.set_group_ban, e.group_id, "禁言抽奖", new SetGroupBanContent(e.group_id, e.user_id, mute_time)); + await Bot.SendMessage(SupportedAPI.set_group_ban, e.group_id, "禁言抽奖", new SetGroupBanContent(e.group_id, e.user_id, mute_time), true); MuteRecall.WillMute.Remove(e.user_id); } else { - _ = Post(e, "禁言抽奖", "我不能禁言主人!"); + _ = Bot.SendGroupMessage(e.group_id, "禁言抽奖", "我不能禁言主人!"); } }); return; @@ -562,11 +405,11 @@ namespace Milimoe.RainBOT.ListeningTask { TaskUtility.NewTask(async () => { - if (!await CheckBlackList(e) || !Bot.BotIsAdmin(e.group_id)) return; + if (!await Bot.CheckBlackList(true, e.user_id, e.group_id) || !Bot.BotIsAdmin(e.group_id)) return; await Task.Delay(3800); MuteRecall.WillMute.Remove(e.user_id); - await Post(SupportedAPI.set_group_ban, e.group_id, "忏悔", new SetGroupBanContent(e.group_id, e.user_id, 0)); - await Post(e, "忏悔", "忏悔成功!!希望你保持纯真,保持野性的美。"); + await Bot.SendMessage(SupportedAPI.set_group_ban, e.group_id, "忏悔", new SetGroupBanContent(e.group_id, e.user_id, 0), true); + await Bot.SendGroupMessage(e.group_id, "忏悔", "忏悔成功!!希望你保持纯真,保持野性的美。"); }); return; } @@ -576,12 +419,12 @@ namespace Milimoe.RainBOT.ListeningTask { if (e.user_id != GeneralSettings.Master && e.CheckThrow(40, out dice)) { - ColorfulCheckPass(sender, "反驳是", dice, 40); - _ = Post(e, "随机反驳是", "是你的头"); + Bot.ColorfulCheckPass(sender, "反驳是", dice, 40); + _ = Bot.SendGroupMessage(e.group_id, "随机反驳是", "是你的头"); } else if (e.user_id == GeneralSettings.Master) { - _ = Post(e, "随机反驳是", "是你的头"); + _ = Bot.SendGroupMessage(e.group_id, "随机反驳是", "是你的头"); } } @@ -602,7 +445,7 @@ namespace Milimoe.RainBOT.ListeningTask { if (pos + 1 < e.detail.Length && !SayNo.IgnoreTriggerAfterNo.Any(e.detail[(pos + 1)..].Contains)) { - ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); + Bot.ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); content.message.Add(new TextMessage(string.Format(SayNo.SayNoWords[new Random().Next(SayNo.SayNoWords.Count)], e.detail[pos + 1]))); break; } @@ -611,7 +454,7 @@ namespace Milimoe.RainBOT.ListeningTask { if (pos + 1 < e.detail.Length) { - ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); + Bot.ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); if (e.detail[pos + 1] == '有') { content.message.Add(new TextMessage(string.Format(SayNo.SayDontHaveWords[new Random().Next(SayNo.SayDontHaveWords.Count - 3)], e.detail[pos + 1]))); @@ -628,7 +471,7 @@ namespace Milimoe.RainBOT.ListeningTask { if (pos + 1 < e.detail.Length && SayNo.TriggerAfterYes.Any(e.detail[(pos + 1)..].Contains)) { - ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); + Bot.ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); content.message.Add(new TextMessage(SayNo.SayNotYesWords[new Random().Next(SayNo.SayNotYesWords.Count)])); break; } @@ -637,7 +480,7 @@ namespace Milimoe.RainBOT.ListeningTask { if (pos + 1 < e.detail.Length && !SayNo.IgnoreTriggerAfterNo.Any(e.detail[(pos + 1)..].Contains) && !SayNo.WillNotSayNo.Any(e.detail[(pos + 1)..].Contains)) { - ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); + Bot.ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); content.message.Add(new TextMessage(string.Format(SayNo.SayDontWords[new Random().Next(SayNo.SayDontWords.Count)], e.detail[pos + 1]))); break; } @@ -645,7 +488,7 @@ namespace Milimoe.RainBOT.ListeningTask } if (content.message.Count > 0) { - _ = Post(e, "随机反驳不", content); + _ = Bot.SendGroupMessage(e.group_id, "随机反驳不", content); } } else if (SayNo.TriggerBeforeNo.Any(e.detail.Contains) && GeneralSettings.IsSayNo && e.CheckThrow(GeneralSettings.PSayNo, out dice)) @@ -664,7 +507,7 @@ namespace Milimoe.RainBOT.ListeningTask { if (pos + keyword.Length + 1 < e.detail.Length) { - ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); + Bot.ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); if (e.detail[(pos + keyword.Length + 1)..].Contains('了')) { sayword = e.detail[pos..].Replace(keyword, ""); @@ -683,7 +526,7 @@ namespace Milimoe.RainBOT.ListeningTask { if (pos + keyword.Length + 1 < e.detail.Length) { - ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); + Bot.ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); sayword = e.detail[pos..].Replace(keyword, ""); if (sayword.Length > 2) sayword = sayword[..2]; List NewSayWords = []; @@ -698,43 +541,43 @@ namespace Milimoe.RainBOT.ListeningTask } if (content.message.Count > 0) { - _ = Post(e, "随机反驳不", content); + _ = Bot.SendGroupMessage(e.group_id, "随机反驳不", content); } } else if (e.detail.Contains("可以") && !e.detail.Contains('不') && e.CheckThrow(GeneralSettings.PSayNo, out dice)) { - ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); + Bot.ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); if (dice < (GeneralSettings.PSayNo / 2)) { - _ = Post(e, "随机反驳不", "可以"); + _ = Bot.SendGroupMessage(e.group_id, "随机反驳不", "可以"); } else { - _ = Post(e, "随机反驳不", "不可以"); + _ = Bot.SendGroupMessage(e.group_id, "随机反驳不", "不可以"); } } else if (e.detail.Contains('能') && !e.detail.Contains('不') && !e.detail.Contains('技') && !e.detail.Contains('可') && e.CheckThrow(GeneralSettings.PSayNo, out dice)) { - ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); + Bot.ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); if (dice < (GeneralSettings.PSayNo / 2)) { - _ = Post(e, "随机反驳不", "能"); + _ = Bot.SendGroupMessage(e.group_id, "随机反驳不", "能"); } else { - _ = Post(e, "随机反驳不", "不能"); + _ = Bot.SendGroupMessage(e.group_id, "随机反驳不", "不能"); } } else if (e.detail.Contains("可能") && !e.detail.Contains('不') && e.CheckThrow(GeneralSettings.PSayNo, out dice)) { - ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); + Bot.ColorfulCheckPass(sender, "随机反驳不", dice, GeneralSettings.PSayNo); if (dice < (GeneralSettings.PSayNo / 2)) { - _ = Post(e, "随机反驳不", "可能"); + _ = Bot.SendGroupMessage(e.group_id, "随机反驳不", "可能"); } else { - _ = Post(e, "随机反驳不", "不可能"); + _ = Bot.SendGroupMessage(e.group_id, "随机反驳不", "不可能"); } } @@ -744,13 +587,13 @@ namespace Milimoe.RainBOT.ListeningTask { if (GeneralSettings.IsReverseAt && e.CheckThrow(GeneralSettings.PReverseAt, out dice)) { - ColorfulCheckPass(sender, "反向艾特", dice, GeneralSettings.PReverseAt); + Bot.ColorfulCheckPass(sender, "反向艾特", dice, GeneralSettings.PReverseAt); foreach (AtMessage at in temp_at) { at.data.qq = e.user_id.ToString(); GroupMessageContent content = new(e.group_id); content.message.AddRange(e.message); - _ = Post(e, "反向艾特", content); + _ = Bot.SendGroupMessage(e.group_id, "反向艾特", content); } } return; @@ -759,7 +602,7 @@ namespace Milimoe.RainBOT.ListeningTask // 随机OSM if (GeneralSettings.IsOSM && !Ignore.RepeatIgnore.Contains(e.detail) && e.CheckThrow(GeneralSettings.POSM, out dice)) { - ColorfulCheckPass(sender, "随机OSM", dice, GeneralSettings.POSM); + Bot.ColorfulCheckPass(sender, "随机OSM", dice, GeneralSettings.POSM); GroupMessageContent content = new(e.group_id); string img = new Random().Next(3) switch { @@ -768,7 +611,7 @@ namespace Milimoe.RainBOT.ListeningTask _ => "file:///" + AppDomain.CurrentDomain.BaseDirectory.ToString() + @"img\osm.jpg", }; content.message.Add(new ImageMessage(img)); - _ = Post(e, "Image", content); + _ = Bot.SendGroupMessage(e.group_id, "Image", content); return; } @@ -776,10 +619,10 @@ namespace Milimoe.RainBOT.ListeningTask if (GeneralSettings.IsRepeat && !Ignore.RepeatIgnore.Contains(e.detail) && e.CheckThrow(GeneralSettings.PRepeat, out dice)) { int delay = GeneralSettings.RepeatDelay[0] + new Random().Next(GeneralSettings.RepeatDelay[1] - GeneralSettings.RepeatDelay[0]); - ColorfulCheckPass(sender, "随机复读", dice, GeneralSettings.PRepeat, delay); + Bot.ColorfulCheckPass(sender, "随机复读", dice, GeneralSettings.PRepeat, delay); GroupMessageContent content = new(e.group_id); content.message.AddRange(e.message); - _ = Post(e, "随机复读", content, delay * 1000); + _ = Bot.SendGroupMessage(e.group_id, "随机复读", content, delay * 1000); return; } @@ -787,13 +630,13 @@ namespace Milimoe.RainBOT.ListeningTask if (GeneralSettings.IsCallBrother && e.CheckThrow(GeneralSettings.PCallBrother, out dice)) { int delay = GeneralSettings.RepeatDelay[0] + new Random().Next(GeneralSettings.RepeatDelay[1]); - ColorfulCheckPass(sender, "随机叫哥", dice, GeneralSettings.PCallBrother, delay); + Bot.ColorfulCheckPass(sender, "随机叫哥", dice, GeneralSettings.PCallBrother, delay); string name = (sender.card != "" ? sender.card : sender.nickname).Trim(); int pos = new Random().Next(name.Length - 1); GroupMessageContent content = new(e.group_id); content.message.Add(new AtMessage(e.user_id)); content.message.Add(new TextMessage(string.Concat(name.AsSpan(pos, 2), "哥"))); - _ = Post(e, "随机叫哥", content, delay * 1000); + _ = Bot.SendGroupMessage(e.group_id, "随机叫哥", content, delay * 1000); return; } } @@ -804,217 +647,5 @@ namespace Milimoe.RainBOT.ListeningTask Console.ForegroundColor = ConsoleColor.Gray; } } - - public static async Task CheckBlackList(GroupMessageEvent e) - { - // 黑名单 - if (e.user_id == GeneralSettings.Master) return true; - if (!BlackList.Times.ContainsKey(e.user_id)) - { - BlackList.Times.Add(e.user_id, 1); - return true; - } - else if (BlackList.Times.TryGetValue(e.user_id, out long bltimes) && bltimes > 5) - { - return false; - } - else if (++bltimes == 5) - { - BlackList.Times[e.user_id] = 6; - GroupMessageContent content = new(e.group_id); - content.message.Add(new AtMessage(e.user_id)); - content.message.Add(new TextMessage("警告:你已因短时间内频繁操作被禁止使用BOT指令" + (GeneralSettings.BlackFrozenTime / 60) + "分钟" + (GeneralSettings.BlackFrozenTime % 60) + "秒。")); - _ = Task.Run(async () => - { - await Task.Delay(1000 * GeneralSettings.BlackFrozenTime); - BlackList.Times.Remove(e.user_id); - }); - await Post(e, "黑名单", content); - return false; - } - else - { - BlackList.Times[e.user_id] = bltimes; - return true; - } - } - - public static async Task Mute(GroupMessageEvent e) - { - if (!Bot.BotIsAdmin(e.group_id)) return; - bool unmute = e.detail.Contains("解禁"); - string[] strs = Regex.Split(e.detail, @"\s+"); - if (!unmute && strs.Length < 2) return; - if (e.user_id == GeneralSettings.Master || (unmute && GeneralSettings.UnMuteAccessGroup.Contains(e.user_id)) || (!unmute && GeneralSettings.MuteAccessGroup.Contains(e.user_id))) - { - strs = Regex.Split(e.detail.Replace(".osm mute", "").Replace("禁言", "").Replace("解禁", "").Replace("所有人", "").Trim(), @"\s+"); - long time = 0; - if ((!unmute && strs.Length > 1 && long.TryParse(strs[^1], out time) && time >= 0 && time < 2592000) || unmute) - { - List qqlist = []; - List list = []; - foreach (string str in unmute ? strs : strs[..^1]) - { - if (long.TryParse(str.Replace(@"@", "").Trim(), out long qq)) - { - SetGroupBanContent content = new(e.group_id, qq, time); - list.Add(content); - qqlist.Add(qq); - } - } - await Post(SupportedAPI.set_group_ban, e.group_id, "禁言指令", list); - if (time > 0) - { - await Task.Delay(3000); - foreach (long qq in qqlist) - { - if (MuteRecall.Muted[e.group_id].ContainsKey(qq)) MuteRecall.Muted[e.group_id][qq] = GeneralSettings.Master; - else MuteRecall.Muted[e.group_id].Add(qq, GeneralSettings.Master); - } - } - return; - } - await Post(e, "OSM指令", MasterCommand.Execute(".osm mute", "", strs.Length > 1 ? strs[1..] : [])); - } - else await Post(e, "OSM指令", "你没有权限使用此指令。"); - } - - public static async Task Mute(GroupMessageEvent e, IEnumerable qqlist) - { - if (!Bot.BotIsAdmin(e.group_id)) return; - bool unmute = e.detail.Contains("解禁"); - if (e.user_id == GeneralSettings.Master || (unmute && GeneralSettings.UnMuteAccessGroup.Contains(e.user_id)) || (!unmute && GeneralSettings.MuteAccessGroup.Contains(e.user_id))) - { - string[] strs = Regex.Split(e.detail.Replace("禁言", "").Replace("解禁", "").Replace("所有人", "").Trim(), @"\s+"); - long mute_time = unmute ? 0 : GeneralSettings.MuteTime[0] + new Random().NextInt64(GeneralSettings.MuteTime[1] - GeneralSettings.MuteTime[0]); - if (long.TryParse(strs[^1], out long time) && time >= 0 && time < 2592000) - { - mute_time = time; - } - List list = []; - foreach (long qq in qqlist) - { - SetGroupBanContent content = new(e.group_id, qq, mute_time); - list.Add(content); - } - await Post(SupportedAPI.set_group_ban, e.group_id, "批量禁言指令", list); - if (mute_time > 0) - { - await Task.Delay(3000); - foreach (long qq in qqlist) - { - if (MuteRecall.Muted[e.group_id].ContainsKey(qq)) MuteRecall.Muted[e.group_id][qq] = GeneralSettings.Master; - else MuteRecall.Muted[e.group_id].Add(qq, GeneralSettings.Master); - } - } - } - else await Post(e, "OSM指令", "你没有权限使用此指令。"); - } - - public static async Task MuteGroup(GroupMessageEvent e) - { - if (!Bot.BotIsAdmin(e.group_id)) return; - if (e.user_id == GeneralSettings.Master || GeneralSettings.MuteAccessGroup.Contains(e.user_id)) - { - string str = e.detail.Replace(".osm mutegroup", "").Replace("跨群禁言", "").Trim(); - string[] strs = Regex.Split(str, @"\s+"); - if (strs.Length > 2) - { - string str_group = strs[0].Replace(@"@", "").Trim(); - string str_qq = strs[1].Replace(@"@", "").Trim(); - if (long.TryParse(str_group, out long group) && long.TryParse(str_qq, out long qq) && long.TryParse(strs[2], out long time) && time >= 0 && time < 2592000) - { - SetGroupBanContent content = new(group, qq, time); - await Post(SupportedAPI.set_group_ban, group, "OSM指令", content); - if (time > 0) - { - await Task.Delay(3000); - if (MuteRecall.Muted[e.group_id].ContainsKey(qq)) MuteRecall.Muted[e.group_id][qq] = GeneralSettings.Master; - else MuteRecall.Muted[e.group_id].Add(qq, GeneralSettings.Master); - } - } - else await Post(e, "OSM指令", MasterCommand.Execute(".osm mute", "", strs.Length > 1 ? strs[1..] : [])); - } - else await Post(e, "OSM指令", MasterCommand.Execute(".osm mute", "", strs.Length > 1 ? strs[1..] : [])); - } - else await Post(e, "OSM指令", "你没有权限使用此指令。"); - } - - public static void ColorfulCheckPass(Sender sender, string function, long dice, long probability, int delay = 0) - { - Console.ForegroundColor = ConsoleColor.Green; - if (delay > 0) - { - Console.Write($"{DateTime.Now:yyyy/MM/dd HH:mm:ss} "); - } - Console.Write($"{sender.user_id}({(sender.card != "" ? sender.card : sender.nickname)})的{function}检定通过:{dice} < {probability}"); - if (delay > 0) - { - Console.Write(" -> " + delay + "秒后执行"); - } - Console.WriteLine(); - Console.ForegroundColor = ConsoleColor.Gray; - } - - public static async Task Post(GroupMessageEvent e, string function, string text, int delay = 0) - { - string result = (await e.SendMessage(text, delay)).ReasonPhrase ?? ""; - Console.Write($"{DateTime.Now:yyyy/MM/dd HH:mm:ss} F/"); - Console.ForegroundColor = ConsoleColor.Yellow; - Console.Write(function); - Console.ForegroundColor = ConsoleColor.Gray; - Console.WriteLine($" G/{e.group_id} <- {text} {result}"); - } - - public static async Task Post(GroupMessageEvent e, string function, GroupMessageContent content, int delay = 0) - { - string result = (await e.SendMessage(content, delay)).ReasonPhrase ?? ""; - Console.Write($"{DateTime.Now:yyyy/MM/dd HH:mm:ss} F/"); - Console.ForegroundColor = ConsoleColor.Yellow; - Console.Write(function); - Console.ForegroundColor = ConsoleColor.Gray; - if (!GeneralSettings.IsDebug) - { - Console.WriteLine($" G/{e.group_id} <- {content.detail} {result}"); - } - else - { - Console.WriteLine($" G/{e.group_id} <- {JsonTools.GetString(content)} {result}"); - } - } - - public static async Task Post(string api, long group_id, string function, IContent content) - { - string result = (await HTTPPost.Post(api, content)).ReasonPhrase ?? ""; - Console.Write($"{DateTime.Now:yyyy/MM/dd HH:mm:ss} F/"); - Console.ForegroundColor = ConsoleColor.Yellow; - Console.Write(function); - Console.ForegroundColor = ConsoleColor.Gray; - if (!GeneralSettings.IsDebug) - { - Console.WriteLine($" G/{group_id} <- {content.detail} {result}"); - } - else - { - Console.WriteLine($" G/{group_id} <- {HTTPHelper.GetJsonString(api, content)} {result}"); - } - } - - public static async Task Post(string api, long group_id, string function, IEnumerable contents) - { - await HTTPPost.Post(api, contents); - Console.Write($"{DateTime.Now:yyyy/MM/dd HH:mm:ss} F/"); - Console.ForegroundColor = ConsoleColor.Yellow; - Console.Write(function); - Console.ForegroundColor = ConsoleColor.Gray; - if (!GeneralSettings.IsDebug) - { - Console.WriteLine($" G/{group_id} <- 已在后台执行"); - } - else - { - Console.WriteLine($" G/{group_id} <- 已在后台执行"); - } - } } } \ No newline at end of file diff --git a/src/Main.cs b/src/Main.cs index 51e0154..5f123f2 100644 --- a/src/Main.cs +++ b/src/Main.cs @@ -151,7 +151,7 @@ try } } }); - + _ = Task.Factory.StartNew(async () => { while (true) @@ -172,19 +172,42 @@ try } } }); - - // 循环接收消息,此线程会在没有请求时阻塞 + + bool isListening = true; + CancellationTokenSource cts = new(); + CancellationToken ct = cts.Token; + + Task t = Task.Factory.StartNew(() => + { + // 循环接收消息,此线程会在没有请求时阻塞 + while (isListening) + { + try + { + listener.GetContext(); + } + catch (Exception e) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(e); + Console.ForegroundColor = ConsoleColor.Gray; + } + } + }, ct); + while (true) { - try + string order = Console.ReadLine()?.ToLower().Trim() ?? ""; + switch (order) { - listener.GetContext(); - } - catch (Exception e) - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(e); - Console.ForegroundColor = ConsoleColor.Gray; + case "debug on": + GeneralSettings.IsDebug = true; + Console.WriteLine("开启Debug模式"); + break; + case "debug off": + GeneralSettings.IsDebug = false; + Console.WriteLine("关闭Debug模式"); + break; } } } diff --git a/src/Settings/Bot.cs b/src/Settings/Bot.cs index 7e3b62c..7d97a63 100644 --- a/src/Settings/Bot.cs +++ b/src/Settings/Bot.cs @@ -1,9 +1,14 @@ -using System.Reflection; -using System.Text.Json; +using System.Text.Json; +using System.Text.RegularExpressions; using Milimoe.OneBot.Framework; +using Milimoe.OneBot.Framework.Interface; using Milimoe.OneBot.Framework.Utility; using Milimoe.OneBot.Model.Content; +using Milimoe.OneBot.Model.Message; using Milimoe.OneBot.Model.Other; +using Milimoe.OneBot.Utility; +using Milimoe.RainBOT.Command; +using Group = Milimoe.OneBot.Model.Other.Group; namespace Milimoe.RainBOT.Settings { @@ -28,7 +33,7 @@ namespace Milimoe.RainBOT.Settings public static async Task GetGroups() { - HttpResponseMessage msg = await HTTPPost.Post(SupportedAPI.get_group_list, new GroupMessageContent(0)); + HttpResponseMessage msg = await HTTPPost.Post(SupportedAPI.get_group_list, new GroupMessageContent(0)); if (msg.IsSuccessStatusCode) { JsonDocument jsonDocument = JsonDocument.Parse(await msg.Content.ReadAsStringAsync()); @@ -81,5 +86,227 @@ namespace Milimoe.RainBOT.Settings { return member.card != "" ? member.card : member.nickname; } + + public static async Task CheckBlackList(bool send_group, long user_id, long target_id) + { + // 黑名单 + if (user_id == GeneralSettings.Master) return true; + if (!BlackList.Times.ContainsKey(user_id)) + { + BlackList.Times.Add(user_id, 1); + return true; + } + else if (BlackList.Times.TryGetValue(user_id, out long bltimes) && bltimes > 5) + { + return false; + } + else if (++bltimes == 5) + { + BlackList.Times[user_id] = 6; + FriendMessageContent content = new(user_id); + content.message.Add(new AtMessage(user_id)); + content.message.Add(new TextMessage("警告:你已因短时间内频繁操作被禁止使用BOT指令" + (GeneralSettings.BlackFrozenTime / 60) + "分钟" + (GeneralSettings.BlackFrozenTime % 60) + "秒。")); + _ = Task.Run(async () => + { + await Task.Delay(1000 * GeneralSettings.BlackFrozenTime); + BlackList.Times.Remove(user_id); + }); + await (send_group ? SendFriendMessage(target_id, "黑名单", content) : SendFriendMessage(target_id, "黑名单", content)); + return false; + } + else + { + BlackList.Times[user_id] = bltimes; + return true; + } + } + + public static void ColorfulCheckPass(Sender sender, string function, long dice, long probability, int delay = 0) + { + Console.ForegroundColor = ConsoleColor.Green; + if (delay > 0) + { + Console.Write($"{DateTime.Now:yyyy/MM/dd HH:mm:ss} "); + } + Console.Write($"{sender.user_id}({(sender.card != "" ? sender.card : sender.nickname)})的{function}检定通过:{dice} < {probability}"); + if (delay > 0) + { + Console.Write(" -> " + delay + "秒后执行"); + } + Console.WriteLine(); + Console.ForegroundColor = ConsoleColor.Gray; + } + + public static async Task SendGroupMessage(long group_id, string function, string text, int delay = 0) + { + GroupMessageContent content = new(group_id); + content.message.Add(new TextMessage(text)); + if (delay > 0) + { + await Task.Delay(delay); + } + await SendMessage(SupportedAPI.send_group_msg, group_id, function, content, true); + } + + public static async Task SendGroupMessage(long group_id, string function, IContent content) => await SendMessage(SupportedAPI.send_group_msg, group_id, function, content, true); + + public static async Task SendGroupMessage(long group_id, string function, IContent content, int delay = 0) + { + if (delay > 0) + { + await Task.Delay(delay); + } + await SendMessage(SupportedAPI.send_group_msg, group_id, function, content, true); + } + + public static async Task SendGroupMessage(long group_id, string function, IEnumerable contents) => await SendMessage(SupportedAPI.send_group_msg, group_id, function, contents, true); + + public static async Task SendFriendMessage(long user_id, string function, string text) + { + FriendMessageContent content = new(user_id); + content.message.Add(new TextMessage(text)); + await SendMessage(SupportedAPI.send_private_msg, user_id, function, content, false); + } + + public static async Task SendFriendMessage(long user_id, string function, IContent content) => await SendMessage(SupportedAPI.send_private_msg, user_id, function, content, false); + + public static async Task SendFriendMessage(long user_id, string function, IEnumerable contents) => await SendMessage(SupportedAPI.send_private_msg, user_id, function, contents, false); + + public static async Task SendMessage(string api, long target_id, string function, IContent content, bool send_group) + { + string msg_type = send_group ? "G" : "P"; + string result = (await HTTPPost.Post(api, content)).ReasonPhrase ?? ""; + Console.Write($"{DateTime.Now:yyyy/MM/dd HH:mm:ss} F/"); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.Write(function); + Console.ForegroundColor = ConsoleColor.Gray; + if (!GeneralSettings.IsDebug) + { + Console.WriteLine($" {msg_type}/{target_id} <- {content.detail} {result}"); + } + else + { + Console.WriteLine($" {msg_type}/{target_id} <- {HTTPHelper.GetJsonString(api, content)} {result}"); + } + } + + public static async Task SendMessage(string api, long target_id, string function, IEnumerable contents, bool send_group) + { + string msg_type = send_group ? "G" : "P"; + await HTTPPost.Post(api, contents); + Console.Write($"{DateTime.Now:yyyy/MM/dd HH:mm:ss} F/"); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.Write(function); + Console.ForegroundColor = ConsoleColor.Gray; + if (!GeneralSettings.IsDebug) + { + Console.WriteLine($" {msg_type}/{target_id} <- 已在后台执行"); + } + else + { + Console.WriteLine($" {msg_type}/{target_id} <- 已在后台执行"); + } + } + + public static async Task Mute(long user_id, long group_id, string detail) + { + if (!BotIsAdmin(group_id)) return; + bool unmute = detail.Contains("解禁"); + string[] strs = Regex.Split(detail, @"\s+"); + if (!unmute && strs.Length < 2) return; + if (user_id == GeneralSettings.Master || (unmute && GeneralSettings.UnMuteAccessGroup.Contains(user_id)) || (!unmute && GeneralSettings.MuteAccessGroup.Contains(user_id))) + { + strs = Regex.Split(detail.Replace(".osm mute", "").Replace("禁言", "").Replace("解禁", "").Replace("所有人", "").Trim(), @"\s+"); + long time = 0; + if ((!unmute && strs.Length > 1 && long.TryParse(strs[^1], out time) && time >= 0 && time < 2592000) || unmute) + { + List qqlist = []; + List list = []; + foreach (string str in unmute ? strs : strs[..^1]) + { + if (long.TryParse(str.Replace(@"@", "").Trim(), out long qq)) + { + SetGroupBanContent content = new(group_id, qq, time); + list.Add(content); + qqlist.Add(qq); + } + } + await SendMessage(SupportedAPI.set_group_ban, group_id, "禁言指令", list, true); + if (time > 0) + { + await Task.Delay(3000); + foreach (long qq in qqlist) + { + if (MuteRecall.Muted[group_id].ContainsKey(qq)) MuteRecall.Muted[group_id][qq] = GeneralSettings.Master; + else MuteRecall.Muted[group_id].Add(qq, GeneralSettings.Master); + } + } + return; + } + await SendGroupMessage(group_id, "OSM指令", MasterCommand.Execute_Worker(".osm mute", "", strs.Length > 1 ? strs[1..] : [])); + } + else await SendGroupMessage(group_id, "OSM指令", "你没有权限使用此指令。"); + } + + public static async Task Mute(long user_id, long group_id, string detail, IEnumerable qqlist) + { + if (!BotIsAdmin(group_id)) return; + bool unmute = detail.Contains("解禁"); + if (user_id == GeneralSettings.Master || (unmute && GeneralSettings.UnMuteAccessGroup.Contains(user_id)) || (!unmute && GeneralSettings.MuteAccessGroup.Contains(user_id))) + { + string[] strs = Regex.Split(detail.Replace("禁言", "").Replace("解禁", "").Replace("所有人", "").Trim(), @"\s+"); + long mute_time = unmute ? 0 : GeneralSettings.MuteTime[0] + new Random().NextInt64(GeneralSettings.MuteTime[1] - GeneralSettings.MuteTime[0]); + if (long.TryParse(strs[^1], out long time) && time >= 0 && time < 2592000) + { + mute_time = time; + } + List list = []; + foreach (long qq in qqlist) + { + SetGroupBanContent content = new(group_id, qq, mute_time); + list.Add(content); + } + await SendMessage(SupportedAPI.set_group_ban, group_id, "批量禁言指令", list, true); + if (mute_time > 0) + { + await Task.Delay(3000); + foreach (long qq in qqlist) + { + if (MuteRecall.Muted[group_id].ContainsKey(qq)) MuteRecall.Muted[group_id][qq] = GeneralSettings.Master; + else MuteRecall.Muted[group_id].Add(qq, GeneralSettings.Master); + } + } + } + else await SendGroupMessage(group_id, "OSM指令", "你没有权限使用此指令。"); + } + + public static async Task MuteGroup(long user_id, long group_id, string detail) + { + if (!BotIsAdmin(group_id)) return; + if (user_id == GeneralSettings.Master || GeneralSettings.MuteAccessGroup.Contains(user_id)) + { + string str = detail.Replace(".osm mutegroup", "").Replace("跨群禁言", "").Trim(); + string[] strs = Regex.Split(str, @"\s+"); + if (strs.Length > 2) + { + string str_group = strs[0].Replace(@"@", "").Trim(); + string str_qq = strs[1].Replace(@"@", "").Trim(); + if (long.TryParse(str_group, out long group) && long.TryParse(str_qq, out long qq) && long.TryParse(strs[2], out long time) && time >= 0 && time < 2592000) + { + SetGroupBanContent content = new(group, qq, time); + await SendMessage(SupportedAPI.set_group_ban, group, "OSM指令", content, true); + if (time > 0) + { + await Task.Delay(3000); + if (MuteRecall.Muted[group_id].ContainsKey(qq)) MuteRecall.Muted[group_id][qq] = GeneralSettings.Master; + else MuteRecall.Muted[group_id].Add(qq, GeneralSettings.Master); + } + } + else await SendGroupMessage(group, "OSM指令", MasterCommand.Execute_Worker(".osm mute", "", strs.Length > 1 ? strs[1..] : [])); + } + else await SendGroupMessage(group_id, "OSM指令", MasterCommand.Execute_Worker(".osm mute", "", strs.Length > 1 ? strs[1..] : [])); + } + else await SendGroupMessage(group_id, "OSM指令", "你没有权限使用此指令。"); + } } } diff --git a/src/Settings/Music.cs b/src/Settings/Music.cs index d76acf8..a5f6745 100644 --- a/src/Settings/Music.cs +++ b/src/Settings/Music.cs @@ -10,8 +10,9 @@ /// man /// 马云 /// 疤王 - /// 电锯/追命 + /// 电锯 /// 终极 + /// 高考 /// public static Dictionary MusicList { get; set; } = []; @@ -25,6 +26,7 @@ MusicList.Add("疤王", "file:///" + AppDomain.CurrentDomain.BaseDirectory.ToString() + @"music\疤王.mp3"); MusicList.Add("电锯", "file:///" + AppDomain.CurrentDomain.BaseDirectory.ToString() + @"music\电锯.mp3"); MusicList.Add("终极", "file:///" + AppDomain.CurrentDomain.BaseDirectory.ToString() + @"music\终极.mp3"); + MusicList.Add("高考", "file:///" + AppDomain.CurrentDomain.BaseDirectory.ToString() + @"music\高考.mp3"); } } } diff --git a/src/Settings/OSMCore.cs b/src/Settings/OSMCore.cs index 6e0ed13..f197e15 100644 --- a/src/Settings/OSMCore.cs +++ b/src/Settings/OSMCore.cs @@ -3,8 +3,8 @@ public class OSMCore { public const string version = "v1.0"; - public const string version2 = "Release"; - public const string time = "Mar. 21st, 2024"; + public const string version2 = "Patch1"; + public const string time = "Mar. 22nd, 2024"; public static string Info => $"OSM Core {version} {version2}\r\nAuthor: Milimoe\r\nBuilt on {time}\r\nSee: https://github.com/milimoe"; }