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;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.ErrorHandling;
|
2017-09-28 18:55:57 +02:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
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
|
|
|
|
|
2018-02-14 23:01:28 +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");
|
|
|
|
|
var randomNumber = new Random().Next(randomQuotes.Length - 1);
|
|
|
|
|
var randomQuote = randomQuotes[randomNumber];
|
2018-05-03 00:56:06 +02:00
|
|
|
|
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
|
|
|
|
|
{
|
2017-10-03 18:46:14 +02:00
|
|
|
|
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
|
|
|
|
|
2017-11-08 19:19:43 +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);
|
|
|
|
|
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);
|
2017-09-29 20:30:00 +02:00
|
|
|
|
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);
|
2017-10-03 18:46:14 +02:00
|
|
|
|
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
|
|
|
|
|
2017-11-08 19:19:43 +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);
|
2017-09-29 20:30:00 +02:00
|
|
|
|
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);
|
|
|
|
|
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)
|
|
|
|
|
{
|
2018-05-03 00:56:06 +02:00
|
|
|
|
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
|
|
|
|
{
|
2017-12-29 01:53:50 +01:00
|
|
|
|
var list = Context.Channel.GetMessagesAsync().Flatten();
|
2017-09-28 18:55:57 +02:00
|
|
|
|
await list;
|
2017-09-29 00:18:31 +02:00
|
|
|
|
return list.Result
|
2017-12-29 01:53:50 +01:00
|
|
|
|
.First(msg => msg.Author.Id == user.Id
|
|
|
|
|
&& msg.Embeds.Count == 0
|
|
|
|
|
&& msg.Id != Context.Message.Id
|
|
|
|
|
&& !msg.Content.ToLower().StartsWith("!"));
|
2017-09-28 18:55:57 +02:00
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
private EmbedBuilder QuoteBuilder(QuoteObjectDto quote, int id = 0)
|
2017-09-28 18:55:57 +02:00
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
var user = Context.Client.GetUserAsync(quote.UserId).Result;
|
2017-09-28 18:55:57 +02:00
|
|
|
|
var eb = new EmbedBuilder();
|
2017-09-29 00:18:31 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
2017-09-29 00:18:31 +02:00
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
private QuoteObjectDto CreateQuoteObject(IMessage message)
|
2017-09-29 00:18:31 +02:00
|
|
|
|
{
|
|
|
|
|
string image;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
image = message.Attachments.First().Url;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
image = null;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
return new QuoteObjectDto
|
2017-09-29 00:18:31 +02:00
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
UserId = message.Author.Id,
|
|
|
|
|
Time = message.Timestamp.DateTime,
|
|
|
|
|
Quote = message.Content,
|
|
|
|
|
Image = image
|
2017-09-29 00:18:31 +02:00
|
|
|
|
};
|
|
|
|
|
}
|
2017-09-28 18:55:57 +02:00
|
|
|
|
}
|
|
|
|
|
}
|