From 59d0a5e1358ae52f92d980abe829f96715f8c8c1 Mon Sep 17 00:00:00 2001 From: dboerlage Date: Mon, 17 Apr 2017 12:55:20 +0200 Subject: [PATCH] youtube, welcome messages and futher code improvements --- Geekbot.net/Geekbot.net.csproj | 3 ++ Geekbot.net/Lib/BootTasks.cs | 16 ++++++++++ Geekbot.net/Lib/RedisClient.cs | 52 +++++++++++++++++++++++++++++--- Geekbot.net/Lib/StatsRecorder.cs | 2 -- Geekbot.net/Modules/UserInfo.cs | 26 ++++++++++++---- Geekbot.net/Modules/Youtube.cs | 42 ++++++++++++++++++++++++++ Geekbot.net/Program.cs | 3 ++ 7 files changed, 132 insertions(+), 12 deletions(-) create mode 100644 Geekbot.net/Lib/BootTasks.cs create mode 100644 Geekbot.net/Modules/Youtube.cs diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index c8b6a30..4f955cb 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -7,6 +7,9 @@ 1.0.0-rc + + 1.25.0.760 + 105.2.4-rc4-24214-01 diff --git a/Geekbot.net/Lib/BootTasks.cs b/Geekbot.net/Lib/BootTasks.cs new file mode 100644 index 0000000..e3122e2 --- /dev/null +++ b/Geekbot.net/Lib/BootTasks.cs @@ -0,0 +1,16 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Geekbot.net.Modules; + +namespace Geekbot.net.Lib +{ + public class BootTasks + { + public static async Task CheckSettingsFile() + { + // ToDO: Check settings file, if invalid, reconfig it +// Console.WriteLine(Path.GetFullPath("./settings.json")); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/RedisClient.cs b/Geekbot.net/Lib/RedisClient.cs index 3e23d97..15ae88c 100644 --- a/Geekbot.net/Lib/RedisClient.cs +++ b/Geekbot.net/Lib/RedisClient.cs @@ -3,6 +3,46 @@ using StackExchange.Redis; namespace Geekbot.net.Lib { + +// public class RedisClient +// { +// private static readonly Lazy _instance +// = new Lazy(() => new RedisClient()); +// private static readonly object ThreadLock = new object(); +// public static IDatabase Client; +// +// private RedisClient() +// { } +// +// public static RedisClient Instance +// { +// get +// { +// if (_instance.IsValueCreated) +// { +// return _instance.Value; +// } +// lock (ThreadLock) +// { +// if (Client == null) +// { +// try +// { +// var redis = ConnectionMultiplexer.Connect("127.0.0.1:6379"); +// Client = redis.GetDatabase(); +// } +// catch (Exception) +// { +// Console.WriteLine("Start Reids already you fucking faggot!"); +// Environment.Exit(69); +// } +// } +// } +// return _instance.Value; +// } +// } +// } + public interface IRedisClient { IDatabase Client { get; set; } @@ -12,12 +52,16 @@ namespace Geekbot.net.Lib { public RedisClient() { - var redis = ConnectionMultiplexer.Connect("127.0.0.1:6379"); - if (!redis.IsConnected) + try { - Console.WriteLine("Could not Connect to the Server..."); + var redis = ConnectionMultiplexer.Connect("127.0.0.1:6379"); + Client = redis.GetDatabase(); + } + catch (Exception) + { + Console.WriteLine("Start Redis already you fucking faggot!"); + Environment.Exit(69); } - Client = redis.GetDatabase(); } public IDatabase Client { get; set; } diff --git a/Geekbot.net/Lib/StatsRecorder.cs b/Geekbot.net/Lib/StatsRecorder.cs index 288d59b..2016944 100644 --- a/Geekbot.net/Lib/StatsRecorder.cs +++ b/Geekbot.net/Lib/StatsRecorder.cs @@ -23,7 +23,6 @@ namespace Geekbot.net.Lib var key = guildId + "-" + message.Author.Id + "-messages"; var messages = (int)redis.StringGet(key); redis.StringSet(key, (messages + 1).ToString()); - await Task.FromResult(true); } public async Task UpdateGuildRecordAsync() @@ -32,7 +31,6 @@ namespace Geekbot.net.Lib var key = guildId + "-messages"; var messages = (int)redis.StringGet(key); redis.StringSet(key, (messages + 1).ToString()); - await Task.FromResult(true); } } } \ No newline at end of file diff --git a/Geekbot.net/Modules/UserInfo.cs b/Geekbot.net/Modules/UserInfo.cs index 44d1ca1..3fa9ead 100644 --- a/Geekbot.net/Modules/UserInfo.cs +++ b/Geekbot.net/Modules/UserInfo.cs @@ -21,12 +21,26 @@ namespace Geekbot.net.Modules var messages = (int)redis.StringGet(key); var level = GetLevelAtExperience(messages); - await ReplyAsync($"```\r\n" + - $"{userInfo.Username}#{userInfo.Discriminator}\r\n" + - $"Messages Sent: {messages}\r\n" + - $"Level: {level}\r\n" + - $"Discordian Since: {userInfo.CreatedAt.Day}/{userInfo.CreatedAt.Month}/{userInfo.CreatedAt.Year} ({age} days)" + - $"```"); + var reply = ""; + + if (Context.Message.Author.Id == userInfo.Id) + { + reply = reply + $"here are your stats {userInfo.Mention}\r\n"; + } + else + { + reply = reply + $"here are {userInfo.Mention}'s stats\r\n"; + } + + reply = reply + $"```\r\n"; + reply = reply + $"Level: {level}\r\n"; + reply = reply + $"Messages Sent: {messages}\r\n"; + reply = + reply + + $"Discordian Since: {userInfo.CreatedAt.Day}/{userInfo.CreatedAt.Month}/{userInfo.CreatedAt.Year} ({age} days)"; + reply = reply + $"```"; + + await ReplyAsync(reply); } [Command("level"), Summary("Get a level based on a number")] diff --git a/Geekbot.net/Modules/Youtube.cs b/Geekbot.net/Modules/Youtube.cs new file mode 100644 index 0000000..621b755 --- /dev/null +++ b/Geekbot.net/Modules/Youtube.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; +using Discord.Commands; + +using Google.Apis.Auth.OAuth2; +using Google.Apis.Services; +using Google.Apis.Upload; +using Google.Apis.Util.Store; +using Google.Apis.YouTube.v3; +using Google.Apis.YouTube.v3.Data; + +namespace Geekbot.net.Modules +{ + public class Youtube : ModuleBase + { + [Command("yt"), Summary("Search for something on youtube.")] + public async Task Yt([Remainder, Summary("A Song Title")] string searchQuery) + { + // AIzaSyDQoJvtNXPVwIcUbSeeDEchnA4a-q1go0E + var youtubeService = new YouTubeService(new BaseClientService.Initializer() + { + ApiKey = "AIzaSyDQoJvtNXPVwIcUbSeeDEchnA4a-q1go0E", + ApplicationName = this.GetType().ToString() + }); + + var searchListRequest = youtubeService.Search.List("snippet"); + searchListRequest.Q = searchQuery; // Replace with your search term. + searchListRequest.MaxResults = 50; + + // Call the search.list method to retrieve results matching the specified query term. + 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}"); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index fa9372f..eb7402e 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -26,6 +26,9 @@ namespace Geekbot.net Console.WriteLine(" \\____|_____|_____|_|\\_\\____/ \\___/ |_|"); Console.WriteLine("========================================="); Console.WriteLine("Starting..."); + +// Task.WaitAll(BootTasks.CheckSettingsFile()); + Task.WaitAll(new Program().MainAsync()); }