protected void AddOrRemoveAnnotation(UIElement element, Position position, AddOrRemove action) { StackPanel parent; switch (position) { case Position.Left: parent = AnnotationsLeft; if (element is FrameworkElement) { (element as FrameworkElement).HorizontalAlignment = HorizontalAlignment.Right; (element as FrameworkElement).VerticalAlignment = VerticalAlignment.Center; } break; case Position.Right: parent = AnnotationsRight; if (element is FrameworkElement) { (element as FrameworkElement).HorizontalAlignment = HorizontalAlignment.Left; (element as FrameworkElement).VerticalAlignment = VerticalAlignment.Center; } break; case Position.Top: parent = AnnotationsTop; if (element is FrameworkElement) { (element as FrameworkElement).HorizontalAlignment = HorizontalAlignment.Center; (element as FrameworkElement).VerticalAlignment = VerticalAlignment.Bottom; } break; default: parent = AnnotationsBottom; if (element is FrameworkElement) { (element as FrameworkElement).HorizontalAlignment = HorizontalAlignment.Center; (element as FrameworkElement).VerticalAlignment = VerticalAlignment.Top; } break; } if (action == AddOrRemove.Add) { parent.Children.Add(element); } else { parent.Children.Remove(element); } }
private async Task AddOrRemoveRoleAsync(AddOrRemove action, Cacheable <IUserMessage, ulong> cachedMessage, ISocketMessageChannel channel, SocketReaction reaction) { if (channel == null || !reaction.User.IsSpecified) { return; } var msg = cachedMessage.HasValue ? cachedMessage.Value : await cachedMessage.GetOrDownloadAsync().ConfigureAwait(false); if (!(msg.Channel is ITextChannel textChannel)) { return; } var guild = textChannel.Guild; if (guild == null) { return; } var user = reaction.User.Value; var emote = reaction.Emote; if (user.IsBot) { return; } var match = await dbContext.RoleButtonLinks.SingleOrDefaultAsync(x => x.GuildID == guild.Id && x.MessageID == msg.Id && x.EmoteString == emote.ToString()).ConfigureAwait(false); if (match != null) { var role = guild.GetRole(match.RoleID); var gUser = await guild.GetUserAsync(user.Id).ConfigureAwait(false); if (action == AddOrRemove.Add) { await gUser.AddRoleAsync(role).ConfigureAwait(false); await gUser.SendMessageAsync($"Role {role.Name} added").ConfigureAwait(false); } else { await gUser.RemoveRoleAsync(role).ConfigureAwait(false); await gUser.SendMessageAsync($"Role {role.Name} removed").ConfigureAwait(false); } } else if ((await dbContext.RoleButtonLinks.CountAsync(x => x.MessageID == msg.Id).ConfigureAwait(false)) > 0) // Remove all new reactions that were not added by Bot { await msg.RemoveReactionAsync(emote, user).ConfigureAwait(false); } }
} //Copies of books inputted by the user //Constructor //Uses a BookDetails window as a parameter and AddOrRemove enum /// <summary> /// Constructor for BookCopiesDialog /// Initializes fields and preps the Window for user interaction /// </summary> /// <param name="bookDetails">BookDetails window that opened this window</param> /// <param name="addOrRemove">AddorRemove enum that preps the interaction for adding or removing Book copies</param> public BookCopiesDialog(BookDetails bookDetails, AddOrRemove addOrRemove) { InitializeComponent(); //Initialize properties and fields Owner = bookDetails; this.addOrRemove = addOrRemove; //Set the QuestionTextBlock content if (addOrRemove == AddOrRemove.Add) //if user wants to add copies { QuestionTextBlock.Text = "How many copies would you like to add?"; } else if (addOrRemove == AddOrRemove.Remove) //if user wants to deletec copies { string question = string.Format("How many copies would you like to remove?\n\n({0} copies are available for removal)", (Owner as BookDetails).Book.CopiesAvailable); QuestionTextBlock.Text = question; } NumberTextBox.Text = "0"; ErrorLabel.Content = string.Empty; }
private async Task AddOrRemoveRoleAsync(AddOrRemove action, Cacheable <IUserMessage, ulong> cachedMessage, ISocketMessageChannel channel, SocketReaction reaction) { if (channel == null) { logger.LogDebug($"Error in {nameof(AddOrRemoveRoleAsync)} of {nameof(RoleButtonService)} - Channel was null"); return; } if (reaction == null) { logger.LogDebug($"Error in {nameof(AddOrRemoveRoleAsync)} of {nameof(RoleButtonService)} - Reaction was null"); return; } IEmote emote = reaction.Emote; if (!reaction.User.IsSpecified) { logger.LogDebug($"Error in {nameof(AddOrRemoveRoleAsync)} of {nameof(RoleButtonService)} - No user was specified in the reaction object"); return; } IUser user = reaction.User.Value; IUserMessage msg = cachedMessage.HasValue ? cachedMessage.Value : await cachedMessage.GetOrDownloadAsync().ConfigureAwait(false); if (msg == null) { logger.LogDebug($"Error in {nameof(AddOrRemoveRoleAsync)} of {nameof(RoleButtonService)} - Could not get the underlying message"); return; } if (!(msg.Channel is ITextChannel textChannel)) { logger.LogDebug($"Error in {nameof(AddOrRemoveRoleAsync)} of {nameof(RoleButtonService)} - message was not from a text channel"); return; } IGuild guild = textChannel.Guild; if (guild == null) { logger.LogDebug($"Error in {nameof(AddOrRemoveRoleAsync)} of {nameof(RoleButtonService)} - Guild was null"); return; } if (user.IsBot) { logger.LogDebug($"Error in {nameof(AddOrRemoveRoleAsync)} of {nameof(RoleButtonService)} - Reaction was triggered by a bot"); return; } RoleButtonLink match = await dbContext.RoleButtonLinks .AsQueryable() .SingleOrDefaultAsync(x => x.GuildID == guild.Id && x.MessageID == msg.Id && x.EmoteString == emote.ToString()) .ConfigureAwait(false); if (match != null) { IRole role = guild.GetRole(match.RoleID); IGuildUser gUser = await guild.GetUserAsync(user.Id).ConfigureAwait(false); if (action == AddOrRemove.Add) { await gUser.AddRoleAsync(role).ConfigureAwait(false); _ = await gUser.SendMessageAsync($"Role {role.Name} added").ConfigureAwait(false); } else { await gUser.RemoveRoleAsync(role).ConfigureAwait(false); _ = await gUser.SendMessageAsync($"Role {role.Name} removed").ConfigureAwait(false); } } else if (await dbContext.RoleButtonLinks .AsQueryable() .AnyAsync(x => x.MessageID == msg.Id)// Remove all new reactions that were not added by Bot .ConfigureAwait(false)) { await msg.RemoveReactionAsync(emote, user).ConfigureAwait(false); } }
protected void AddOrRemoveAnnotation(UIElement element, Position position, AddOrRemove action) { StackPanel parent; switch (position) { case Position.Left: parent = AnnotationsLeft; if (element is FrameworkElement) { (element as FrameworkElement).HorizontalAlignment = HorizontalAlignment.Right; (element as FrameworkElement).VerticalAlignment = VerticalAlignment.Center; } break; case Position.Right: parent = AnnotationsRight; if (element is FrameworkElement) { (element as FrameworkElement).HorizontalAlignment = HorizontalAlignment.Left; (element as FrameworkElement).VerticalAlignment = VerticalAlignment.Center; } break; case Position.Top: parent = AnnotationsTop; if (element is FrameworkElement) { (element as FrameworkElement).HorizontalAlignment = HorizontalAlignment.Center; (element as FrameworkElement).VerticalAlignment = VerticalAlignment.Bottom; } break; default: parent = AnnotationsBottom; if (element is FrameworkElement) { (element as FrameworkElement).HorizontalAlignment = HorizontalAlignment.Center; (element as FrameworkElement).VerticalAlignment = VerticalAlignment.Top; } break; } if (action == AddOrRemove.Add) parent.Children.Add(element); else parent.Children.Remove(element); }