Make it possible to create quotes from message links

This commit is contained in:
runebaas 2020-08-13 19:51:56 +02:00
parent 7942308059
commit ad086a5e0c
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
3 changed files with 146 additions and 2 deletions

View file

@ -0,0 +1,47 @@
using System;
using System.Data;
using System.Text.RegularExpressions;
namespace Geekbot.Bot.Commands.Utils.Quote
{
public class MessageLink
{
public readonly static Regex re = new Regex(
@"https:\/\/(canary|ptb)?.discord(app)?.com\/channels\/(?<GuildId>\d{16,20})\/(?<ChannelId>\d{16,20})\/(?<MessageId>\d{16,20})",
RegexOptions.Compiled | RegexOptions.IgnoreCase,
new TimeSpan(0, 0, 2));
public ulong GuildId { get; set; }
public ulong ChannelId { get; set; }
public ulong MessageId { get; set; }
public MessageLink(string url)
{
var matches = re.Matches(url);
foreach (Match match in matches)
{
foreach (Group matchGroup in match.Groups)
{
switch (matchGroup.Name)
{
case "GuildId":
GuildId = ulong.Parse(matchGroup.Value);
break;
case "ChannelId":
ChannelId = ulong.Parse(matchGroup.Value);
break;
case "MessageId":
MessageId = ulong.Parse(matchGroup.Value);
break;
}
}
}
}
public static bool IsValid(string link)
{
return re.IsMatch(link);
}
}
}

View file

@ -82,7 +82,7 @@ namespace Geekbot.Bot.Commands.Utils.Quote
if (lastMessage == null) return;
var quote = CreateQuoteObject(lastMessage);
_database.Quotes.Add(quote);
await _database.Quotes.AddAsync(quote);
await _database.SaveChangesAsync();
var embed = QuoteBuilder(quote);
@ -117,7 +117,61 @@ namespace Geekbot.Bot.Commands.Utils.Quote
}
var quote = CreateQuoteObject(message);
_database.Quotes.Add(quote);
await _database.Quotes.AddAsync(quote);
await _database.SaveChangesAsync();
var embed = QuoteBuilder(quote);
await ReplyAsync(transContext.GetString("QuoteAdded"), false, embed.Build());
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context,
"I couldn't find a message with that id :disappointed:");
}
}
[Command("add")]
[Alias("save")]
[Summary("Add a quote from a message link")]
public async Task AddQuote([Summary("message-link")] string messageLink)
{
try
{
var transContext = await _translationHandler.GetGuildContext(Context);
if (!MessageLink.IsValid(messageLink))
{
await ReplyAsync(transContext.GetString("NotAValidMessageLink"));
return;
}
var link = new MessageLink(messageLink);
if (link.GuildId != Context.Guild.Id)
{
await ReplyAsync(transContext.GetString("OnlyQuoteFromSameServer"));
return;
}
var channel = link.ChannelId == Context.Channel.Id
? Context.Channel
: await Context.Guild.GetTextChannelAsync(link.ChannelId);
var message = await channel.GetMessageAsync(link.MessageId);
if (message.Author.Id == Context.Message.Author.Id)
{
await ReplyAsync(transContext.GetString("CannotSaveOwnQuotes"));
return;
}
if (message.Author.IsBot)
{
await ReplyAsync(transContext.GetString("CannotQuoteBots"));
return;
}
var quote = CreateQuoteObject(message);
await _database.Quotes.AddAsync(quote);
await _database.SaveChangesAsync();
var embed = QuoteBuilder(quote);
@ -167,6 +221,43 @@ namespace Geekbot.Bot.Commands.Utils.Quote
}
}
[Command("make")]
[Summary("Create a quote from a message link")]
public async Task ReturnSpecifiedQuote([Summary("message-link")] string messageLink)
{
try
{
var transContext = await _translationHandler.GetGuildContext(Context);
if (!MessageLink.IsValid(messageLink))
{
await ReplyAsync(transContext.GetString("NotAValidMessageLink"));
return;
}
var link = new MessageLink(messageLink);
if (link.GuildId != Context.Guild.Id)
{
await ReplyAsync(transContext.GetString("OnlyQuoteFromSameServer"));
return;
}
var channel = link.ChannelId == Context.Channel.Id
? Context.Channel
: await Context.Guild.GetTextChannelAsync(link.ChannelId);
var message = await channel.GetMessageAsync(link.MessageId);
var quote = CreateQuoteObject(message);
var embed = QuoteBuilder(quote);
await ReplyAsync("", false, embed.Build());
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context,
"I couldn't find that message :disappointed:");
}
}
[Command("remove")]
[RequireUserPermission(GuildPermission.ManageMessages)]
[Summary("Remove a quote (user needs the 'ManageMessages' permission)")]

View file

@ -169,6 +169,12 @@ quote:
MostQuotesPerson:
EN: "Most quoted person"
CHDE: "Meist quoteti person"
NotAValidMessageLink:
EN: "That is not a valid message link"
CHDE: "Das isch kei korrete nachrichtelink"
OnlyQuoteFromSameServer:
EN: "You can only quote messages from the same server"
CHDE: "Du chasch numme nachrichte vom gliche server quote"
rank:
InvalidType:
EN: "Valid types are '`messages`' '`karma`', '`rolls`' and '`cookies`'"