private bool CreateCard() { try { using (var db = new CardContext()) { var message = Context.Channel.GetMessageAsync(MessageId).Result; db.Cards.Add(Card); db.SaveChanges(); message.Channel.SendMessageAsync($"{Card.Name} card has been created with a value of {Card.Value}."); } return(true); } catch (Exception e) { var server = Context.Guild.Channels.Where(c => c.Name == Commands.CardErrorChannel).FirstOrDefault(); if (null != server) { IMessageChannel channel = (IMessageChannel)server; channel.SendMessageAsync(e.Message); } Logger.Log(LogLevel.Error, e); return(false); } }
private bool ModifyValue() { try { var message = Context.Channel.GetMessageAsync(MessageId).Result; using (var db = new CardContext()) { var c = db.Cards .AsQueryable() .Where(card => card.Id == Card.Id) .FirstOrDefault(); if (null == c) { message.Channel.SendMessageAsync($"Cannot find {Card.Name} to modify it."); return(false); } c.Value = Card.Value; db.Update(c); db.SaveChanges(); message.Channel.SendMessageAsync($"{c.Name}'s new value is {c.Value}"); } return(true); } catch (Exception e) { var server = Context.Guild.Channels.Where(c => c.Name == Commands.CardErrorChannel).FirstOrDefault(); if (null != server) { IMessageChannel channel = (IMessageChannel)server; channel.SendMessageAsync(e.Message); } Logger.Log(LogLevel.Error, e); return(false); } }
private bool DeleteCard() { try { using (var db = new CardContext()) { var message = Context.Channel.GetMessageAsync(MessageId).Result; var c = db.Cards .AsQueryable() .Where(card => card.Id == Card.Id) .FirstOrDefault(); if (null == c) { message.Channel.SendMessageAsync($"Cannot find {Card.Name} to delete it."); return(false); } db.Cards.Remove(c); db.CardGivings.RemoveRange(db.CardGivings.AsQueryable() .Where(g => g.CardId == c.Id).ToArray()); db.SaveChanges(); message.Channel.SendMessageAsync($"{c.Name} card has been deleted."); } return(true); } catch (Exception e) { var server = Context.Guild.Channels.Where(c => c.Name == Commands.CardErrorChannel).FirstOrDefault(); if (null != server) { IMessageChannel channel = (IMessageChannel)server; channel.SendMessageAsync(e.Message); } Logger.Log(LogLevel.Error, e); return(false); } }
private bool ExecuteChallenge() { try { using (var db = new CardContext()) { var message = Context.Channel.GetMessageAsync(MessageId).Result; Cards c = null; if (null != Card) { c = db.Cards .AsQueryable() .Where(card => card.Id == Card.Id) .FirstOrDefault(); if (null == c) { message.Channel.SendMessageAsync($"Cannot find {Card.Name} to delete it."); return(false); } } var giving = db.CardGivings.AsQueryable() .Where(g => g.Id == CardGiving.Id) .FirstOrDefault(); if (null == giving) { message.Channel.SendMessageAsync($"Cannot find card {CardGiving.Id} to challenge."); return(false); } if (null == c) // deleting card { db.CardGivings.Remove(giving); } else // changing card { giving.Card = c; giving.CardId = c.Id; } db.SaveChanges(); message.Channel.SendMessageAsync($"{Context.User.Username}'s challenge has been executed."); } return(true); } catch (Exception e) { var server = Context.Guild.Channels.Where(c => c.Name == Commands.CardErrorChannel).FirstOrDefault(); if (null != server) { IMessageChannel channel = (IMessageChannel)server; channel.SendMessageAsync(e.Message); } Logger.Log(LogLevel.Error, e); return(false); } }