/// <summary> /// 触发加分 /// </summary> /// <param name="eventArgs"></param> /// <param name="userConfig"></param> /// <returns></returns> private async ValueTask TriggerCute(GroupMessageEventArgs eventArgs, UserConfig userConfig) { if (new Random().Next(1, 100) is 66) { var user = _userServices.QueryById(q => q.QNumber == eventArgs.SenderInfo.UserId.ObjToString()); var rank = new Random().Next(1, 10); if (user == null) { await eventArgs.Reply(CQCode.CQAt(eventArgs.SenderInfo.UserId), $"未找到{userConfig.ConfigModel.NickName}任何记录,奖励下发失败~"); } else { user.Rank += rank; user.LastModifyTime = DateTime.Now; _userServices.Update(user); _logsServices.Insert(new SignLogs() { CmdType = CmdType.BonusPoints, LogContent = $"[可爱]指令赠送{rank}分", ModifyRank = rank, Uid = eventArgs.SenderInfo.UserId.ObjToString() }); await eventArgs.Reply(CQCode.CQAt(eventArgs.SenderInfo.UserId), $"看{userConfig.ConfigModel.NickName}这么可爱,就奖励{userConfig.ConfigModel.NickName}{rank}分~"); } } }
/// <summary> /// 用于处理传入指令 /// </summary> public async void GetChat() { ConfigManager configManager = new ConfigManager(HsoEventArgs.LoginUid); configManager.LoadUserConfig(out UserConfig userConfig); if (CheckGroupBlock(userConfig)) { return; } if (CheckInCD.isInCD(HsoEventArgs.SourceGroup, HsoEventArgs.Sender)) { await HsoEventArgs.SourceGroup.SendGroupMessage(CQCode.CQAt(HsoEventArgs.Sender), "你是不是只会要色图"); return; } //检查色图文件夹大小 if (IOUtils.GetHsoSize() >= userConfig.HsoConfig.SizeLimit * 1024 * 1024) { Log.Warning("Hso", "色图文件夹超出大小限制,将清空文件夹"); Directory.Delete(IOUtils.GetHsoPath(), true); } await GiveMeSetu(userConfig.HsoConfig); }
/// <summary> /// 数据库发生错误时的消息提示 /// </summary> public static async ValueTask DatabaseFailedTips(GroupMessageEventArgs groupEventArgs) { await groupEventArgs.SourceGroup.SendGroupMessage(CQCode.CQAt(groupEventArgs.Sender.Id), "\r\nERROR", "\r\n数据库错误"); ConsoleLog.Error("database", "database error"); }
internal static async ValueTask GroupPokeEventParse(object sender, GroupPokeEventArgs groupPokeEventArgs) { if (groupPokeEventArgs.TargetUser == groupPokeEventArgs.LoginUid && !CheckInCD.isInCD(groupPokeEventArgs.SourceGroup, groupPokeEventArgs.SendUser)) { await groupPokeEventArgs.SourceGroup.SendGroupMessage(CQCode.CQAt(groupPokeEventArgs.SendUser), "\r\n你今晚必被爽哥杀害\r\n", CQCode.CQImage("https://i.loli.net/2020/10/20/zWPyocxFEVp2tDT.jpg")); } }
public async ValueTask GroupPokeEventParse(object sender, GroupPokeEventArgs eventArgs) { if (eventArgs.TargetUser == eventArgs.LoginUid && !CheckInCD.IsInCD(eventArgs.SourceGroup, eventArgs.SendUser)) { await eventArgs .SourceGroup .SendGroupMessage( CQCode.CQAt(eventArgs.SendUser), "\r\n再戳锤爆你的头\r\n", CQCode.CQImage("https://i.loli.net/2020/10/20/zWPyocxFEVp2tDT.jpg")); } }
/// <summary> /// 权限检查/越权警告 /// </summary> internal async Task <bool> AuthCheck() { if (IsAdmin) { return(true); } else { await SourceGroup.SendGroupMessage(CQCode.CQAt(Sender.Id), " 你没有执行此指令的权限"); ConsoleLog.Warning($"会战[群:{SourceGroup.Id}]", $"群成员{MessageEventArgs.SenderInfo.Nick}正在尝试执行指令{CommandType}"); return(false); } }
/// <summary> /// 检查参数数组长度 /// </summary> /// <param name="args">指令数组</param> /// <param name="len">至少需要的参数个数</param> /// <param name="QQgroup">(可选,不给的话就不发送错误信息)\n报错信息要发送到的QQ群对象</param> /// <param name="fromQQid">(可选,但QQgroup给了的话本参数必填)\n要通知的人的QQ Id</param> /// <returns>Illegal不符合 Legitimate符合 Extra超出</returns> public static LenType CheckForLength(string[] args, int len, Group QQgroup = null, long fromQQid = 0) { if (args.Length >= len + 1) { if (args.Length == len + 1) { return(LenType.Legitimate); } else { return(LenType.Extra); } } else { QQgroup?.SendGroupMessage(CQCode.CQAt(fromQQid), " 命令参数不全,请补充。"); return(LenType.Illegal); } }
/// <summary> /// 处理ai /// </summary> /// <param name="eventArgs"></param> /// <param name="userConfig"></param> /// <returns></returns> private static async ValueTask IsAI(GroupMessageEventArgs eventArgs, UserConfig userConfig) { //[CQ:at,qq=503745803] 你好啊 try { var at = $"[CQ:at,qq={eventArgs.LoginUid}]"; if (eventArgs.Message.RawText.Contains(at) && userConfig.ModuleSwitch.IsAI) { var json = await RequestAi(userConfig.ConfigModel.AiPath, eventArgs.Message.RawText.Replace(at, "").Replace(" ", "")); await eventArgs.Reply(CQCode.CQAt(eventArgs.Sender.Id), json.ObjectToGBK()); } } catch (Exception c) { await eventArgs.Reply(c.Message); } }
/// <summary> /// 删除公会 /// </summary> private async void DeleteGuild() { //判断公会是否存在 switch (DBHelper.GuildExists()) { case 0: await SourceGroup.SendGroupMessage(CQCode.CQAt(Sender), "\r\n此群并未标记为公会"); return; case -1: await SourceGroup.SendGroupMessage(CQCode.CQAt(Sender), "\r\nERROR\r\n数据库错误"); return; } //获取当前群公会名 string guildName = DBHelper.GetGuildName(SourceGroup); //删除公会 await SourceGroup.SendGroupMessage(DBHelper.DeleteGuild(SourceGroup) ?$" 公会[{guildName}]已被删除。" : $" 公会[{guildName}]删除失败,数据库错误。"); }
/// <summary> /// 零参数指令的参数检查 /// 同时检查成员是否存在 /// </summary> /// <returns> /// <para><see langword="true"/> 指令合法</para> /// <para><see langword="false"/> 有多余参数</para> /// </returns> internal async ValueTask <bool> ZeroArgsCheck() { //检查参数 switch (BotUtils.CheckForLength(CommandArgs, 0)) { case LenType.Extra: await MessageEventArgs.SourceGroup.SendGroupMessage(CQCode.CQAt(MessageEventArgs.Sender.Id), "\r\n听不见!重来!(有多余参数)"); return(false); case LenType.Legitimate: return(true); default: await MessageEventArgs.SourceGroup.SendGroupMessage(CQCode.CQAt(MessageEventArgs.Sender.Id), "发生未知错误,请联系机器人管理员"); ConsoleLog.Error("Unknown error", "LenType"); return(false); } }
private async void RandomNumber() { Random randomGen = new Random(); await QQGroup.SendGroupMessage(CQCode.CQAt(MFKEventArgs.Sender.Id), "丢出了\r\n", randomGen.Next(0, 100)); }
/// <summary> /// 退会 /// </summary> private async void QuitGuild() { //检查公会是否存在 if (DBHelper.GuildExists() != 1) { await SourceGroup.SendGroupMessage("该群未创建公会"); return; } //获取成员参数 List <long> quitList = new List <long>(); switch (BotUtils.CheckForLength(CommandArgs, 1)) { case LenType.Illegal: quitList.Add(Sender); break; case LenType.Legitimate: //只有单一成员时需判断uid参数 quitList.AddRange(MessageEventArgs.Message.GetAllAtList()); if (quitList.Count == 0) { if (long.TryParse(CommandArgs[1], out long uid)) { quitList.Add(uid); } else { await SourceGroup.SendGroupMessage("没有At任何成员"); return; } } break; case LenType.Extra: quitList.AddRange(MessageEventArgs.Message.GetAllAtList()); if (quitList.Count == 0) { await SourceGroup.SendGroupMessage("没有At任何成员"); return; } break; } //检查列表中是否有机器人 if (quitList.Any(member => member == MessageEventArgs.LoginUid)) { quitList.Remove(MessageEventArgs.LoginUid); await SourceGroup.SendGroupMessage("不要在成员中At机器人啊kora"); if (quitList.Count == 0) { return; } } Log.Debug("Guild Mgr", $"Get quit list count={quitList.Count}"); Dictionary <long, int> databaseRet = new Dictionary <long, int>(); //删除退会成员 foreach (long member in quitList) { databaseRet.Add(member, DBHelper.QuitGuild(member, SourceGroup)); } //构建格式化信息 List <CQCode> responseMsg = new List <CQCode>(); if (databaseRet.Any(ret => ret.Value == 0)) { responseMsg.Add(CQCode.CQText("以下成员已退出:")); foreach (long member in databaseRet.Where(member => member.Value == 0) .Select(member => member.Key) .ToList()) { responseMsg.Add(CQCode.CQText("\r\n")); responseMsg.Add(CQCode.CQAt(member)); } } if (databaseRet.Any(ret => ret.Value == 1)) { if (responseMsg.Count != 0) { responseMsg.Add(CQCode.CQText("\r\n")); } responseMsg.Add(CQCode.CQText("以下成员并不在公会中:")); foreach (long member in databaseRet.Where(member => member.Value == 1) .Select(member => member.Key) .ToList()) { responseMsg.Add(CQCode.CQText("\r\n")); responseMsg.Add(CQCode.CQAt(member)); } } if (databaseRet.Any(ret => ret.Value == -1)) { if (responseMsg.Count != 0) { responseMsg.Add(CQCode.CQText("\r\n")); } responseMsg.Add(CQCode.CQText("以下成员在退出时发生错误:")); foreach (long member in databaseRet.Where(member => member.Value == -1) .Select(member => member.Key) .ToList()) { responseMsg.Add(CQCode.CQText("\r\n")); responseMsg.Add(CQCode.CQAt(member)); } } //发送信息 await SourceGroup.SendGroupMessage(responseMsg); }
/// <summary> /// 加入除机器人外的所有成员 /// 没有人数限制 /// </summary> private async void JoinAll() { //检查公会是否存在 if (DBHelper.GuildExists() != 1) { await SourceGroup.SendGroupMessage("该群未创建公会"); return; } //获取所有成员的信息 var(apiStatus, groupMemberList) = await SourceGroup.GetGroupMemberList(); if (apiStatus != APIStatusType.OK) { Log.Error("API error", $"api ret code {(int) apiStatus}"); await SourceGroup.SendGroupMessage(CQCode.CQAt(Sender), "\r\nAPI调用错误请重试"); return; } //移除机器人的成员信息 groupMemberList.RemoveAt(groupMemberList.FindIndex(member => member.UserId == MessageEventArgs.LoginUid)); //添加成员到公会 Dictionary <long, int> databaseRet = new Dictionary <long, int>(); foreach (GroupMemberInfo member in groupMemberList) { //获取群成员名 string memberName = groupMemberList.Any(memberInfo => memberInfo.UserId == member.UserId) ? groupMemberList.Where(memberInfo => memberInfo.UserId == member.UserId) .Select(memberInfo => string.IsNullOrEmpty(memberInfo.Card) ? memberInfo.Nick : memberInfo.Card) .First() : "N/A"; //添加成员 databaseRet.Add(member.UserId, DBHelper.JoinGuild(member.UserId, SourceGroup, memberName)); } //构建格式化信息 List <CQCode> responseMsg = new List <CQCode>(); if (databaseRet.Any(ret => ret.Value == 0)) //成员成功加入 { responseMsg.Add(CQCode.CQText("以下成员已加入:")); foreach (long member in databaseRet.Where(member => member.Value == 0) .Select(member => member.Key) .ToList()) { responseMsg.Add(CQCode.CQText("\r\n")); responseMsg.Add(CQCode.CQAt(member)); } } if (databaseRet.Any(ret => ret.Value == 1)) //成员已存在 { if (responseMsg.Count != 0) { responseMsg.Add(CQCode.CQText("\r\n")); } responseMsg.Add(CQCode.CQText("以下成员已在公会中,仅更新信息:")); foreach (long member in databaseRet.Where(member => member.Value == 1) .Select(member => member.Key) .ToList()) { responseMsg.Add(CQCode.CQText("\r\n")); responseMsg.Add(CQCode.CQAt(member)); } } if (databaseRet.Any(ret => ret.Value == -1)) //数据库错误 { if (responseMsg.Count != 0) { responseMsg.Add(CQCode.CQText("\r\n")); } responseMsg.Add(CQCode.CQText("以下成员在加入时发生错误:")); foreach (long member in databaseRet.Where(member => member.Value == -1) .Select(member => member.Key) .ToList()) { responseMsg.Add(CQCode.CQText("\r\n")); responseMsg.Add(CQCode.CQAt(member)); } } //发送信息 await SourceGroup.SendGroupMessage(responseMsg); }
/// <summary> /// 加入公会 /// </summary> private async void JoinGuild() { //检查公会是否存在 if (DBHelper.GuildExists() != 1) { await SourceGroup.SendGroupMessage("该群未创建公会"); return; } //处理需要加入的成员列表 List <long> joinList = new List <long>(); switch (BotUtils.CheckForLength(CommandArgs, 1)) { case LenType.Illegal: joinList.Add(Sender); break; case LenType.Extra: case LenType.Legitimate: joinList.AddRange(MessageEventArgs.Message.GetAllAtList()); if (joinList.Count == 0) { await SourceGroup.SendGroupMessage("没有At任何成员"); return; } break; } //检查列表中是否有机器人 if (joinList.Any(member => member == MessageEventArgs.LoginUid)) { joinList.Remove(MessageEventArgs.LoginUid); await SourceGroup.SendGroupMessage("不要在成员中At机器人啊kora"); if (joinList.Count == 0) { return; } } Log.Debug("Guild Mgr", $"Get join list count={joinList.Count}"); //从API获取成员信息 Log.Debug("Guild Mgr", "Get group member infos"); var(apiStatus, groupMemberList) = await SourceGroup.GetGroupMemberList(); if (apiStatus != APIStatusType.OK) { Log.Error("API error", $"api ret code {(int) apiStatus}"); await SourceGroup.SendGroupMessage(CQCode.CQAt(Sender), "\r\nAPI调用错误请重试"); return; } //加入待加入的成员 Dictionary <long, int> databaseRet = new Dictionary <long, int>(); foreach (long member in joinList) { //获取群成员名 string memberName = groupMemberList.Any(memberInfo => memberInfo.UserId == member) ? groupMemberList.Where(memberInfo => memberInfo.UserId == member) .Select(memberInfo => string.IsNullOrEmpty(memberInfo.Card) ? memberInfo.Nick : memberInfo.Card) .First() : "N/A"; //添加成员 databaseRet.Add(member, DBHelper.JoinGuild(member, SourceGroup, memberName)); } //构建格式化信息 List <CQCode> responseMsg = new List <CQCode>(); if (databaseRet.Any(ret => ret.Value == 0)) //成员成功加入 { responseMsg.Add(CQCode.CQText("以下成员已加入:")); foreach (long member in databaseRet.Where(member => member.Value == 0) .Select(member => member.Key) .ToList()) { responseMsg.Add(CQCode.CQText("\r\n")); responseMsg.Add(CQCode.CQAt(member)); } } if (databaseRet.Any(ret => ret.Value == 1)) //成员已存在 { if (responseMsg.Count != 0) { responseMsg.Add(CQCode.CQText("\r\n")); } responseMsg.Add(CQCode.CQText("以下成员已在公会中,仅更新信息:")); foreach (long member in databaseRet.Where(member => member.Value == 1) .Select(member => member.Key) .ToList()) { responseMsg.Add(CQCode.CQText("\r\n")); responseMsg.Add(CQCode.CQAt(member)); } } if (databaseRet.Any(ret => ret.Value == -1)) //数据库错误 { if (responseMsg.Count != 0) { responseMsg.Add(CQCode.CQText("\r\n")); } responseMsg.Add(CQCode.CQText("以下成员在加入时发生错误:")); foreach (long member in databaseRet.Where(member => member.Value == -1) .Select(member => member.Key) .ToList()) { responseMsg.Add(CQCode.CQText("\r\n")); responseMsg.Add(CQCode.CQAt(member)); } } //发送信息 await SourceGroup.SendGroupMessage(responseMsg); }
/// <summary> /// 创建公会 /// </summary> private async void CreateGuild() { //检查群是否已经被标记为公会 switch (this.DBHelper.GuildExists()) { case 1: await base.SourceGroup.SendGroupMessage(CQCode.CQAt(base.Sender), $"此群已被标记为[{DBHelper.GetGuildInfo(base.SourceGroup).GuildName}]公会"); return; } //公会名 string guildName; //公会区服 Server guildServer; //判断公会名 switch (BotUtils.CheckForLength(this.CommandArgs, 1)) { case LenType.Extra: if (BotUtils.CheckForLength(this.CommandArgs, 2) == LenType.Legitimate) { guildName = CommandArgs[2]; } else { await base.SourceGroup.SendGroupMessage(CQCode.CQAt(base.Sender), "\r\n有多余参数"); return; } break; case LenType.Legitimate: var groupInfo = await base.SourceGroup.GetGroupInfo(); if (groupInfo.apiStatus != APIStatusType.OK) { Log.Error("API error", $"api ret code {(int) groupInfo.apiStatus}"); await base.SourceGroup.SendGroupMessage(CQCode.CQAt(base.Sender), "\r\nAPI调用错误请重试"); return; } guildName = groupInfo.groupInfo.GroupName; break; default: return; } //判断区服 if (Enum.IsDefined(typeof(Server), CommandArgs[1])) { guildServer = (Server)Enum.Parse(typeof(Server), CommandArgs[1]); if (guildServer != Server.CN) { await base.SourceGroup.SendGroupMessage("暂不支持国服以外的服务器"); return; } } else { await base.SourceGroup.SendGroupMessage(CQCode.CQAt(base.Sender), "弟啊,你哪个服务器的"); return; } switch (this.DBHelper.CreateGuild(guildServer, guildName, base.SourceGroup)) { case -1: await BotUtils.DatabaseFailedTips(MessageEventArgs); break; case 0: await base.SourceGroup.SendGroupMessage($"公会[{guildName}]已创建"); break; default: await base.SourceGroup.SendGroupMessage("发生了未知错误"); break; } }
/// <summary> /// 获取At的CQ码 /// </summary> /// <returns> /// <see cref="CQCode"/> AT /// </returns> public CQCode CQCodeAt() { return(CQCode.CQAt(this.Id)); }