Skip to content

Commit

Permalink
add multiple cq server connection
Browse files Browse the repository at this point in the history
  • Loading branch information
cc004 committed Oct 9, 2021
1 parent aee04b9 commit a00bb9b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 47 deletions.
16 changes: 0 additions & 16 deletions BandoriBotCore/Apis/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,6 @@ public async Task<ActionResult<string>> PostRecord(long group)
{
return Configuration.GetConfig<Pipe>().GetHistory(group);
}
[HttpPost("record")]
public async Task<ActionResult> PostRecord(TimelineData data)
{
var start = data.Data.IndexOf("--[[");
var end = data.Data.IndexOf("]]");
var text = data.Data[(start + 4)..end];
await System.IO.File.AppendAllTextAsync("records.txt", data.Data + "\n");
var pdata = reg.Match(text);

var dmg = int.Parse(pdata.Groups[4].Value);
await MessageHandler.session.UploadGroupFile(1095742657L, Encoding.UTF8.GetBytes(text).ToCache()

, Fix(data.Name));
return NoContent();
}

[HttpGet("countv2")]
public async Task<ActionResult<string>> Countv2(string keyword = null, long qq = 0, long group = 0, long starttime = 0, long endtime = 0)
{
Expand Down
4 changes: 2 additions & 2 deletions BandoriBotCore/Config/SerializableConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace BandoriBot.Config
{
public abstract class SerializableConfiguration<T> : Configuration
public abstract class SerializableConfiguration<T> : Configuration where T : new()
{
public T t;

public override void LoadDefault()
{
t = Activator.CreateInstance<T>();
t = new T();
}

public override void LoadFrom(BinaryReader br)
Expand Down
8 changes: 2 additions & 6 deletions BandoriBotCore/Config/SetuConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace BandoriBot.Config
{
Expand All @@ -22,13 +23,8 @@ public class Picture
public string[] tags;
}

public class SetuConfig : SerializableConfiguration<Picture[]>
public class SetuConfig : SerializableConfiguration<List<Picture>>
{
public override string Name => "setu.json";

public override void LoadDefault()
{
t = Array.Empty<Picture>();
}
}
}
4 changes: 0 additions & 4 deletions BandoriBotCore/Handler/ISession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@

namespace BandoriBot.Handler
{
public interface ISession
{
public SoraApi Session { get; set; }
}
}
18 changes: 17 additions & 1 deletion BandoriBotCore/Handler/MessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public HandlerHolder(IMessageHandler handler)

public struct Source
{
public DateTime time;
public long FromGroup, FromQQ;
public SoraApi Session;
public bool IsTemp;
Expand Down Expand Up @@ -94,7 +95,6 @@ private class State
public static void Register<T>() where T : new()
{
var t = new T();
if (t is ISession session) session.Session = MessageHandler.session;
if (t is ICommand tcmd) Register(tcmd);
else if (t is IMessageHandler tmsg) Register(tmsg);
}
Expand Down Expand Up @@ -143,10 +143,26 @@ public void OnMessage(string message, Source source, bool isAdmin, Action<string
}).Wait();
}

private static Queue<int> msgqueue = new();
public static readonly HashSet<long> bots = new();

private static bool SameMessageFiltering(string msg, Source src)
{
if (bots.Contains(src.FromQQ)) return false;
var hash = HashCode.Combine(msg, src.FromGroup, src.FromQQ, src.time);
if (msgqueue.Contains(hash)) return false;
msgqueue.Enqueue(hash);
while (msgqueue.Count > 100) msgqueue.Dequeue();
return true;
}

public static async Task OnMessage(SoraApi session, string message, Source Sender)
{
if (!booted) return;

lock (msgqueue)
if (!SameMessageFiltering(message, Sender)) return;

long ticks = DateTime.Now.Ticks;

Func<string, Task> callback = async s =>
Expand Down
47 changes: 29 additions & 18 deletions BandoriBotCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Sora.Net;
using Sora.OnebotModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
Expand Down Expand Up @@ -71,8 +72,8 @@ private static void PluginInitialize()
MessageHandler.Register<SekaiCommand>();
//MessageHandler.Register<SekaiPCommand>();
MessageHandler.Register<WhitelistCommand>();
MessageHandler.Register<GachaCommand>();
MessageHandler.Register<GachaListCommand>();
//MessageHandler.Register<GachaCommand>();
//MessageHandler.Register<GachaListCommand>();
MessageHandler.Register<BlacklistCommand>();
MessageHandler.Register<TitleCommand>();
MessageHandler.Register<CarTypeCommand>();
Expand Down Expand Up @@ -146,32 +147,39 @@ private static void PluginInitialize()

}

public static async Task Main(string[] args)
public static void Main(string[] args)
{
//await Testing();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;

var service = SoraServiceFactory.CreateInstance(new ClientConfig()
{
Host = "127.0.0.1",
Port = uint.Parse(args[1])
});

service.Event.OnClientConnect += Event_OnClientConnect;
service.Event.OnFriendRequest += Event_OnFriendRequest;
service.Event.OnGroupMessage += Event_OnGroupMessage;
service.Event.OnPrivateMessage += Event_OnPrivateMessage;

PluginInitialize();

new Thread(() => Apis.Program.Main2(args)).Start();
var tasks = new List<Task>();

Console.WriteLine("connected to server");
foreach (var line in File.ReadAllLines("cqservers.txt"))
{
var s = line.Split(":");
var service = SoraServiceFactory.CreateInstance(new ClientConfig()
{
Host = s[0],
Port = uint.Parse(s[1])
});

service.Event.OnClientConnect += Event_OnClientConnect;
service.Event.OnFriendRequest += Event_OnFriendRequest;
service.Event.OnGroupMessage += Event_OnGroupMessage;
service.Event.OnPrivateMessage += Event_OnPrivateMessage;

await service.StartService();
Console.WriteLine("connected to server");

tasks.Add(service.StartService().AsTask());
}

Task.WaitAll(tasks.ToArray());
}

private static async ValueTask Event_OnPrivateMessage(string type, PrivateMessageEventArgs eventArgs)
Expand All @@ -180,7 +188,8 @@ private static async ValueTask Event_OnPrivateMessage(string type, PrivateMessag
{
Session = eventArgs.SoraApi,
FromGroup = 0,
FromQQ = eventArgs.SenderInfo.UserId
FromQQ = eventArgs.SenderInfo.UserId,
time = eventArgs.Time
});
}

Expand All @@ -190,7 +199,8 @@ private static async ValueTask Event_OnGroupMessage(string type, GroupMessageEve
{
Session = eventArgs.SoraApi,
FromGroup = eventArgs.SourceGroup.Id,
FromQQ = eventArgs.SenderInfo.UserId
FromQQ = eventArgs.SenderInfo.UserId,
time = eventArgs.Time
});
}

Expand All @@ -202,6 +212,7 @@ private static async ValueTask Event_OnFriendRequest(string type, FriendRequestE
private static async ValueTask Event_OnClientConnect(string type, Sora.EventArgs.SoraEvent.ConnectEventArgs eventArgs)
{
MessageHandler.session = eventArgs.SoraApi;
MessageHandler.bots.Add(eventArgs.LoginUid);
MessageHandler.booted = true;
}

Expand Down

0 comments on commit a00bb9b

Please sign in to comment.