From 2af916e4d1252dab301e9fb2b16231a05564d4af Mon Sep 17 00:00:00 2001 From: Runebaas Date: Tue, 25 Apr 2017 20:59:38 +0200 Subject: [PATCH] True Async and Ping Pong --- .gitignore | 3 +- Geekbot.net/Modules/AdminCmd.cs | 2 +- Geekbot.net/Modules/Cat.cs | 2 +- Geekbot.net/Modules/Counters.cs | 4 +-- Geekbot.net/Modules/EightBall.cs | 3 +- Geekbot.net/Modules/GuildInfo.cs | 47 ++++++++++++++++++++++++++++++++ Geekbot.net/Modules/Help.cs | 2 +- Geekbot.net/Modules/Info.cs | 41 ++++++++++------------------ Geekbot.net/Modules/Ping.cs | 2 +- Geekbot.net/Modules/Roll.cs | 4 +-- Geekbot.net/Modules/Say.cs | 2 +- Geekbot.net/Modules/UserInfo.cs | 3 +- Geekbot.net/Modules/Youtube.cs | 2 +- Geekbot.net/Program.cs | 16 +++++++---- 14 files changed, 87 insertions(+), 46 deletions(-) create mode 100644 Geekbot.net/Modules/GuildInfo.cs diff --git a/.gitignore b/.gitignore index eaca9a0..57f9728 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ Geekbot.net/bin Geekbot.net/obj Backup/ .vs/ -UpgradeLog.htm \ No newline at end of file +UpgradeLog.htm +.idea \ No newline at end of file diff --git a/Geekbot.net/Modules/AdminCmd.cs b/Geekbot.net/Modules/AdminCmd.cs index ffc2719..6fa0868 100644 --- a/Geekbot.net/Modules/AdminCmd.cs +++ b/Geekbot.net/Modules/AdminCmd.cs @@ -14,7 +14,7 @@ namespace Geekbot.net.Modules } [RequireUserPermission(Discord.GuildPermission.Administrator)] - [Command("welcome"), Summary("Set a Welcome Message (use '$user' to mention the new joined user).")] + [Command("welcome", RunMode = RunMode.Async), Summary("Set a Welcome Message (use '$user' to mention the new joined user).")] public async Task SetWelcomeMessage([Remainder, Summary("The message")] string welcomeMessage) { var key = Context.Guild.Id + "-welcomeMsg"; diff --git a/Geekbot.net/Modules/Cat.cs b/Geekbot.net/Modules/Cat.cs index 33bc912..5ca9017 100644 --- a/Geekbot.net/Modules/Cat.cs +++ b/Geekbot.net/Modules/Cat.cs @@ -13,7 +13,7 @@ namespace Geekbot.net.Modules this.catClient = catClient; } - [Command("cat"), Summary("Return a random image of a cat.")] + [Command("cat", RunMode = RunMode.Async), Summary("Return a random image of a cat.")] public async Task Say() { var request = new RestRequest("meow.php", Method.GET); diff --git a/Geekbot.net/Modules/Counters.cs b/Geekbot.net/Modules/Counters.cs index fc3d549..e037b35 100644 --- a/Geekbot.net/Modules/Counters.cs +++ b/Geekbot.net/Modules/Counters.cs @@ -14,7 +14,7 @@ namespace Geekbot.net.Modules redis = redisClient; } - [Command("good"), Summary("Increase Someones Karma")] + [Command("good", RunMode = RunMode.Async), Summary("Increase Someones Karma")] public async Task Good([Summary("The someone")] IUser user) { var lastKarma = GetLastKarma(); @@ -37,7 +37,7 @@ namespace Geekbot.net.Modules } } - [Command("bad"), Summary("Decrease Someones Karma")] + [Command("bad", RunMode = RunMode.Async), Summary("Decrease Someones Karma")] public async Task Bad([Summary("The someone")] IUser user) { var lastKarma = GetLastKarma(); diff --git a/Geekbot.net/Modules/EightBall.cs b/Geekbot.net/Modules/EightBall.cs index d845fc7..2d93c1b 100644 --- a/Geekbot.net/Modules/EightBall.cs +++ b/Geekbot.net/Modules/EightBall.cs @@ -13,8 +13,7 @@ namespace Geekbot.net.Modules { rnd = randomClient; } - - [Command("8ball"), Summary("Ask 8Ball a Question.")] + [Command("8ball", RunMode = RunMode.Async), Summary("Ask 8Ball a Question.")] public async Task Ball([Remainder, Summary("The Question")] string echo) { var replies = new List { diff --git a/Geekbot.net/Modules/GuildInfo.cs b/Geekbot.net/Modules/GuildInfo.cs new file mode 100644 index 0000000..950868f --- /dev/null +++ b/Geekbot.net/Modules/GuildInfo.cs @@ -0,0 +1,47 @@ +using System; +using System.Threading.Tasks; +using Discord.Commands; +using Discord; +using Geekbot.net.Lib; +using System.Linq; + +namespace Geekbot.net.Modules +{ + public class GuildInfo : ModuleBase + { + private readonly IRedisClient redis; + public GuildInfo(IRedisClient redisClient) + { + redis = redisClient; + } + + [Command("serverstats", RunMode = RunMode.Async), Summary("Show some info about the bot.")] + public async Task getInfo() + { + var eb = new EmbedBuilder(); + eb.WithAuthor(new EmbedAuthorBuilder() + .WithIconUrl(Context.Guild.IconUrl) + .WithName(Context.Guild.Name)); + eb.WithColor(new Color(110, 204, 147)); + + var created = Context.Guild.CreatedAt; + var age = Math.Floor((DateTime.Now - created).TotalDays); + + var messages = redis.Client.StringGet($"{Context.Guild.Id}-messages"); + var level = LevelCalc.GetLevelAtExperience((int)messages); + + eb.AddField("Server Age", $"{created.Day}/{created.Month}/{created.Year} ({age} days)"); + eb.AddInlineField("Level", level) + .AddInlineField("Messages", messages); + + await ReplyAsync("", false, eb.Build()); + } + + public static string FirstCharToUpper(string input) + { + if (String.IsNullOrEmpty(input)) + throw new ArgumentException("ARGH!"); + return input.First().ToString().ToUpper() + input.Substring(1); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Modules/Help.cs b/Geekbot.net/Modules/Help.cs index 2570e7e..7064cba 100644 --- a/Geekbot.net/Modules/Help.cs +++ b/Geekbot.net/Modules/Help.cs @@ -6,7 +6,7 @@ namespace Geekbot.net.Modules { public class Help : ModuleBase { - [Command("help"), Summary("List all Commands")] + [Command("help", RunMode = RunMode.Async), Summary("List all Commands")] public async Task GetHelp() { var commands = new CommandService(); diff --git a/Geekbot.net/Modules/Info.cs b/Geekbot.net/Modules/Info.cs index 37dfa90..49c4af2 100644 --- a/Geekbot.net/Modules/Info.cs +++ b/Geekbot.net/Modules/Info.cs @@ -1,10 +1,10 @@ using System; -using System.Threading.Tasks; -using Discord.Commands; -using Discord; -using Geekbot.net.Lib; using System.Collections.Generic; -using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Geekbot.net.Lib; namespace Geekbot.net.Modules { @@ -16,33 +16,22 @@ namespace Geekbot.net.Modules redis = redisClient; } - [Command("serverstats"), Summary("Show some info about the bot.")] - public async Task getInfo() + [Command("info", RunMode = RunMode.Async), Summary("Get Information about the bot")] + public async Task BotInfo() { var eb = new EmbedBuilder(); - eb.WithAuthor(new EmbedAuthorBuilder() - .WithIconUrl(Context.Guild.IconUrl) - .WithName(Context.Guild.Name)); - eb.WithColor(new Color(110, 204, 147)); - - var created = Context.Guild.CreatedAt; - var age = Math.Floor((DateTime.Now - created).TotalDays); - var messages = redis.Client.StringGet($"{Context.Guild.Id}-messages"); - var level = LevelCalc.GetLevelAtExperience((int)messages); + eb.WithTitle("Geekbot V3"); - eb.AddField("Server Age", $"{created.Day}/{created.Month}/{created.Year} ({age} days)"); - eb.AddInlineField("Level", level) - .AddInlineField("Messages", messages); + var botOwner = Context.Guild.GetUserAsync(ulong.Parse(redis.Client.StringGet("botOwner"))).Result; + + eb.AddInlineField("Status", Context.Client.ConnectionState.ToString()) + .AddInlineField("Bot Name", Context.Client.CurrentUser.Username) + .AddInlineField("Bot Owner", $"{botOwner.Username}#{botOwner.Discriminator}"); + + eb.AddInlineField("Servers", Context.Client.GetGuildsAsync().Result.Count); await ReplyAsync("", false, eb.Build()); } - - public static string FirstCharToUpper(string input) - { - if (String.IsNullOrEmpty(input)) - throw new ArgumentException("ARGH!"); - return input.First().ToString().ToUpper() + input.Substring(1); - } } } \ No newline at end of file diff --git a/Geekbot.net/Modules/Ping.cs b/Geekbot.net/Modules/Ping.cs index 6b80c63..0803413 100644 --- a/Geekbot.net/Modules/Ping.cs +++ b/Geekbot.net/Modules/Ping.cs @@ -6,7 +6,7 @@ namespace Geekbot.net.Modules { public class Ping : ModuleBase { - [Command("👀"), Summary("Look at the bot.")] + [Command("👀", RunMode = RunMode.Async), Summary("Look at the bot.")] public async Task Eyes() { await ReplyAsync("S... Stop looking at me... baka!"); diff --git a/Geekbot.net/Modules/Roll.cs b/Geekbot.net/Modules/Roll.cs index cfc4957..6331d22 100644 --- a/Geekbot.net/Modules/Roll.cs +++ b/Geekbot.net/Modules/Roll.cs @@ -15,7 +15,7 @@ namespace Geekbot.net.Modules rnd = randomClient; } - [Command("roll"), Summary("Roll a number between 1 and 100.")] + [Command("roll", RunMode = RunMode.Async), Summary("Roll a number between 1 and 100.")] public async Task RollCommand([Remainder, Summary("stuff...")] string stuff = "nothing") { var number = rnd.Client.Next(1, 100); @@ -38,7 +38,7 @@ namespace Geekbot.net.Modules } } - [Command("dice"), Summary("Roll a dice")] + [Command("dice", RunMode = RunMode.Async), Summary("Roll a dice")] public async Task DiceCommand([Summary("The highest number on the dice")] int max = 6) { var number = rnd.Client.Next(1, max); diff --git a/Geekbot.net/Modules/Say.cs b/Geekbot.net/Modules/Say.cs index fca9ed2..35f0294 100644 --- a/Geekbot.net/Modules/Say.cs +++ b/Geekbot.net/Modules/Say.cs @@ -6,7 +6,7 @@ namespace Geekbot.net.Modules public class Say : ModuleBase { [RequireUserPermission(Discord.GuildPermission.Administrator)] - [Command("say"), Summary("Say Something.")] + [Command("say", RunMode = RunMode.Async), Summary("Say Something.")] public async Task Echo([Remainder, Summary("What?")] string echo) { await Context.Message.DeleteAsync(); diff --git a/Geekbot.net/Modules/UserInfo.cs b/Geekbot.net/Modules/UserInfo.cs index e440d46..7eeb4cb 100644 --- a/Geekbot.net/Modules/UserInfo.cs +++ b/Geekbot.net/Modules/UserInfo.cs @@ -15,7 +15,7 @@ namespace Geekbot.net.Modules } [Alias("stats")] - [Command("user"), Summary("Get information about this user")] + [Command("user", RunMode = RunMode.Async), Summary("Get information about this user")] public async Task User([Summary("The (optional) user to get info for")] IUser user = null) { var userInfo = user ?? Context.Message.Author; @@ -30,6 +30,7 @@ namespace Geekbot.net.Modules eb.WithAuthor(new EmbedAuthorBuilder() .WithIconUrl(userInfo.GetAvatarUrl()) .WithName(userInfo.Username)); + eb.WithColor(new Color(221, 255, 119)); eb.AddField("Discordian Since", $"{userInfo.CreatedAt.Day}/{userInfo.CreatedAt.Month}/{userInfo.CreatedAt.Year} ({age} days)"); diff --git a/Geekbot.net/Modules/Youtube.cs b/Geekbot.net/Modules/Youtube.cs index 6f3c07f..eca4620 100644 --- a/Geekbot.net/Modules/Youtube.cs +++ b/Geekbot.net/Modules/Youtube.cs @@ -17,7 +17,7 @@ namespace Geekbot.net.Modules { public class Youtube : ModuleBase { - [Command("yt"), Summary("Search for something on youtube.")] + [Command("yt", RunMode = RunMode.Async), Summary("Search for something on youtube.")] public async Task Yt([Remainder, Summary("A Song Title")] string searchQuery) { var youtubeService = new YouTubeService(new BaseClientService.Initializer() diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 34833d3..e6e35a6 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -54,32 +54,37 @@ namespace Geekbot.net map.Add(redis); map.Add(new RandomClient()); - await InstallCommands(); Console.WriteLine("Connecting to Discord..."); try { await client.LoginAsync(TokenType.Bot, token); await client.StartAsync(); + client.Connected += FinishStartup; } catch (AggregateException) { Console.WriteLine("Could not connect to discord..."); Environment.Exit(1); } - Console.WriteLine("Done and ready for use...\n"); await Task.Delay(-1); } - public async Task InstallCommands() + public async Task FinishStartup() { + await client.SetGameAsync("Ping Pong"); + Console.WriteLine($"Now Connected to {client.Guilds.Count} Servers"); + + Console.WriteLine("Registering Stuff"); + client.MessageReceived += HandleCommand; client.MessageReceived += HandleMessageReceived; - client.UserJoined += HandleUserJoined; - await commands.AddModulesAsync(Assembly.GetEntryAssembly()); + + Console.WriteLine("Done and ready for use...\n"); } + public async Task HandleCommand(SocketMessage messageParam) { var message = messageParam as SocketUserMessage; @@ -110,7 +115,6 @@ namespace Geekbot.net { var message = messsageParam; if (message == null) return; -// if (message.Author.Username.Equals(client.CurrentUser.Username)) return; var channel = (SocketGuildChannel)message.Channel;