diff --git a/asg_form/Program.cs b/asg_form/Program.cs index 23b551b..062a7f7 100644 --- a/asg_form/Program.cs +++ b/asg_form/Program.cs @@ -16,7 +16,9 @@ using Mirai.Net.Sessions.Http.Managers; using System; using System.Data; using System.Drawing.Drawing2D; +using System.Reflection; using System.Text; +using Zack.EventBus; @@ -78,6 +80,9 @@ options.AddDefaultPolicy(builder => builder.WithOrigins(urls) IServiceCollection services = builder.Services; +var eventBusSec = builder.Configuration.GetSection("EventBus"); +builder.Services.Configure(eventBusSec); +builder.Services.AddEventBus("qqbot_server", Assembly.GetExecutingAssembly()); services.AddDbContext(opt => { diff --git a/asg_form/appsettings.json b/asg_form/appsettings.json index 752775e..23e660c 100644 --- a/asg_form/appsettings.json +++ b/asg_form/appsettings.json @@ -5,6 +5,10 @@ "Microsoft.AspNetCore": "Warning" } }, + "EventBus": { + "HostName": "127.0.0.1", + "ExchangeName": "qqbot" + }, "AllowedHosts": "*", "JWT": { "SigningKey": "JWT加密密钥(请严格保密)", diff --git a/asg_form/asg_form.csproj b/asg_form/asg_form.csproj index 8acaf42..01b4da4 100644 --- a/asg_form/asg_form.csproj +++ b/asg_form/asg_form.csproj @@ -64,6 +64,7 @@ + diff --git a/asg_form/qqbot.cs b/asg_form/qqbot.cs index 76b7f0b..8b8118a 100644 --- a/asg_form/qqbot.cs +++ b/asg_form/qqbot.cs @@ -1,4 +1,4 @@ -/*using asg_form.Controllers; +using asg_form.Controllers; using Mirai.Net.Data.Messages.Concretes; using Mirai.Net.Data.Messages; using Mirai.Net.Data.Messages.Receivers; @@ -20,25 +20,46 @@ using Microsoft.AspNetCore.Connections; using NPOI.SS.Formula.Functions; using Onebot.Protocol; using Onebot.Protocol.Models.Messages; +using Zack.EventBus; namespace asg_form { - public class qqbot:BackgroundService + [EventName("查询选手")] + public class qqbot:IIntegrationEventHandler { - private void setTaskAtFixedTime() - { - DateTime now = DateTime.Now; - DateTime oneOClock = DateTime.Today.AddHours(1.0); //凌晨1:00 - if (now > oneOClock) - { - // Console.WriteLine("=======================================>"); - oneOClock = oneOClock.AddDays(5.0); - } - int msUntilFour = (int)((oneOClock - now).TotalMilliseconds); + private IEventBus eventBus; - var t = new System.Threading.Timer(doAt1AM); - t.Change(msUntilFour, Timeout.Infinite); + public qqbot(IEventBus eventBus) + { + this.eventBus = eventBus; } + public Task Handle(string eventName, string eventData) + { + + if (eventName == "查询选手") + { + try + { + TestDbContext ctx = new TestDbContext(); + + string msg = ""; + var roles = ctx.Roles.Include(a => a.form).Where(a => a.role_name.IndexOf(eventData) >= 0).ToList(); + foreach (var role in roles) + { + msg = $"{msg}\r\n姓名:{role.role_name}\r\n第五人格ID:{role.role_id}\r\n选手id:{role.Id}\r\n阵营:{role.role_lin}\r\n属于队伍:{role.form.team_name}\r\n"; + } + eventBus.Publish("查询选手req",msg); + } + catch + { + + } + + } + return Task.CompletedTask; + } + + public static bool isToday(DateTime dt) { DateTime today = DateTime.Today; @@ -52,188 +73,139 @@ namespace asg_form private async void doAt1AM(object state) { //执行功能... - try - { - TestDbContext db = new TestDbContext(); - var sh = db.team_Games.ToList(); - var sh1= sh.Where(a=>isToday(a.opentime)).ToList(); - string msg = ""; - if (sh1 == null) - { - msg = "<今日无赛程!>"; - } - else - { - foreach (var a in sh1) - { - msg = $"{msg}\r\r{a.team1_name} VS {a.team2_name}"; - } - } - await MessageManager.SendGroupMessageAsync("870248618", $"今日赛程:\r\n{msg}\r\n请有比赛的解说提前准备好。"); - await Task.Delay(3000); - await MessageManager.SendGroupMessageAsync("456414070", $"今日赛程:\r\n{msg}\r\n。直播地址:\r\nhttps://live.bilibili.com/24208371"); + + } - Console.WriteLine("开始备份数据库"); + + // protected override async Task ExecuteAsync(CancellationToken stoppingToken) + // { + // try + // { + // var client = new OnebotClient(ConnectionFactory.FromWebsocket("localhost", 7890, "WHO_S_YOUR_DADDY")); + // await client.SendPrivateMessageAsync("10000", new Message() + // { + // MessageSegment.Text("hello") + // }); + // client. - } - catch - { + // bot.EventReceived + // .OfType() + // .Subscribe(async receiver => + // { + // await Task.Delay(10000); + // await ExecuteAsync(stoppingToken); + // }); - } - //再次设定 - setTaskAtFixedTime(); - } + // bot.MessageReceived + // .OfType() + // .Subscribe(async x => + // { + // try + // { + // if (x.MessageChain.GetPlainMessage() == "近期赛程") + // { + // TestDbContext testDb = new TestDbContext(); + // int q = testDb.team_Games.Count(); + // var a = testDb.team_Games.Where(a => a.opentime >= DateTime.Now).Take(7); + // string msg = ""; + // foreach (var b in a) + // { + // msg = $"{msg}\r\n{b.team1_name} VS {b.team2_name}\r\n时间:{b.opentime.ToString("f")}"; + // } + // await MessageManager.SendGroupMessageAsync(x.GroupId, msg); + // } + // if (x.MessageChain.GetPlainMessage() == "后端状态") + // { + // string msg = $"CPU占用:{SystemInfo.CpuLoad.ToString("f2")}\r\n内存占用:{SystemInfo.MemoryAvailable.ToString("f2")}\r\n部署系统:{Windows.GetOsVersion()}"; + // await MessageManager.SendGroupMessageAsync(x.GroupId, msg); + // } + // if (x.MessageChain.GetPlainMessage() == "参赛队伍") + // { + // TestDbContext testDb = new TestDbContext(); + // var team = testDb.Forms.Select(a => a.team_name); + // string msg = ""; + // foreach (var t in team) + // { + // msg = $"{msg} {t}"; + // } + // await MessageManager.SendGroupMessageAsync(x.GroupId, $"在所有比赛中有以下队伍参赛:\r\n{string.Join(" , ",team)}"); + // } + // if (x.MessageChain.GetPlainMessage() == "查询冠军") + // { + // TestDbContext ctx = new TestDbContext(); - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - try - { - //定时任务 - setTaskAtFixedTime(); - var client = new OnebotClient(ConnectionFactory.FromWebsocket("localhost", 7890, "WHO_S_YOUR_DADDY")); - await client.SendPrivateMessageAsync("10000", new Message() - { - MessageSegment.Text("hello") - }); - client. - - bot.EventReceived - .OfType() - .Subscribe(async receiver => - { - await Task.Delay(10000); - await ExecuteAsync(stoppingToken); - }); - - bot.MessageReceived - .OfType() - .Subscribe(async x => - { - try - { - if (x.MessageChain.GetPlainMessage() == "近期赛程") - { - TestDbContext testDb = new TestDbContext(); - int q = testDb.team_Games.Count(); - var a = testDb.team_Games.Where(a => a.opentime >= DateTime.Now).Take(7); - string msg = ""; - foreach (var b in a) - { - msg = $"{msg}\r\n{b.team1_name} VS {b.team2_name}\r\n时间:{b.opentime.ToString("f")}"; - } - await MessageManager.SendGroupMessageAsync(x.GroupId, msg); - } - if (x.MessageChain.GetPlainMessage() == "后端状态") - { - string msg = $"CPU占用:{SystemInfo.CpuLoad.ToString("f2")}\r\n内存占用:{SystemInfo.MemoryAvailable.ToString("f2")}\r\n部署系统:{Windows.GetOsVersion()}"; - - await MessageManager.SendGroupMessageAsync(x.GroupId, msg); - } - if (x.MessageChain.GetPlainMessage() == "参赛队伍") - { - TestDbContext testDb = new TestDbContext(); - var team = testDb.Forms.Select(a => a.team_name); - string msg = ""; - foreach (var t in team) - { - msg = $"{msg} {t}"; - } - await MessageManager.SendGroupMessageAsync(x.GroupId, $"在所有比赛中有以下队伍参赛:\r\n{string.Join(" , ",team)}"); - - } - if (x.MessageChain.GetPlainMessage() == "查询冠军") - { - TestDbContext ctx = new TestDbContext(); - - var teams = ctx.Champions.Include(a=>a.events).Include(a=>a.form.role).Select(a => new {a.form,a.events,a.msg}).ToList(); - string msg = ""; - foreach (var t in teams) - { - string role = ""; + // var teams = ctx.Champions.Include(a=>a.events).Include(a=>a.form.role).Select(a => new {a.form,a.events,a.msg}).ToList(); + // string msg = ""; + // foreach (var t in teams) + // { + // string role = ""; - foreach (var t2 in t.form.role) - { - role = $"{role} {t2.role_name}"; - } - msg = $"{msg}\r\n队伍名称:{t.form.team_name}\r\n队员:{role}\r\n属于:{t.events.name}\r\n简介:{t.msg}\r\n"; - } - await MessageManager.SendGroupMessageAsync(x.GroupId, $"拥有以下冠军:{msg}"); + // foreach (var t2 in t.form.role) + // { + // role = $"{role} {t2.role_name}"; + // } + // msg = $"{msg}\r\n队伍名称:{t.form.team_name}\r\n队员:{role}\r\n属于:{t.events.name}\r\n简介:{t.msg}\r\n"; + // } + // await MessageManager.SendGroupMessageAsync(x.GroupId, $"拥有以下冠军:{msg}"); - } - } - catch(Exception ex) - { - await MessageManager.SendGroupMessageAsync(x.GroupId, $"错误:{ex.Message}"); + // } + // } + // catch(Exception ex) + // { + // await MessageManager.SendGroupMessageAsync(x.GroupId, $"错误:{ex.Message}"); - } + // } - }); + // }); - bot.MessageReceived - .OfType() - .Where(a=>a.MessageChain.GetPlainMessage().StartsWith("查询战队 ")) - .Subscribe(async x => - { + // bot.MessageReceived + // .OfType() + // .Where(a=>a.MessageChain.GetPlainMessage().StartsWith("查询战队 ")) + // .Subscribe(async x => + // { - try { - TestDbContext ctx = new TestDbContext(); + // try { + // TestDbContext ctx = new TestDbContext(); - string result = x.MessageChain.GetPlainMessage().Substring(5); // 截取从'o'之后的字符串 - Console.WriteLine(result); - List
teams = ctx.Forms.Include(a => a.role).Where(a => a.team_name.IndexOf(result) >= 0).ToList(); - string msg = ""; - foreach (var t in teams) - { - string role = ""; - foreach (var t2 in t.role) - { - role = $"{role} {t2.role_name}"; - } - msg = $"{msg}\r\n队伍名称:{t.team_name}\r\n队员:{role}\r\n"; - } - await MessageManager.SendGroupMessageAsync(x.GroupId, msg); - } - catch - { + // string result = x.MessageChain.GetPlainMessage().Substring(5); // 截取从'o'之后的字符串 + // Console.WriteLine(result); + // List teams = ctx.Forms.Include(a => a.role).Where(a => a.team_name.IndexOf(result) >= 0).ToList(); + // string msg = ""; + // foreach (var t in teams) + // { + // string role = ""; + // foreach (var t2 in t.role) + // { + // role = $"{role} {t2.role_name}"; + // } + // msg = $"{msg}\r\n队伍名称:{t.team_name}\r\n队员:{role}\r\n"; + // } + // await MessageManager.SendGroupMessageAsync(x.GroupId, msg); + // } + // catch + // { - } + // } - }); + // }); - bot.MessageReceived - .OfType() - .Where(a => a.MessageChain.GetPlainMessage().StartsWith("查询选手 ")) - .Subscribe(async x => - { + // bot.MessageReceived + //.OfType() + //.Where(a => a.MessageChain.GetPlainMessage().StartsWith("查询选手 ")) + // .Subscribe(async x => + // { - try - { - TestDbContext ctx = new TestDbContext(); + - string result = x.MessageChain.GetPlainMessage().Substring(5); // 截取从'o'之后的字符串 - Console.WriteLine(result); - string msg = ""; - var roles= ctx.Roles.Include(a => a.form).Where(a => a.role_name.IndexOf(result) >= 0).ToList(); - foreach(var role in roles) - { - msg = $"{msg}\r\n姓名:{role.role_name}\r\n第五人格ID:{role.role_id}\r\n选手id:{role.Id}\r\n阵营:{role.role_lin}\r\n属于队伍:{role.form.team_name}\r\n"; - } - await MessageManager.SendGroupMessageAsync(x.GroupId,$"搜索到以下结果:\r\n{msg}"); - } - catch - { - - } - - }); + // }); @@ -245,17 +217,15 @@ namespace asg_form - Console.ReadLine(); - bot.Dispose(); - } - catch - { - Console.WriteLine("bot启动失败 ~~~~~ "); - await Task.Delay(10000); - } + // } + // catch + // { + + // } - } - } + // } + + + } } -*/ \ No newline at end of file diff --git a/qqbot/ExamplePlugin.cs b/qqbot/ExamplePlugin.cs index 52b051c..f570ac5 100644 --- a/qqbot/ExamplePlugin.cs +++ b/qqbot/ExamplePlugin.cs @@ -3,11 +3,20 @@ using Flandre.Core.Common; using Flandre.Core.Messaging; using Flandre.Framework.Common; using Flandre.Framework.Routing; +using Zack.EventBus; namespace qqbot { public sealed class ExamplePlugin : Plugin { + private IEventBus eventBus; + + public ExamplePlugin(IEventBus eventBus) + { + this.eventBus = eventBus; + } + + /// /// 每次收到消息时触发。 /// @@ -43,15 +52,10 @@ namespace qqbot { try { - TestDbContext ctx = new TestDbContext(); + eventBus.Publish("查询选手", name); - string msg = ""; - var roles = ctx.Roles.Where(a => a.role_name.IndexOf(name) >= 0).ToList(); - foreach (var role in roles) - { - msg = $"{msg}\r\n姓名:{role.role_name}\r\n第五人格ID:{role.role_id}\r\n选手id:{role.Id}\r\n阵营:{role.role_lin}\r\n"; - } - return msg; + + return null; } catch { diff --git a/qqbot/Program.cs b/qqbot/Program.cs index bb2ef59..f5db69e 100644 --- a/qqbot/Program.cs +++ b/qqbot/Program.cs @@ -1,7 +1,10 @@ using Flandre.Adapters.OneBot.Extensions; using Flandre.Framework; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using qqbot; +using System.Reflection; +using Zack.EventBus; var builder = FlandreApp.CreateBuilder(new HostApplicationBuilderSettings { @@ -14,6 +17,15 @@ var builder = FlandreApp.CreateBuilder(new HostApplicationBuilderSettings builder.Adapters.AddOneBot(builder.Configuration.GetSection("Adapters:OneBot")); builder.Plugins.Add(); +var eventBusSec = builder.Configuration.GetSection("EventBus"); +builder.Services.Configure(eventBusSec); +builder.Services.AddEventBus("qqbot_client", Assembly.GetExecutingAssembly()); + + + + + + var app = builder.Build(); // 添加内置中间件。 // 这些中间件保证 Flandre 的正常运转。你也可以加入自己的中间件,并灵活调整其顺序。 diff --git a/qqbot/appsettings.json b/qqbot/appsettings.json index 09f2485..ef8532e 100644 --- a/qqbot/appsettings.json +++ b/qqbot/appsettings.json @@ -4,6 +4,10 @@ "Default": "Information" } }, + "EventBus": { + "HostName": "127.0.0.1", + "ExchangeName": "qqbot" + }, "Adapters": { "OneBot": { "Bots": [ diff --git a/qqbot/qqbot.csproj b/qqbot/qqbot.csproj index 1c293e9..3b4969b 100644 --- a/qqbot/qqbot.csproj +++ b/qqbot/qqbot.csproj @@ -13,6 +13,7 @@ +