Skip to content

Commit 810fcf5

Browse files
committed
Allow users to reply to links with a bot ping to get the link's video
1 parent f1a3571 commit 810fcf5

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

Workers/MessageHandler.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,28 @@ public Task HandleMessage(SocketMessage socketMessage)
5555
{
5656
try
5757
{
58+
// If the message has no URLs and is a reply, use URLs from the referenced message
59+
var referencedText = GetReferencedMessageUrls(message);
60+
5861
if (splitMessage.Length < 1)
5962
{
63+
if (referencedText != null)
64+
{
65+
await HandleDownload(message, referencedText);
66+
return;
67+
}
6068
if (isMentioned) await HandleHelp(message);
6169
return;
6270
}
6371

6472
if (splitMessage[0].StartsWith("/download") || splitMessage[0].StartsWith("!download"))
6573
{
66-
await HandleDownload(message, messageText);
74+
var downloadText = messageText;
75+
// If the only content is the command itself with no URLs, check the reply
76+
var commandArgs = splitMessage.Length > 1 ? splitMessage[1..] : [];
77+
if (!commandArgs.Any(HasUrl) && referencedText != null)
78+
downloadText = $"{splitMessage[0]} {referencedText}";
79+
await HandleDownload(message, downloadText);
6780
return;
6881
}
6982

@@ -73,6 +86,10 @@ public Task HandleMessage(SocketMessage socketMessage)
7386
return;
7487
}
7588

89+
// If the message has no URLs but is a reply, use the referenced message
90+
if (!splitMessage.Any(HasUrl) && referencedText != null)
91+
messageText = referencedText;
92+
7693
await HandleDownload(message, messageText);
7794
}
7895
catch (Exception ex)
@@ -245,6 +262,26 @@ private int GetFileSizeLimit(IChannel channel)
245262
return config.FileSizeLimit;
246263
}
247264

265+
private static bool HasUrl(string token) =>
266+
Uri.TryCreate(token, UriKind.Absolute, out var uri) && (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps);
267+
268+
/// <summary>
269+
/// If the message is a reply to another message, extract any URLs from the referenced message.
270+
/// Returns a space-separated string of URLs, or null if there are none.
271+
/// </summary>
272+
private static string? GetReferencedMessageUrls(SocketUserMessage message)
273+
{
274+
if (message.ReferencedMessage is not { Content: { Length: > 0 } content })
275+
return null;
276+
277+
var urls = content
278+
.Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
279+
.Where(HasUrl)
280+
.ToArray();
281+
282+
return urls.Length > 0 ? string.Join(' ', urls) : null;
283+
}
284+
248285
private async Task HandleHelp(SocketUserMessage message)
249286
{
250287
var fileSizeLimit = GetFileSizeLimit(message.Channel);

0 commit comments

Comments
 (0)