private void UpdateList(int ms = 1000) { int counter = 0; while (true) { this.Invoke(new Action(() => { UpdateListBoxItems(ref LstSessions, ref HistoryMsg.Friend); UpdateListBoxItems(ref LstGroupMsg, ref HistoryMsg.Group); })); if (counter == 0) { var contact = session.GetFriendListAsync().Result; this.Invoke(new Action(() => { UpdateListBoxItems(ref LstContacts, contact); })); var group = session.GetGroupListAsync().Result; this.Invoke(new Action(() => { UpdateListBoxItems(ref LstGroups, group); })); } if (counter == 5 * 60) { counter = 0; } Thread.Sleep(ms); ++counter; } // ReSharper disable once FunctionNeverReturns }
public static async Task Main() { AppDomain.CurrentDomain.UnhandledException += (_, e) => ErrorHelper.WriteErrorLog(e.ExceptionObject); Console.WriteLine("葱葱机器人"); if (!File.Exists(Cache.JsonConfigFileName)) { Console.WriteLine("初次使用本机器人,请先设置config.json相关参数。"); } Console.WriteLine("请输入机器人QQ号:"); long qqId = Convert.ToInt64(Console.ReadLine()); Console.WriteLine("请输入mirai-api-http host IP:"); string ip = Console.ReadLine(); Console.WriteLine("请输入mirai-api-http端口:"); int port = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入mirai-api-http autoKey:"); string autoKey = Console.ReadLine(); MiraiHttpSessionOptions options = new MiraiHttpSessionOptions(ip, port, autoKey); await using MiraiHttpSession session = new MiraiHttpSession(); session.AddPlugin(new TempMessage()); session.AddPlugin(new FriendMessage()); session.AddPlugin(new GroupMessage()); bool stop = false; await session.ConnectAsync(options, qqId).ContinueWith(callback => { if (callback.IsFaulted) { stop = true; Console.WriteLine("连接失败,请检查Mirai是否已经正常启动并已配置mirai-api-http相关参数。"); } }); if (stop) { return; } Cache.SetTaskAtFixedTime(); string nickname = (await session.GetFriendListAsync()).Where(m => m.Id == qqId).FirstOrDefault().Name; Console.WriteLine($"已连接到mirai-api-http, 登录昵称:{nickname}, 输入exit或按下Ctrl+C断开连接。"); while (true) { if (await Console.In.ReadLineAsync() == "exit") { return; } } }
public QClient(string addr, int port, string passwd, long account) { MiraiHttpSessionOptions options = new MiraiHttpSessionOptions(addr, port, passwd); session = new MiraiHttpSession(); handler = new EventHandler(this); conversations = new List <Conversation>(); session.ConnectAsync(options, account).Wait(); friends = session.GetFriendListAsync().Result.ToList(); groups = session.GetGroupListAsync().Result.ToList(); session.AddPlugin(handler); }
public async Task Main(long qqId, string ip, int port, string autoKey) { MiraiHttpSessionOptions options = new MiraiHttpSessionOptions(ip, port, autoKey); await using MiraiHttpSession session = new MiraiHttpSession(); session.AddPlugin(new TempMessage()); session.AddPlugin(new FriendMessage()); session.AddPlugin(new GroupMessage()); bool stop = false; await session.ConnectAsync(options, qqId).ContinueWith(callback => { if (callback.IsFaulted) { stop = true; MessageBox.Show("连接失败,请检查Mirai是否已经正常启动并已配置mirai-api-http相关参数。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }); if (stop) { return; } Cache.SetTaskAtFixedTime(); string nickname = (await session.GetFriendListAsync()).Where(m => m.Id == qqId).FirstOrDefault().Name; Invoke(new Action(() => { lblState.Text = $"连接状态: 已连接到mirai-api-http, 登录昵称:{nickname}"; lblState.ForeColor = Color.Black; btnConnect.Text = "断开连接"; btnConnect.Click -= btnConnect_Click; btnConnect.Click += btnDeconnect_Click; })); while (true) { if (await Console.In.ReadLineAsync() == "exit") { Invoke(new Action(() => { btnConnect.Text = "连接到mirai-api-http"; lblState.Text = "连接状态: 未连接到mirai-api-http"; lblState.ForeColor = Color.Red; })); return; } } }
public static Task <IFriendInfo[]> GetFriendListAsync() => session.GetFriendListAsync();