geekbot/Geekbot.net/Commands/Utils/Quote/Quote.cs

240 lines
8.8 KiB
C#
Raw Normal View History

2017-09-28 18:55:57 +02:00
using System;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
2018-05-06 02:43:23 +02:00
using Geekbot.net.Lib.Polyfills;
2017-09-28 18:55:57 +02:00
using Newtonsoft.Json;
using StackExchange.Redis;
namespace Geekbot.net.Commands.Utils.Quote
2017-09-28 18:55:57 +02:00
{
[Group("quote")]
public class Quote : ModuleBase
{
2018-01-21 16:01:43 +01:00
private readonly IErrorHandler _errorHandler;
private readonly IDatabase _redis;
2017-12-29 01:53:50 +01:00
public Quote(IDatabase redis, IErrorHandler errorHandler)
2017-09-28 18:55:57 +02:00
{
2018-01-21 16:01:43 +01:00
_redis = redis;
_errorHandler = errorHandler;
2017-09-28 18:55:57 +02:00
}
2017-12-29 01:53:50 +01:00
[Command]
2017-10-12 16:34:10 +02:00
[Remarks(CommandCategories.Quotes)]
2017-09-28 18:55:57 +02:00
[Summary("Return a random quoute from the database")]
2018-04-30 23:44:19 +02:00
public async Task GetRandomQuote()
2017-09-28 18:55:57 +02:00
{
try
{
2018-01-21 16:01:43 +01:00
var randomQuotes = _redis.SetMembers($"{Context.Guild.Id}:Quotes");
2018-05-06 02:43:23 +02:00
if (!randomQuotes.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>`");
return;
}
2018-01-21 16:01:43 +01:00
var randomNumber = new Random().Next(randomQuotes.Length - 1);
var randomQuote = randomQuotes[randomNumber];
var quote = JsonConvert.DeserializeObject<QuoteObjectDto>(randomQuote);
2018-04-30 23:44:19 +02:00
var embed = QuoteBuilder(quote, randomNumber + 1);
2017-09-28 18:55:57 +02:00
await ReplyAsync("", false, embed.Build());
}
catch (Exception e)
{
2018-01-21 16:01:43 +01:00
_errorHandler.HandleCommandException(e, Context, "Whoops, seems like the quote was to edgy to return");
2017-09-28 18:55:57 +02:00
}
}
2017-12-29 01:53:50 +01:00
2017-09-28 18:55:57 +02:00
[Command("save")]
2017-10-12 16:34:10 +02:00
[Remarks(CommandCategories.Quotes)]
2017-09-28 18:55:57 +02:00
[Summary("Save a quote from the last sent message by @user")]
2018-04-30 23:44:19 +02:00
public async Task SaveQuote([Summary("@user")] IUser user)
2017-09-28 18:55:57 +02:00
{
try
{
if (user.Id == Context.Message.Author.Id)
{
await ReplyAsync("You can't save your own quotes...");
return;
}
2017-12-29 01:53:50 +01:00
if (user.IsBot)
{
await ReplyAsync("You can't save quotes by a bot...");
return;
}
2017-12-29 01:53:50 +01:00
2018-04-30 23:44:19 +02:00
var lastMessage = await GetLastMessageByUser(user);
2018-05-06 02:43:23 +02:00
if (lastMessage == null) return;
2018-04-30 23:44:19 +02:00
var quote = CreateQuoteObject(lastMessage);
2017-09-28 18:55:57 +02:00
var quoteStore = JsonConvert.SerializeObject(quote);
2018-01-21 16:01:43 +01:00
_redis.SetAdd($"{Context.Guild.Id}:Quotes", quoteStore);
2018-04-30 23:44:19 +02:00
var embed = QuoteBuilder(quote);
await ReplyAsync("**Quote Added**", false, embed.Build());
2017-09-28 18:55:57 +02:00
}
catch (Exception e)
{
2018-01-21 16:01:43 +01:00
_errorHandler.HandleCommandException(e, Context,
2017-12-29 01:53:50 +01:00
"I counldn't find a quote from that user :disappointed:");
2017-09-28 18:55:57 +02:00
}
}
2017-12-29 01:53:50 +01:00
2017-09-28 18:55:57 +02:00
[Command("save")]
2017-10-12 16:34:10 +02:00
[Remarks(CommandCategories.Quotes)]
2017-09-28 18:55:57 +02:00
[Summary("Save a quote from a message id")]
2018-04-30 23:44:19 +02:00
public async Task SaveQuote([Summary("messageId")] ulong messageId)
2017-09-28 18:55:57 +02:00
{
try
{
var message = await Context.Channel.GetMessageAsync(messageId);
if (message.Author.Id == Context.Message.Author.Id)
{
await ReplyAsync("You can't save your own quotes...");
return;
}
2017-12-29 01:53:50 +01:00
if (message.Author.IsBot)
{
await ReplyAsync("You can't save quotes by a bot...");
return;
}
2017-12-29 01:53:50 +01:00
2018-04-30 23:44:19 +02:00
var quote = CreateQuoteObject(message);
2017-09-28 18:55:57 +02:00
var quoteStore = JsonConvert.SerializeObject(quote);
2018-01-21 16:01:43 +01:00
_redis.SetAdd($"{Context.Guild.Id}:Quotes", quoteStore);
2018-04-30 23:44:19 +02:00
var embed = QuoteBuilder(quote);
await ReplyAsync("**Quote Added**", false, embed.Build());
2017-09-28 18:55:57 +02:00
}
catch (Exception e)
{
2018-01-21 16:01:43 +01:00
_errorHandler.HandleCommandException(e, Context,
2017-12-29 01:53:50 +01:00
"I couldn't find a message with that id :disappointed:");
2017-09-28 18:55:57 +02:00
}
}
2017-12-29 01:53:50 +01:00
2017-09-28 18:55:57 +02:00
[Command("make")]
2017-10-12 16:34:10 +02:00
[Remarks(CommandCategories.Quotes)]
2017-09-28 18:55:57 +02:00
[Summary("Create a quote from the last sent message by @user")]
2018-04-30 23:44:19 +02:00
public async Task ReturnSpecifiedQuote([Summary("@user")] IUser user)
2017-09-28 18:55:57 +02:00
{
try
{
2018-04-30 23:44:19 +02:00
var lastMessage = await GetLastMessageByUser(user);
2018-05-06 02:43:23 +02:00
if (lastMessage == null) return;
2018-04-30 23:44:19 +02:00
var quote = CreateQuoteObject(lastMessage);
var embed = QuoteBuilder(quote);
2017-09-28 18:55:57 +02:00
await ReplyAsync("", false, embed.Build());
}
catch (Exception e)
{
2018-01-21 16:01:43 +01:00
_errorHandler.HandleCommandException(e, Context,
2017-12-29 01:53:50 +01:00
"I counldn't find a quote from that user :disappointed:");
2017-09-28 18:55:57 +02:00
}
}
2017-12-29 01:53:50 +01:00
2017-09-28 18:55:57 +02:00
[Command("make")]
2017-10-12 16:34:10 +02:00
[Remarks(CommandCategories.Quotes)]
2017-09-28 18:55:57 +02:00
[Summary("Create a quote from a message id")]
2018-04-30 23:44:19 +02:00
public async Task ReturnSpecifiedQuote([Summary("messageId")] ulong messageId)
2017-09-28 18:55:57 +02:00
{
try
{
var message = await Context.Channel.GetMessageAsync(messageId);
2018-04-30 23:44:19 +02:00
var quote = CreateQuoteObject(message);
var embed = QuoteBuilder(quote);
2017-09-28 18:55:57 +02:00
await ReplyAsync("", false, embed.Build());
}
catch (Exception e)
{
2018-01-21 16:01:43 +01:00
_errorHandler.HandleCommandException(e, Context,
2017-12-29 01:53:50 +01:00
"I couldn't find a message with that id :disappointed:");
2017-09-28 18:55:57 +02:00
}
}
2018-01-21 16:01:43 +01:00
[Command("remove")]
[RequireUserPermission(GuildPermission.KickMembers)]
[RequireUserPermission(GuildPermission.ManageMessages)]
[RequireUserPermission(GuildPermission.ManageRoles)]
[Remarks(CommandCategories.Quotes)]
[Summary("Remove a quote (required mod permissions)")]
2018-04-30 23:44:19 +02:00
public async Task RemoveQuote([Summary("quoteId")] int id)
2018-01-21 16:01:43 +01:00
{
try
{
var quotes = _redis.SetMembers($"{Context.Guild.Id}:Quotes");
var success = _redis.SetRemove($"{Context.Guild.Id}:Quotes", quotes[id - 1]);
if (success)
{
var quote = JsonConvert.DeserializeObject<QuoteObjectDto>(quotes[id - 1]);
2018-04-30 23:44:19 +02:00
var embed = QuoteBuilder(quote);
2018-01-21 17:17:55 +01:00
await ReplyAsync($"**Removed #{id}**", false, embed.Build());
2018-01-21 16:01:43 +01:00
}
else
{
2018-04-30 23:44:19 +02:00
await ReplyAsync("I couldn't find a quote with that id :disappointed:");
2018-01-21 16:01:43 +01:00
}
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context,
"I couldn't find a quote with that id :disappointed:");
}
}
2017-09-28 18:55:57 +02:00
2018-04-30 23:44:19 +02:00
private async Task<IMessage> GetLastMessageByUser(IUser user)
2017-09-28 18:55:57 +02:00
{
2018-05-06 02:43:23 +02:00
try
{
var list = Context.Channel.GetMessagesAsync().Flatten();
await list;
return list.Result
.First(msg => msg.Author.Id == user.Id
&& msg.Embeds.Count == 0
&& msg.Id != Context.Message.Id
&& !msg.Content.ToLower().StartsWith("!"));
}
catch
{
await ReplyAsync($"No quoteable message have been sent by {user.Username} in this channel");
return null;
}
2017-09-28 18:55:57 +02:00
}
2017-12-29 01:53:50 +01:00
private EmbedBuilder QuoteBuilder(QuoteObjectDto quote, int id = 0)
2017-09-28 18:55:57 +02:00
{
2018-05-06 02:43:23 +02:00
var user = Context.Client.GetUserAsync(quote.UserId).Result ?? new UserPolyfillDto { Username = "Unknown User" };
2017-09-28 18:55:57 +02:00
var eb = new EmbedBuilder();
eb.WithColor(new Color(143, 167, 232));
2018-01-21 16:01:43 +01:00
eb.Title = id == 0 ? "" : $"#{id} | ";
2018-04-30 23:44:19 +02:00
eb.Title += $"{user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}";
eb.Description = quote.Quote;
2017-09-28 18:55:57 +02:00
eb.ThumbnailUrl = user.GetAvatarUrl();
2018-04-30 23:44:19 +02:00
if (quote.Image != null) eb.ImageUrl = quote.Image;
2017-09-28 18:55:57 +02:00
return eb;
}
private QuoteObjectDto CreateQuoteObject(IMessage message)
{
string image;
try
{
image = message.Attachments.First().Url;
}
catch (Exception)
{
image = null;
}
2017-12-29 01:53:50 +01:00
return new QuoteObjectDto
{
2018-04-30 23:44:19 +02:00
UserId = message.Author.Id,
Time = message.Timestamp.DateTime,
Quote = message.Content,
Image = image
};
}
2017-09-28 18:55:57 +02:00
}
}