Example #1
0
        public async Task ColorMe(string color)
        {
            // Check if this guild supports ColorMe.
            if (!guildsDefinition.GetSettings(Context.Guild.Id).colorMe)
            {
                await Context.Channel.SendMessageAsync("ColorMe is not enabled for this server.");

                return;
            }

            string roleName = ("colorme" + Context.User.Id);

            // Check if the color is valid.
            Discord.Color roleColor = new Discord.Color();
            try
            {
                roleColor = new Discord.Color(GetRawColor(color));
            }
            catch
            {
                await Context.Channel.SendMessageAsync("Invalid hex code.");

                return;
            }
            // Check if the user already has a color role.
            IRole colorRole = Context.Guild.Roles.FirstOrDefault(x => x.Name == roleName);
            // Get where this role should go so it works, which is right above their current role.
            int highestRole = Context.Guild.GetUser(Context.User.Id).Roles.LastOrDefault(x => x.Name != roleName).Position + 1;

            // Create the role if needed.
            if (colorRole == null)
            {
                colorRole = await Context.Guild.CreateRoleAsync(roleName, null, roleColor, false, null);

                await colorRole.ModifyAsync(new Action <RoleProperties>(x => x.Position = highestRole));
            }
            else
            {
                await colorRole.ModifyAsync(new Action <RoleProperties>(x => x.Color = roleColor));
            }

            // If the user doesn't have the color role assigned, add it to them.
            if (Context.Guild.GetUser(Context.User.Id).Roles.FirstOrDefault(x => x.Name == roleName) == null)
            {
                try
                {
                    await Context.Guild.GetUser(Context.User.Id).AddRolesAsync(new List <IRole>()
                    {
                        colorRole
                    });
                }catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            await Context.Channel.SendMessageAsync($"Assigned color {roleColor.ToString()} to {Context.User.Username}.");
        }