コード例 #1
0
ファイル: Program.cs プロジェクト: Sammca/CoOpBot
        /************************************************
        *
        * 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);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Sammca/CoOpBot
        private async static Task <bool> HandleTimer(DiscordSocketClient client)
        {
            try
            {
                IReadOnlyCollection <SocketGuild> guilds;

                guilds = client.Guilds;

                foreach (SocketGuild curGuild in guilds)
                {
                    IReadOnlyCollection <SocketGuildUser> users;
                    users = curGuild.Users;
                    foreach (SocketGuildUser curUser in users)
                    {
                        if (curUser.Activity.Type == Discord.ActivityType.Playing && !curUser.IsBot)
                        {
                            IRole  gameRole;
                            string gameName;
                            IReadOnlyCollection <SocketRole>  userRoles;
                            CoOpBot.Modules.Admin.RolesModule roleModule = new Modules.Admin.RolesModule();
                            Boolean          userHasRole = false;
                            List <IRole>     roleList    = new List <IRole>();
                            List <ulong>     userList    = new List <ulong>();
                            RoleTranslations rt          = new RoleTranslations();

                            gameName = curUser.Activity.Name;

                            rt = rt.find(gameName) as RoleTranslations;

                            if (rt != null)
                            {
                                gameName = rt.translateTo;
                            }

                            gameRole = roleModule.FindRoleFromName(gameName, curGuild);

                            if (gameRole == null)
                            {
                                return(true);

                                //await roleModule.RoleCreate(curGuild, gameName);

                                //gameRole = roleModule.FindRoleFromName(gameName, curGuild);
                            }

                            userRoles = curUser.Roles;

                            foreach (SocketRole curRole in userRoles)
                            {
                                if (curRole.Name == gameRole.Name)
                                {
                                    userHasRole = true;
                                    break;
                                }
                            }

                            if (!userHasRole)
                            {
                                roleList.Add(gameRole);
                                userList.Add(curUser.Id);
                                await roleModule.RoleAddUsers(curGuild, userList, roleList);
                            }
                        }
                    }
                }
            }
            catch (OperationCanceledException) { }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                return(true);
            }

            return(true);
        }