Skip to content

Commit

Permalink
Use human readable types for invalid arguments embed
Browse files Browse the repository at this point in the history
  • Loading branch information
versx committed Dec 14, 2020
1 parent abc3ea7 commit 130eec5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,15 @@ private async Task Commands_CommandErrored(CommandErrorEventArgs e)
// The user lacks required permissions,
var emoji = DiscordEmoji.FromName(e.Context.Client, ":x:");

var prefix = _whConfig.Servers.ContainsKey(e.Context.Guild.Id) ? _whConfig.Servers[e.Context.Guild.Id].CommandPrefix : "!";
var guildId = e.Context.Guild?.Id ?? e.Context.Client.Guilds.FirstOrDefault(x => _whConfig.Servers.ContainsKey(x.Key)).Key;
var prefix = _whConfig.Servers.ContainsKey(guildId) ? _whConfig.Servers[guildId].CommandPrefix : "!";
var example = $"Command Example: ```{prefix}{e.Command.Name} {string.Join(" ", e.Command.Arguments.Select(x => x.IsOptional ? $"[{x.Name}]" : x.Name))}```\r\n*Parameters in brackets are optional.*";

// let's wrap the response into an embed
var embed = new DiscordEmbedBuilder
{
Title = $"{emoji} Invalid Argument(s)",
Description = $"{string.Join(Environment.NewLine, e.Command.Arguments.Select(x => $"Parameter **{x.Name}** expects type **{x.Type}.**"))}.\r\n\r\n{example}",
Description = $"{string.Join(Environment.NewLine, e.Command.Arguments.Select(x => $"Parameter **{x.Name}** expects type **{x.Type.ToHumanReadableString()}.**"))}.\r\n\r\n{example}",
Color = new DiscordColor(0xFF0000) // red
};
await e.Context.RespondAsync(embed: embed);
Expand Down
14 changes: 14 additions & 0 deletions src/Extensions/GenericsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,19 @@ public static bool ScrambledEquals<T>(this IEnumerable<T> list1, IEnumerable<T>
}
return cnt.Values.All(c => c == 0);
}

public static string ToHumanReadableString(this Type type)
{
if (type == typeof(string))
return "Text";
else if (type == typeof(int))
return "Number";
else if (type == typeof(double) ||
type == typeof(float))
return "Decimal";
else if (type == typeof(bool))
return "Boolean";
return type.Name.ToString();
}
}
}

0 comments on commit 130eec5

Please sign in to comment.