private static Task ColorPicker(SocketReaction reaction, ColorRole colorRole) { ColorRoleAnswer answer = colorRole.Answers.SingleOrDefault(e => e.AnswerEmoji.Equals(reaction.Emote)); if (reaction.UserId == colorRole.UserId) { var guild = (reaction.Channel as SocketGuildChannel).Guild as SocketGuild; var chan = guild.GetChannel(colorRole.ChannelId) as SocketTextChannel; var _user = guild.GetUser(colorRole.UserId); var botRole = guild.Roles.FirstOrDefault(e => e.Name == "Geometric"); if (answer.Answer == "Yes") { var _role = guild.Roles.FirstOrDefault(x => x.Name == $"USER-{colorRole.UserId}") as IRole; if (_role != null) { var role = _role; role.ModifyAsync(r => r.Color = new Color(colorRole.r, colorRole.g, colorRole.b)); role.ModifyAsync(r => r.Position = botRole.Position); _user.AddRoleAsync(role as IRole); chan.SendMessageAsync($"{_user.Mention} has been given role {role.Name}"); } else { var role = guild.CreateRoleAsync($"USER-{colorRole.UserId}", null, new Color(colorRole.r, colorRole.g, colorRole.b), false, false, null); role.Result.ModifyAsync(r => r.Position = botRole.Position); _user.AddRoleAsync(role.Result); chan.SendMessageAsync($"{_user.Mention} has been given role {role.Result.Name}"); } } else if (answer.Answer == "No") { chan.SendMessageAsync($"{_user.Mention} cancelled"); } if (answer != null && reaction.User.IsSpecified) { _ = colorRole.ReactionUsers.AddOrUpdate( answer, new List <IUser> { reaction.User.Value }, (_, list) => { list.Add(reaction.User.Value); return(list); } ); } } return(Task.CompletedTask); }
private static async Task ReactionEndedAsync(SocketCommandContext context, ColorRole colorRole) { if (colorRole == null) { return; } if (colorRole.ReactionUsers.IsEmpty) { _ = await context.Channel.SendMessageAsync("took too long").ConfigureAwait(false); } }
private static ReactionCallbackData GenerateColorRole(ColorRole colorRole) { var embedBuilder = new EmbedBuilder() .WithTitle($"Color Role") .WithColor(new Color(colorRole.r, colorRole.g, colorRole.b)) .WithDescription( $"Do you like this color? HEX: {colorRole.HexColor.ToUpper()} / RGB: {colorRole.r}, {colorRole.g}, {colorRole.b}" + Environment.NewLine + "React with your choice." ); var rcbd = new ReactionCallbackData("", embedBuilder.Build(), true, true, reactDuration, async c => await ReactionEndedAsync(c, colorRole).ConfigureAwait(false)); foreach (IEmote answerEmoji in colorRole.Answers.Select(x => x.AnswerEmoji)) { _ = rcbd.WithCallback(answerEmoji, (c, r) => ColorPicker(r, colorRole)); } return(rcbd); }
public async Task ColorRoleAsync([Summary("Hex Color")] string hexString = null) { if (hexString == null) { _ = await ReplyAsync("Provide a color, ex `ffffff`").ConfigureAwait(false); return; } int rVal, gVal, bVal = 0; rVal = int.Parse(hexString.Substring(0, 2), NumberStyles.AllowHexSpecifier); gVal = int.Parse(hexString.Substring(2, 2), NumberStyles.AllowHexSpecifier); bVal = int.Parse(hexString.Substring(4, 2), NumberStyles.AllowHexSpecifier); var emoteBank = Context.Client.GetGuild(690373608228519997); var colorRole = new ColorRole { GuildId = Context.Guild.Id, ChannelId = Context.Channel.Id, UserId = Context.User.Id, MessageId = Context.Message.Id, HexColor = hexString, r = rVal, g = gVal, b = bVal, ColorObj = new Color(rVal, gVal, bVal), Answers = new List <ColorRoleAnswer> { new ColorRoleAnswer("Yes", emoteBank.Emotes.First(e => e.Name == "yes")), //742446277916360724 new ColorRoleAnswer("No", emoteBank.Emotes.First(e => e.Name == "no")), //742446286245986364 } }; _ = await InlineReactionReplyAsync(GenerateColorRole(colorRole), false).ConfigureAwait(false); }