From 77ea9dae53a8f45e88768bf9d22f7366d6168450 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Tue, 5 Dec 2017 14:58:32 -0500 Subject: [PATCH] fix duplicate help entries and upsert for email addresses --- Commands/CleverBot.cs | 4 ++-- Commands/Definitions.cs | 2 +- Commands/Emails.cs | 32 ++++++++++++++++++++++++-------- Commands/Help.cs | 32 +++++++++++++------------------- Commands/Status.cs | 6 +++--- 5 files changed, 43 insertions(+), 33 deletions(-) diff --git a/Commands/CleverBot.cs b/Commands/CleverBot.cs index 6a47b73..190f026 100644 --- a/Commands/CleverBot.cs +++ b/Commands/CleverBot.cs @@ -28,8 +28,8 @@ namespace dotbot.Commands } - [Command("b")] - [Alias("cleverbot", "cl")] + [Command("cleverbot")] + [Alias("bb")] [Summary("talk to benbot")] public async Task ChatWithCleverBot([Remainder] [Summary("what you want to say to benbot")] string message) { diff --git a/Commands/Definitions.cs b/Commands/Definitions.cs index 3686aca..0c71491 100644 --- a/Commands/Definitions.cs +++ b/Commands/Definitions.cs @@ -15,7 +15,7 @@ namespace dotbot.Commands { using (var db = new DotbotDbContext()) { - if (db.Defs.Any(c => c.Id == Key)) + if (db.Defs.Any(d => d.Id == Key)) db.Defs.Find(Key).Def = Value; else db.Defs.Add(new Definition { Id = Key, Def = Value }); diff --git a/Commands/Emails.cs b/Commands/Emails.cs index 3618db0..f2d1408 100644 --- a/Commands/Emails.cs +++ b/Commands/Emails.cs @@ -21,7 +21,7 @@ namespace dotbot.Commands [Command] public async Task SendEmail( - [Summary("user to send to")] IUser recipient, + [Summary("user to send to")] IUser recipient, [Summary("message to send")] [Remainder] string message ) { using (var db = new DotbotDbContext()) @@ -41,8 +41,8 @@ namespace dotbot.Commands Credentials = new NetworkCredential(_config["gmail_login"], _config["tokens:gmail"]) }; smtp.Send( - $"{Context.User.Username}-{Context.Guild.Name}@benbot.tilde.team", - db.Emails.Find(recipient.Id).EmailAddress, + $"{Context.User.Username}-{Context.Guild.Name}@benbot.tilde.team", + db.Emails.Find(recipient.Id).EmailAddress, $"benbot message from {Context.User.Username}", message ); @@ -59,16 +59,32 @@ namespace dotbot.Commands { using (var db = new DotbotDbContext()) { + var id = Context.User.Id; await Context.Message.DeleteAsync(); - db.Emails.Add(new Email - { - Id = Context.User.Id, - EmailAddress = email - }); + if (db.Emails.Any(e => e.Id == id)) + db.Emails.Find(id).EmailAddress = email; + else + db.Emails.Add(new Email + { + Id = id, + EmailAddress = email + }); db.SaveChanges(); await ReplyAsync("your email has been saved"); } } + + [Command("show")] + [Summary("shows your saved email address")] + public async Task ShowEmail() + { + using (var db = new DotbotDbContext()) + { + await Context.Message.DeleteAsync(); + await ReplyAsync($"{Context.User.Mention}, your saved email is {db.Emails.Find(Context.User.Id)?.EmailAddress ?? "non existent"}"); + } + } + } } diff --git a/Commands/Help.cs b/Commands/Help.cs index 414f5e7..c3b3640 100644 --- a/Commands/Help.cs +++ b/Commands/Help.cs @@ -3,6 +3,7 @@ using Discord.Commands; using Microsoft.Extensions.Configuration; using System.Linq; using System.Threading.Tasks; +using System.Collections.Generic; namespace dotbot.Commands { @@ -20,34 +21,30 @@ namespace dotbot.Commands [Command("help")] public async Task HelpAsync() { - var builder = new EmbedBuilder() + var embed = new EmbedBuilder() { Color = new Color(114, 137, 218), Description = "These are the commands you can use" }; + var cmds = new List(); foreach (var module in _service.Modules) { - string description = null; + string description = ""; foreach (var cmd in module.Commands) { + if (cmds.Contains(cmd.Name)) continue; + cmds.Add(cmd.Name); var result = await cmd.CheckPreconditionsAsync(Context); if (result.IsSuccess) description += $"{_config["prefix"]}{cmd.Aliases.First()}\n"; } if (!string.IsNullOrWhiteSpace(description)) - { - builder.AddField(x => - { - x.Name = module.Name; - x.Value = description; - x.IsInline = false; - }); - } + embed.AddField(module.Name, description); } - await ReplyAsync("", false, builder.Build()); + await ReplyAsync("", embed: embed); } [Command("help")] @@ -63,6 +60,7 @@ namespace dotbot.Commands var builder = new EmbedBuilder() { + Title = $"{Context.Client.CurrentUser.Username} Help", Color = new Color(114, 137, 218), Description = $"Here are some commands like **{command}**" }; @@ -70,14 +68,10 @@ namespace dotbot.Commands foreach (var match in result.Commands) { var cmd = match.Command; - - builder.AddField(x => - { - x.Name = string.Join(", ", cmd.Aliases); - x.Value = $"Parameters: {string.Join(", ", cmd.Parameters.Select(p => p.Name))}\n" + - $"Summary: {cmd.Summary}"; - x.IsInline = false; - }); + builder.AddField( + string.Join(", ", cmd.Aliases), + $"Parameters: {string.Join(", ", cmd.Parameters.Select(p => p.Name))}\nSummary: {cmd.Summary}" + ); } await ReplyAsync("", embed: builder.Build()); diff --git a/Commands/Status.cs b/Commands/Status.cs index 927302f..213a395 100644 --- a/Commands/Status.cs +++ b/Commands/Status.cs @@ -35,7 +35,7 @@ namespace dotbot.Commands embed.AddField("Game", $"{user.Game?.ToString() ?? "not playing anything right now"}"); embed.AddField("Joined At", $"{user.JoinedAt:f}"); - await ReplyAsync("", false, embed); + await ReplyAsync("", embed: embed); } @@ -57,7 +57,7 @@ namespace dotbot.Commands embed.AddField("Bot Uptime", $"{uptime:g}"); embed.AddField("Bot Memory Usage", $"{GC.GetTotalMemory(false) / 1000} mb"); - await ReplyAsync("", false, embed); + await ReplyAsync("", embed: embed); } @@ -92,7 +92,7 @@ namespace dotbot.Commands ); embed.AddField("Server ID", $"{guild.Id}"); - await ReplyAsync("", false, embed); + await ReplyAsync("", embed: embed); } }