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

244 lines
8.7 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.Database;
using Geekbot.net.Database.Models;
using Geekbot.net.Lib.CommandPreconditions;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.Extensions;
2018-05-06 02:43:23 +02:00
using Geekbot.net.Lib.Polyfills;
2017-09-28 18:55:57 +02:00
namespace Geekbot.net.Commands.Utils.Quote
2017-09-28 18:55:57 +02:00
{
[Group("quote")]
[DisableInDirectMessage]
2017-09-28 18:55:57 +02:00
public class Quote : ModuleBase
{
2018-01-21 16:01:43 +01:00
private readonly IErrorHandler _errorHandler;
private readonly DatabaseContext _database;
2017-12-29 01:53:50 +01:00
public Quote(IErrorHandler errorHandler, DatabaseContext database)
2017-09-28 18:55:57 +02:00
{
2018-01-21 16:01:43 +01:00
_errorHandler = errorHandler;
_database = database;
2017-09-28 18:55:57 +02:00
}
2017-12-29 01:53:50 +01:00
[Command]
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
{
var s = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())).ToList();
if (!s.Any())
2018-05-06 02:43:23 +02:00
{
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;
}
var random = new Random().Next(s.Count());
var quote = s[random];
var embed = QuoteBuilder(quote);
2017-09-28 18:55:57 +02:00
await ReplyAsync("", false, embed.Build());
}
catch (Exception e)
{
await _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")]
[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);
_database.Quotes.Add(quote);
2018-05-14 18:57:07 +02:00
await _database.SaveChangesAsync();
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)
{
await _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")]
[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);
_database.Quotes.Add(quote);
2018-05-14 18:57:07 +02:00
await _database.SaveChangesAsync();
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)
{
await _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")]
[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)
{
await _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")]
[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)
{
await _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)]
[Summary("Remove a quote (required mod permissions)")]
public async Task RemoveQuote([Summary("quoteId")] int id)
{
try
{
var quote = _database.Quotes.Where(e => e.GuildId == Context.Guild.Id.AsLong() && e.InternalId == id)?.FirstOrDefault();
if (quote != null)
{
_database.Quotes.Remove(quote);
2018-05-14 18:57:07 +02:00
await _database.SaveChangesAsync();
var embed = QuoteBuilder(quote);
await ReplyAsync($"**Removed #{id}**", false, embed.Build());
}
else
{
await ReplyAsync("I couldn't find a quote with that id :disappointed:");
}
}
catch (Exception e)
{
await _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();
2018-05-17 22:06:58 +02:00
return await list.FirstOrDefault(msg =>
msg.Author.Id == user.Id &&
msg.Embeds.Count == 0 &&
msg.Id != Context.Message.Id &&
!msg.Content.ToLower().StartsWith("!"));
2018-05-06 02:43:23 +02:00
}
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(QuoteModel quote)
2017-09-28 18:55:57 +02:00
{
var user = Context.Client.GetUserAsync(quote.UserId.AsUlong()).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));
eb.Title = $"#{quote.InternalId} | {user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}";
2018-04-30 23:44:19 +02:00
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 QuoteModel CreateQuoteObject(IMessage message)
{
string image;
try
{
image = message.Attachments.First().Url;
}
catch (Exception)
{
image = null;
}
2017-12-29 01:53:50 +01:00
var last = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong()))
.OrderByDescending(e => e.InternalId).FirstOrDefault();
int internalId = 1;
if (last != null) internalId = last.InternalId + 1;
return new QuoteModel()
{
InternalId = internalId,
GuildId = Context.Guild.Id.AsLong(),
UserId = message.Author.Id.AsLong(),
2018-04-30 23:44:19 +02:00
Time = message.Timestamp.DateTime,
Quote = message.Content,
Image = image
};
}
2017-09-28 18:55:57 +02:00
}
}