private void RegisterAntiSpamFunctionality() { client.MessageReceived += async(message) => { SocketGuildUser messageSender; ISocketMessageChannel channel; SocketGuild guild; messageSender = message.Author as SocketGuildUser; channel = message.Channel; guild = (message.Channel as IGuildChannel).Guild as SocketGuild; xmlParameters.Load(FileLocations.xmlParameters()); XmlNode root = xmlParameters.DocumentElement; XmlNode spamTimerNode = CoOpGlobal.XML.findOrCreateChild(xmlParameters, root, "SpamTimer", "8"); spamTimer = int.Parse(spamTimerNode.InnerText); // Check to make sure that a bot is not the author if (!messageSender.IsBot) { // Increment the counter by 1 await Task.Factory.StartNew(async() => { await CountMessage(messageSender, guild, channel, 1); }); // Decrese the counter by 1 after parameteriesed number of seconds (default 8) await Task.Factory.StartNew(async() => { await CountMessage(messageSender, guild, channel, -1, spamTimer); }); } }; }
public static string steamKey() { XmlDocument xmlParameters = new XmlDocument(); XmlNode paramsRoot; XmlNode steamKeyNode; string steamKey; xmlParameters.Load(FileLocations.xmlParameters()); paramsRoot = xmlParameters.DocumentElement; steamKeyNode = CoOpGlobal.XML.findOrCreateChild(xmlParameters, paramsRoot, "SteamToken"); steamKey = steamKeyNode.InnerText; return(steamKey); }
private void loadXMLParameters() { XmlNode prefixNode; if (!File.Exists(FileLocations.xmlParameters())) { if (!File.Exists(FileLocations.backupXMLParameters())) { Console.WriteLine("XML parameters not found, no backup found"); throw new FileNotFoundException(@"XML parameters not found, and no backup found"); } else { xmlParameters.Load(FileLocations.backupXMLParameters()); xmlParameters.Save(FileLocations.xmlParameters()); Console.WriteLine("XML parameters restored from backup"); } } if (!File.Exists(FileLocations.xmlDatabase())) { if (!File.Exists(FileLocations.backupXMLDatabase())) { xmlParameters.Load(FileLocations.xmlParameters()); XmlNode dbRoot = xmlDatabase.CreateElement("CoOpBotDB"); XmlNode paramsRoot = xmlParameters.DocumentElement; XmlNode paramsUsersNode = paramsRoot.SelectSingleNode("descendant::Users"); XmlNode paramsGuildWarsNode = paramsRoot.SelectSingleNode("descendant::GuildWars"); if (paramsUsersNode != null && paramsGuildWarsNode != null) { XmlNode importUsersNode; XmlNode importGuildWarsNode; importUsersNode = xmlDatabase.ImportNode(paramsUsersNode, true); importGuildWarsNode = xmlDatabase.ImportNode(paramsGuildWarsNode, true); dbRoot.AppendChild(importUsersNode); dbRoot.AppendChild(importGuildWarsNode); xmlDatabase.AppendChild(dbRoot); xmlDatabase.Save(FileLocations.xmlDatabase()); paramsRoot.RemoveChild(paramsUsersNode); paramsRoot.RemoveChild(paramsGuildWarsNode); xmlParameters.Save(FileLocations.xmlParameters()); Console.WriteLine("XML database created from data in parameters file"); } else { Console.WriteLine("XML database not found, no backup found"); Console.WriteLine("File cannot be initialsed from old parameters file since required db nodes do not exist there"); throw new FileNotFoundException(@"XML database not found, and no backup found"); } } else { xmlParameters.Load(FileLocations.backupXMLDatabase()); xmlParameters.Save(FileLocations.xmlDatabase()); Console.WriteLine("XML parameters restored from backup"); } } xmlParameters.Load(FileLocations.xmlParameters()); XmlNode root = xmlParameters.DocumentElement; XmlNode botTokenNode = root.SelectSingleNode("descendant::BotToken"); XmlNode spamTimerNode; XmlNode spamMessageCountNode; token = botTokenNode.InnerText; prefixNode = CoOpGlobal.XML.findOrCreateChild(xmlParameters, root, "PrefixChar", "!"); spamTimerNode = CoOpGlobal.XML.findOrCreateChild(xmlParameters, root, "SpamTimer", "8"); spamMessageCountNode = CoOpGlobal.XML.findOrCreateChild(xmlParameters, root, "SpamMessageCount", "3"); CoOpGlobal.prefixCharacter = Convert.ToChar(prefixNode.InnerText); spamTimer = int.Parse(spamTimerNode.InnerText); spamMessageCount = int.Parse(spamMessageCountNode.InnerText); }
/************************************************ * * Separated functions * (Functions not directly usable in discord) * ************************************************/ private async Task <int> CountMessage(SocketGuildUser messageSender, SocketGuild guild, ISocketMessageChannel channel, int changeAmount, int delaySeconds = 0) { int messageCount; await Task.Delay(delaySeconds * 1000); if (userRecentMessageCounter[messageSender.Username] == null) { userRecentMessageCounter[messageSender.Username] = 0.ToString(); } messageCount = int.Parse(userRecentMessageCounter[messageSender.Username]) + changeAmount; userRecentMessageCounter[messageSender.Username] = messageCount.ToString(); if (changeAmount == 1) { xmlParameters.Load(FileLocations.xmlParameters()); XmlNode root = xmlParameters.DocumentElement; XmlNode spamMessageCountNode = CoOpGlobal.XML.findOrCreateChild(xmlParameters, root, "SpamMessageCount", "3"); spamMessageCount = int.Parse(spamMessageCountNode.InnerText); if (messageCount == spamMessageCount) { CoOpBot.Modules.Admin.RolesModule roleModule = new Modules.Admin.RolesModule(); await channel.SendMessageAsync("#StopCamSpam"); List <IRole> roleList = new List <IRole>(); List <ulong> userList = new List <ulong>(); // Check if the Muted role exists (case sensitve [I think]) foreach (IRole curRole in guild.Roles) { if (curRole.Name == "Muted") { roleList.Add(curRole); } } userList.Add(messageSender.Id); // Mute the user if (roleList.Count == 1 && userList.Count == 1) { try { await roleModule.RoleAddUsers(guild, userList, roleList); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } } else if (changeAmount == -1 && messageCount == 0) { CoOpBot.Modules.Admin.RolesModule roleModule = new Modules.Admin.RolesModule(); List <IRole> roleList = new List <IRole>(); List <ulong> userList = new List <ulong>(); // Check if the Muted role exists (case sensitve [I think]) foreach (IRole curRole in guild.Roles) { if (curRole.Name == "Muted") { roleList.Add(curRole); } } userList.Add(messageSender.Id); // Unmute the user if (roleList.Count == 1 && userList.Count == 1) { try { await roleModule.RoleRemoveUsers(guild, userList, roleList); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } return(messageCount); }