geekbot/Geekbot.net/Modules/Youtube.cs

56 lines
2 KiB
C#
Raw Permalink Normal View History

using System;
using System.Threading.Tasks;
using Discord.Commands;
2017-04-25 22:53:44 +02:00
using Geekbot.net.Lib;
using Google.Apis.Services;
using Google.Apis.YouTube.v3;
namespace Geekbot.net.Modules
{
public class Youtube : ModuleBase
{
2017-04-25 22:53:44 +02:00
private readonly IRedisClient redis;
public Youtube(IRedisClient redisClient)
{
redis = redisClient;
}
2017-04-25 20:59:38 +02:00
[Command("yt", RunMode = RunMode.Async), Summary("Search for something on youtube.")]
public async Task Yt([Remainder, Summary("A Song Title")] string searchQuery)
{
2017-04-25 22:53:44 +02:00
var key = redis.Client.StringGet("youtubeKey");
if (key.IsNullOrEmpty)
{
2017-04-25 22:53:44 +02:00
await ReplyAsync("No youtube key set, please tell my senpai to set one");
return;
}
2017-04-25 22:53:44 +02:00
try
{
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = key.ToString(),
ApplicationName = this.GetType().ToString()
});
var searchListRequest = youtubeService.Search.List("snippet");
searchListRequest.Q = searchQuery;
searchListRequest.MaxResults = 2;
2017-04-25 22:53:44 +02:00
var searchListResponse = await searchListRequest.ExecuteAsync();
2017-04-25 22:53:44 +02:00
var result = searchListResponse.Items[0];
2017-04-25 22:53:44 +02:00
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.Client.StringGet("botOwner"))).Result;
var dm = await botOwner.CreateDMChannelAsync();
await dm.SendMessageAsync($"Something went wrong while getting a video from youtube:\r\n```\r\n{e.Message}\r\n```");
}
}
}
}