Beispiel #1
0
        public static string gwItemNames()
        {
            string filename = @"\gwItemNames.xml";

            if (!File.Exists(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + filename))
            {
                if (!File.Exists(FileLocations.gwItemNamesBackup()))
                {
                    XmlDocument gwItems = new XmlDocument();
                    gwItems.CreateXmlDeclaration("1.0", "UTF-8", "yes");
                    XmlElement root = gwItems.CreateElement("gwItemNames");

                    gwItems.AppendChild(root);
                    gwItems.Save(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + filename);
                }
                else
                {
                    XmlDocument gwItems = new XmlDocument();
                    gwItems.Load(FileLocations.gwItemNamesBackup());
                    gwItems.Save(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + filename);

                    Console.WriteLine("GW item names file restored from backup");
                }
            }
            return(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + filename);
        }
Beispiel #2
0
        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); });
                }
            };
        }
Beispiel #3
0
            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);
            }
Beispiel #4
0
        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);
        }
Beispiel #5
0
        /************************************************
        *
        * 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);
        }