Save quotes to the database after adding them
This commit is contained in:
parent
510b030fec
commit
177942f7fe
2 changed files with 46 additions and 33 deletions
|
@ -33,18 +33,18 @@ namespace Geekbot.net.Commands.Utils.Quote
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var s = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id));
|
var s = _database.Quotes.OrderBy(e => e.GuildId).Where(e => e.GuildId.Equals(Context.Guild.Id)).ToList();
|
||||||
var totalQuotes = s.Count();
|
|
||||||
|
|
||||||
if (totalQuotes > 0)
|
if (!s.Any())
|
||||||
{
|
{
|
||||||
await ReplyAsync("This server doesn't seem to have any quotes yet. You can add a quote with `!quote save @user` or `!quote save <messageId>`");
|
await ReplyAsync("This server doesn't seem to have any quotes yet. You can add a quote with `!quote save @user` or `!quote save <messageId>`");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var quote = s.Where(e => e.Id.Equals(new Random().Next(totalQuotes)))?.FirstOrDefault();
|
|
||||||
|
var random = new Random().Next(1, s.Count());
|
||||||
|
var quote = s[random - 1];
|
||||||
|
|
||||||
|
var embed = QuoteBuilder(quote);
|
||||||
var embed = QuoteBuilder(quote, 2);
|
|
||||||
await ReplyAsync("", false, embed.Build());
|
await ReplyAsync("", false, embed.Build());
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
@ -76,7 +76,8 @@ namespace Geekbot.net.Commands.Utils.Quote
|
||||||
if (lastMessage == null) return;
|
if (lastMessage == null) return;
|
||||||
|
|
||||||
var quote = CreateQuoteObject(lastMessage);
|
var quote = CreateQuoteObject(lastMessage);
|
||||||
await _database.Quotes.AddAsync(quote);
|
_database.Quotes.Add(quote);
|
||||||
|
_database.SaveChanges();
|
||||||
|
|
||||||
var embed = QuoteBuilder(quote);
|
var embed = QuoteBuilder(quote);
|
||||||
await ReplyAsync("**Quote Added**", false, embed.Build());
|
await ReplyAsync("**Quote Added**", false, embed.Build());
|
||||||
|
@ -109,7 +110,8 @@ namespace Geekbot.net.Commands.Utils.Quote
|
||||||
}
|
}
|
||||||
|
|
||||||
var quote = CreateQuoteObject(message);
|
var quote = CreateQuoteObject(message);
|
||||||
await _database.Quotes.AddAsync(quote);
|
_database.Quotes.Add(quote);
|
||||||
|
_database.SaveChanges();
|
||||||
|
|
||||||
var embed = QuoteBuilder(quote);
|
var embed = QuoteBuilder(quote);
|
||||||
await ReplyAsync("**Quote Added**", false, embed.Build());
|
await ReplyAsync("**Quote Added**", false, embed.Build());
|
||||||
|
@ -168,27 +170,25 @@ namespace Geekbot.net.Commands.Utils.Quote
|
||||||
[Summary("Remove a quote (required mod permissions)")]
|
[Summary("Remove a quote (required mod permissions)")]
|
||||||
public async Task RemoveQuote([Summary("quoteId")] int id)
|
public async Task RemoveQuote([Summary("quoteId")] int id)
|
||||||
{
|
{
|
||||||
await ReplyAsync("Command currently Disabled");
|
try
|
||||||
// try
|
{
|
||||||
// {
|
var quote = _database.Quotes.Where(e => e.GuildId == Context.Guild.Id && e.InternalId == id)?.FirstOrDefault();
|
||||||
// var quotes = _redis.SetMembers($"{Context.Guild.Id}:Quotes");
|
if (quote != null)
|
||||||
// var success = _redis.SetRemove($"{Context.Guild.Id}:Quotes", quotes[id - 1]);
|
{
|
||||||
// if (success)
|
_database.Quotes.Remove(quote);
|
||||||
// {
|
_database.SaveChanges();
|
||||||
// var quote = JsonConvert.DeserializeObject<QuoteObjectDto>(quotes[id - 1]);
|
var embed = QuoteBuilder(quote);
|
||||||
// var embed = QuoteBuilder(quote);
|
await ReplyAsync($"**Removed #{id}**", false, embed.Build());
|
||||||
// await ReplyAsync($"**Removed #{id}**", false, embed.Build());
|
}
|
||||||
// }
|
else
|
||||||
// else
|
{
|
||||||
// {
|
await ReplyAsync("I couldn't find a quote with that id :disappointed:");
|
||||||
// await ReplyAsync("I couldn't find a quote with that id :disappointed:");
|
}
|
||||||
// }
|
}
|
||||||
// }
|
catch (Exception e)
|
||||||
// catch (Exception e)
|
{
|
||||||
// {
|
_errorHandler.HandleCommandException(e, Context, "I couldn't find a quote with that id :disappointed:");
|
||||||
// _errorHandler.HandleCommandException(e, Context,
|
}
|
||||||
// "I couldn't find a quote with that id :disappointed:");
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<IMessage> GetLastMessageByUser(IUser user)
|
private async Task<IMessage> GetLastMessageByUser(IUser user)
|
||||||
|
@ -210,13 +210,12 @@ namespace Geekbot.net.Commands.Utils.Quote
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private EmbedBuilder QuoteBuilder(QuoteModel quote, int id = 0)
|
private EmbedBuilder QuoteBuilder(QuoteModel quote)
|
||||||
{
|
{
|
||||||
var user = Context.Client.GetUserAsync(quote.UserId).Result ?? new UserPolyfillDto { Username = "Unknown User" };
|
var user = Context.Client.GetUserAsync(quote.UserId).Result ?? new UserPolyfillDto { Username = "Unknown User" };
|
||||||
var eb = new EmbedBuilder();
|
var eb = new EmbedBuilder();
|
||||||
eb.WithColor(new Color(143, 167, 232));
|
eb.WithColor(new Color(143, 167, 232));
|
||||||
eb.Title = id == 0 ? "" : $"#{id} | ";
|
eb.Title = $"#{quote.InternalId} | {user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}";
|
||||||
eb.Title += $"{user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}";
|
|
||||||
eb.Description = quote.Quote;
|
eb.Description = quote.Quote;
|
||||||
eb.ThumbnailUrl = user.GetAvatarUrl();
|
eb.ThumbnailUrl = user.GetAvatarUrl();
|
||||||
if (quote.Image != null) eb.ImageUrl = quote.Image;
|
if (quote.Image != null) eb.ImageUrl = quote.Image;
|
||||||
|
@ -235,8 +234,10 @@ namespace Geekbot.net.Commands.Utils.Quote
|
||||||
image = null;
|
image = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var internalId = _database.Quotes.Count(e => e.GuildId == Context.Guild.Id);
|
||||||
return new QuoteModel()
|
return new QuoteModel()
|
||||||
{
|
{
|
||||||
|
InternalId = internalId,
|
||||||
GuildId = Context.Guild.Id,
|
GuildId = Context.Guild.Id,
|
||||||
UserId = message.Author.Id,
|
UserId = message.Author.Id,
|
||||||
Time = message.Timestamp.DateTime,
|
Time = message.Timestamp.DateTime,
|
||||||
|
|
|
@ -8,10 +8,22 @@ namespace Geekbot.net.Database
|
||||||
{
|
{
|
||||||
[Key]
|
[Key]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public int InternalId { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
public ulong GuildId { get; set; }
|
public ulong GuildId { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
public ulong UserId { get; set; }
|
public ulong UserId { get; set; }
|
||||||
public string Quote { get; set; }
|
|
||||||
|
[Required]
|
||||||
|
[DataType(DataType.DateTime)]
|
||||||
public DateTime Time { get; set; }
|
public DateTime Time { get; set; }
|
||||||
|
|
||||||
|
public string Quote { get; set; }
|
||||||
|
|
||||||
public string Image { get; set; }
|
public string Image { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue