Example #1
0
        public static async Task AssignNewRoles(SocketGuild guild, Dictionary <SocketGuildUser, IList <object> > users)
        {
            foreach (KeyValuePair <SocketGuildUser, IList <object> > user in users)
            {
                List <string> allRoles = await SheetsFunctionality.GetRoles(user.Value, user.Key);

                List <SocketRole> mappedRoles = new List <SocketRole>();
                foreach (string s in allRoles)
                {
                    SocketRole role = guild.Roles.FirstOrDefault(x => x.Name == s);
                    mappedRoles.Add(role);
                }

                await SheetsFunctionality.AddRolesToUser(user.Key, mappedRoles.ToArray());
            }
        }
Example #2
0
        private static async Task AssignRoles(DiscordSocketClient client, IList <IList <Object> > values)
        {
            Stopwatch time = Stopwatch.StartNew();

            if (values != null && values.Count > 0)
            {
                foreach (SocketGuild g in client.Guilds) // Updates for every guild the bot is registered in. Take note that this will quickly hit a discord limit. This is fine, it will resume after a few seconds.
                {
                    Dictionary <SocketGuildUser, IList <object> > redoUsers = new Dictionary <SocketGuildUser, IList <object> >();

                    Console.WriteLine("\n\nUpdating roles in " + g.Name + ".");
                    await g.DownloadUsersAsync();                   // Gets all users

                    SocketGuildUser[] allUsers = g.Users.ToArray(); // Converts list of users to array

                    foreach (SocketGuildUser u in allUsers)
                    {
                        bool change = false;
                        // Checking roles for user
                        foreach (IList <object> row in values)
                        {
                            if (!SheetsFunctionality.FindUsername(u, row))
                            {
                                continue;
                            }

                            //await SheetsFunctionality.StoreUserID(u);
                            if (Config.GoogleData.NicknameOnly)
                            {
                                Console.WriteLine("Updating Nickname for " + u.Username + "#" + u.Discriminator);
                                change = await SheetsFunctionality.FindAndSetNickname(u, row);

                                break;
                            }

                            Console.WriteLine("Updating Roles for " + u.Username + "#" + u.Discriminator);
                            List <string> allUserRoles = new List <string>(); // All of the rolls that need to be assigned to the user

                            // Gets all roles that need to be assigned to the user in addition to removing those that interfere with roleGroups.json
                            allUserRoles = await SheetsFunctionality.GetRoles(row, u);

                            if (Config.Bot.AutoRole != "")
                            {
                                allUserRoles.Add(Config.Bot.AutoRole);
                            }

                            List <SocketRole> formattedRoles = new List <SocketRole>();

                            bool redo = false;
                            // Google Sheets data to SocketRole
                            foreach (string s in allUserRoles)
                            {
                                SocketRole role = g.Roles.FirstOrDefault(x => x.Name == s);
                                if (role == default(SocketRole))
                                {
                                    role = await SheetsFunctionality.CreateRole(g, s);

                                    redo   = true;
                                    change = true;
                                }
                                // Don't add the role if the user already has it.
                                if (u.Roles.Contains(role))
                                {
                                    continue;
                                }
                                change = true;
                                formattedRoles.Add(role); // Adds role to list of queued roles
                            }

                            // Add user to list of roles to redo
                            if (redo)
                            {
                                redoUsers.Add(u, row);
                            }

                            // Add Roles To User
                            await SheetsFunctionality.AddRolesToUser(u, formattedRoles.ToArray());

                            // Find and set nickname
                            await SheetsFunctionality.FindAndSetNickname(u, row);
                        }

                        // Secondary Role Assigner for roles that were just created
                        await AssignNewRoles(g, redoUsers);

                        if (change)
                        {
                            await DiscordSpecificHandler.VerifyUser(g, u);
                        }
                    }
                }
            }
            time.Stop();
            Console.WriteLine(time.ElapsedMilliseconds + "ms\n");
        }