Quotes now have image support and a new look

This commit is contained in:
Runebaas 2017-09-29 00:18:31 +02:00
parent 0b680d7fb2
commit 9f4e23ff20
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
2 changed files with 38 additions and 32 deletions

View file

@ -50,12 +50,7 @@ namespace Geekbot.net.Modules
try try
{ {
var lastMessage = await getLastMessageByUser(user); var lastMessage = await getLastMessageByUser(user);
var quote = new QuoteObject() var quote = createQuoteObject(lastMessage);
{
userId = user.Id,
time = DateTime.Now,
quote = lastMessage.Content
};
var quoteStore = JsonConvert.SerializeObject(quote); var quoteStore = JsonConvert.SerializeObject(quote);
redis.SetAdd($"{Context.Guild.Id}:Quotes", quoteStore); redis.SetAdd($"{Context.Guild.Id}:Quotes", quoteStore);
await ReplyAsync("Quote Added"); await ReplyAsync("Quote Added");
@ -73,12 +68,7 @@ namespace Geekbot.net.Modules
try try
{ {
var message = await Context.Channel.GetMessageAsync(messageId); var message = await Context.Channel.GetMessageAsync(messageId);
var quote = new QuoteObject() var quote = createQuoteObject(message);
{
userId = message.Author.Id,
time = DateTime.Now,
quote = message.Content
};
var quoteStore = JsonConvert.SerializeObject(quote); var quoteStore = JsonConvert.SerializeObject(quote);
redis.SetAdd($"{Context.Guild.Id}:Quotes", quoteStore); redis.SetAdd($"{Context.Guild.Id}:Quotes", quoteStore);
await ReplyAsync("Quote Added"); await ReplyAsync("Quote Added");
@ -96,12 +86,7 @@ namespace Geekbot.net.Modules
try try
{ {
var lastMessage = await getLastMessageByUser(user); var lastMessage = await getLastMessageByUser(user);
var quote = new QuoteObject() var quote = createQuoteObject(lastMessage);
{
userId = user.Id,
time = DateTime.Now,
quote = lastMessage.Content
};
var embed = quoteBuilder(quote); var embed = quoteBuilder(quote);
await ReplyAsync("", false, embed.Build()); await ReplyAsync("", false, embed.Build());
} }
@ -118,12 +103,7 @@ namespace Geekbot.net.Modules
try try
{ {
var message = await Context.Channel.GetMessageAsync(messageId); var message = await Context.Channel.GetMessageAsync(messageId);
var quote = new QuoteObject() var quote = createQuoteObject(message);
{
userId = message.Author.Id,
time = DateTime.Now,
quote = message.Content
};
var embed = quoteBuilder(quote); var embed = quoteBuilder(quote);
await ReplyAsync("", false, embed.Build()); await ReplyAsync("", false, embed.Build());
} }
@ -137,27 +117,53 @@ namespace Geekbot.net.Modules
{ {
Task<IEnumerable<IMessage>> list = Context.Channel.GetMessagesAsync().Flatten(); Task<IEnumerable<IMessage>> list = Context.Channel.GetMessagesAsync().Flatten();
await list; await list;
return list.Result.Where(msg => return list.Result
msg.Author.Id == user.Id .First(msg => msg.Author.Id == user.Id
&& msg.Embeds.Count == 0 && msg.Embeds.Count == 0
&& msg.Id != Context.Message.Id) && msg.Id != Context.Message.Id
.First(); && !msg.Content.ToLower().StartsWith("!"));
} }
private EmbedBuilder quoteBuilder(QuoteObject quote) private EmbedBuilder quoteBuilder(QuoteObject quote)
{ {
var user = Context.Client.GetUserAsync(quote.userId).Result; var user = Context.Client.GetUserAsync(quote.userId).Result;
var eb = new EmbedBuilder(); var eb = new EmbedBuilder();
eb.WithColor(new Color(143, 167, 232));
eb.Title = $"{user.Username} @ {quote.time.Day}.{quote.time.Month}.{quote.time.Year}";
eb.Description = quote.quote;
eb.ThumbnailUrl = user.GetAvatarUrl(); eb.ThumbnailUrl = user.GetAvatarUrl();
eb.AddField(quote.quote, $"-- {user.Username} - {quote.time.Day}.{quote.time.Month}.{quote.time.Year}"); if (quote.image != null)
{
eb.ImageUrl = quote.image;
}
return eb; return eb;
} }
private QuoteObject createQuoteObject(IMessage message)
{
string image;
try
{
image = message.Attachments.First().Url;
}
catch (Exception)
{
image = null;
}
return new QuoteObject()
{
userId = message.Author.Id,
time = message.Timestamp.DateTime,
quote = message.Content,
image = image
};
}
} }
public class QuoteObject { public class QuoteObject {
public ulong userId { get; set; } public ulong userId { get; set; }
public DateTime time { get; set; }
public string quote { get; set; } public string quote { get; set; }
public DateTime time { get; set; }
public string image { get; set; }
} }
} }

View file

@ -238,7 +238,7 @@ namespace Geekbot.net
return Task.CompletedTask; return Task.CompletedTask;
} }
// temporary db migration script // db migration script
private Task MigrateDatabaseToHash() private Task MigrateDatabaseToHash()
{ {
foreach (var key in redis.Multiplexer.GetServer("127.0.0.1", 6379).Keys(6)) foreach (var key in redis.Multiplexer.GetServer("127.0.0.1", 6379).Keys(6))