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 Newtonsoft.Json;
|
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
|
2017-10-02 21:57:48 +02:00
|
|
|
|
namespace Geekbot.net.Commands
|
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")]
|
|
|
|
|
public async Task getRandomQuote()
|
|
|
|
|
{
|
|
|
|
|
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];
|
2017-09-28 18:55:57 +02:00
|
|
|
|
var quote = JsonConvert.DeserializeObject<QuoteObject>(randomQuote);
|
2018-01-21 16:01:43 +01: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")]
|
|
|
|
|
public async Task saveQuote([Summary("@user")] IUser user)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
2017-09-28 18:55:57 +02:00
|
|
|
|
var lastMessage = await getLastMessageByUser(user);
|
2017-09-29 00:18:31 +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);
|
2017-09-29 20:30:00 +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")]
|
|
|
|
|
public async Task saveQuote([Summary("messageId")] ulong messageId)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
2017-09-29 00:18:31 +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);
|
2017-09-29 20:30:00 +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")]
|
|
|
|
|
public async Task returnSpecifiedQuote([Summary("@user")] IUser user)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var lastMessage = await getLastMessageByUser(user);
|
2017-09-29 00:18:31 +02:00
|
|
|
|
var quote = createQuoteObject(lastMessage);
|
2017-09-28 18:55:57 +02:00
|
|
|
|
var embed = quoteBuilder(quote);
|
|
|
|
|
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")]
|
|
|
|
|
public async Task returnSpecifiedQuote([Summary("messageId")] ulong messageId)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var message = await Context.Channel.GetMessageAsync(messageId);
|
2017-09-29 00:18:31 +02:00
|
|
|
|
var quote = createQuoteObject(message);
|
2017-09-28 18:55:57 +02:00
|
|
|
|
var embed = quoteBuilder(quote);
|
|
|
|
|
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)")]
|
|
|
|
|
public async Task removeQuote([Summary("quoteId")] int id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var quotes = _redis.SetMembers($"{Context.Guild.Id}:Quotes");
|
|
|
|
|
var success = _redis.SetRemove($"{Context.Guild.Id}:Quotes", quotes[id - 1]);
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
2018-01-21 17:17:55 +01:00
|
|
|
|
var quote = JsonConvert.DeserializeObject<QuoteObject>(quotes[id - 1]);
|
|
|
|
|
var embed = quoteBuilder(quote);
|
|
|
|
|
await ReplyAsync($"**Removed #{id}**", false, embed.Build());
|
2018-01-21 16:01:43 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync($"I couldn't find a quote with that id :disappointed:");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
private async Task<IMessage> getLastMessageByUser(IUser user)
|
|
|
|
|
{
|
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-01-21 16:01:43 +01:00
|
|
|
|
private EmbedBuilder quoteBuilder(QuoteObject quote, int id = 0)
|
2017-09-28 18:55:57 +02:00
|
|
|
|
{
|
|
|
|
|
var user = Context.Client.GetUserAsync(quote.userId).Result;
|
|
|
|
|
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} | ";
|
|
|
|
|
eb.Title += $"{user.Username} @ {quote.time.Day}.{quote.time.Month}.{quote.time.Year}";
|
2017-09-29 00:18:31 +02:00
|
|
|
|
eb.Description = quote.quote;
|
2017-09-28 18:55:57 +02:00
|
|
|
|
eb.ThumbnailUrl = user.GetAvatarUrl();
|
2017-12-29 01:53:50 +01: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
|
|
|
|
|
|
|
|
|
private QuoteObject 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 QuoteObject
|
2017-09-29 00:18:31 +02:00
|
|
|
|
{
|
|
|
|
|
userId = message.Author.Id,
|
|
|
|
|
time = message.Timestamp.DateTime,
|
|
|
|
|
quote = message.Content,
|
|
|
|
|
image = image
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-09-28 18:55:57 +02:00
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
|
|
|
|
public class QuoteObject
|
|
|
|
|
{
|
2017-09-28 18:55:57 +02:00
|
|
|
|
public ulong userId { get; set; }
|
|
|
|
|
public string quote { get; set; }
|
2017-09-29 00:18:31 +02:00
|
|
|
|
public DateTime time { get; set; }
|
|
|
|
|
public string image { get; set; }
|
2017-09-28 18:55:57 +02:00
|
|
|
|
}
|
|
|
|
|
}
|