geekbot/Geekbot.net/Commands/Youtube.cs

61 lines
2.1 KiB
C#
Raw Normal View History

using System;
using System.Threading.Tasks;
using Discord.Commands;
2017-10-12 16:34:10 +02:00
using Geekbot.net.Lib;
using Google.Apis.Services;
using Google.Apis.YouTube.v3;
using StackExchange.Redis;
namespace Geekbot.net.Commands
{
public class Youtube : ModuleBase
{
private readonly IDatabase redis;
2017-09-15 22:56:03 +02:00
public Youtube(IDatabase redis)
{
this.redis = redis;
}
2017-09-15 22:56:03 +02:00
[Command("yt", RunMode = RunMode.Async)]
2017-10-12 16:34:10 +02:00
[Remarks(CommandCategories.Helpers)]
2017-09-15 22:56:03 +02:00
[Summary("Search for something on youtube.")]
public async Task Yt([Remainder] [Summary("Title")] string searchQuery)
{
var key = redis.StringGet("youtubeKey");
if (key.IsNullOrEmpty)
{
await ReplyAsync("No youtube key set, please tell my senpai to set one");
return;
}
try
{
2017-09-15 22:56:03 +02:00
var youtubeService = new YouTubeService(new BaseClientService.Initializer
{
ApiKey = key.ToString(),
2017-09-15 22:56:03 +02:00
ApplicationName = GetType().ToString()
});
var searchListRequest = youtubeService.Search.List("snippet");
searchListRequest.Q = searchQuery;
searchListRequest.MaxResults = 2;
var searchListResponse = await searchListRequest.ExecuteAsync();
var result = searchListResponse.Items[0];
await ReplyAsync(
$"\"{result.Snippet.Title}\" from \"{result.Snippet.ChannelTitle}\" https://youtu.be/{result.Id.VideoId}");
}
catch (Exception e)
{
await ReplyAsync("Something went wrong... informing my senpai...");
var botOwner = Context.Guild.GetUserAsync(ulong.Parse(redis.StringGet("botOwner"))).Result;
var dm = await botOwner.GetOrCreateDMChannelAsync();
2017-09-15 22:56:03 +02:00
await dm.SendMessageAsync(
$"Something went wrong while getting a video from youtube:\r\n```\r\n{e.Message}\r\n```");
}
}
}
}