.qs command added

This commit is contained in:
Master Kwoth
2017-06-13 14:21:24 +02:00
parent 48fff57d7a
commit f71801cbe6
8 changed files with 109 additions and 5 deletions

View File

@@ -182,6 +182,44 @@ namespace NadekoBot.Modules.Music
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task QueueSearch([Remainder] string query)
{
var videos = (await _google.GetVideoInfosByKeywordAsync(query, 5))
.ToArray();
if (!videos.Any())
{
await ReplyErrorLocalized("song_not_found").ConfigureAwait(false);
return;
}
var msg = await Context.Channel.SendConfirmAsync(string.Join("\n", videos.Select((x, i) => $"`{i + 1}.`\n\t{Format.Bold(x.Name)}\n\t{x.Url}")));
try
{
var input = await GetUserInputAsync(Context.User.Id, Context.Channel.Id);
if (input == null
|| !int.TryParse(input, out var index)
|| (index -= 1) < 0
|| index >= videos.Length)
{
try { await msg.DeleteAsync().ConfigureAwait(false); } catch { }
return;
}
query = videos[index].Url;
await Queue(query).ConfigureAwait(false);
}
finally
{
try { await msg.DeleteAsync().ConfigureAwait(false); } catch { }
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task SoundCloudQueue([Remainder] string query)

View File

@@ -5,6 +5,8 @@ using NadekoBot.Services;
using NLog;
using System.Globalization;
using System.Threading.Tasks;
using System;
using Discord.WebSocket;
namespace NadekoBot.Modules
{
@@ -84,6 +86,44 @@ namespace NadekoBot.Modules
var text = GetText(textKey, replacements);
return Context.Channel.SendConfirmAsync(Context.User.Mention + " " + text);
}
// todo maybe make this generic and use
// TypeConverter typeConverter = TypeDescriptor.GetConverter(propType);
public async Task<string> GetUserInputAsync(ulong userId, ulong channelId)
{
var userInputTask = new TaskCompletionSource<string>();
var dsc = (DiscordShardedClient)Context.Client;
try
{
dsc.MessageReceived += MessageReceived;
if ((await Task.WhenAny(userInputTask.Task, Task.Delay(10000))) != userInputTask.Task)
{
return null;
}
return await userInputTask.Task;
}
finally
{
dsc.MessageReceived -= MessageReceived;
}
Task MessageReceived(SocketMessage arg)
{
if (!(arg is SocketUserMessage userMsg) ||
!(userMsg.Channel is ITextChannel chan) ||
userMsg.Author.Id != userId ||
userMsg.Channel.Id != channelId)
{
return Task.CompletedTask;
}
userInputTask.SetResult(arg.Content);
userMsg.DeleteAfter(1);
return Task.CompletedTask;
}
}
}
public abstract class NadekoSubmodule : NadekoTopLevelModule

View File

@@ -89,7 +89,7 @@ namespace NadekoBot.Modules.Searches
public async Task Youtube([Remainder] string query = null)
{
if (!await ValidateQuery(Context.Channel, query).ConfigureAwait(false)) return;
var result = (await _google.GetVideosByKeywordsAsync(query, 1)).FirstOrDefault();
var result = (await _google.GetVideoLinksByKeywordAsync(query, 1)).FirstOrDefault();
if (string.IsNullOrWhiteSpace(result))
{
await ReplyErrorLocalized("no_results").ConfigureAwait(false);