Skip to content

Instantly share code, notes, and snippets.

@foxbot
Created August 26, 2016 01:31
Show Gist options
  • Save foxbot/7c6d1dff4b93f9ecc9b06d75353507b1 to your computer and use it in GitHub Desktop.
Save foxbot/7c6d1dff4b93f9ecc9b06d75353507b1 to your computer and use it in GitHub Desktop.
Discord.Net 1.0 Debug Command Handler
StringBuilder builder = new StringBuilder();
var searchResult = _commands.Search(msg, argPos);
if (searchResult.IsSuccess)
{
foreach (var cmd in searchResult.Commands)
{
builder.Append($"**{cmd.Text}({string.Join(",", cmd.Parameters.Select(x => GetParamName(x)))})** ");
var preconditionResult = await cmd.CheckPreconditions(msg);
var parseResult = await cmd.Parse(msg, searchResult, preconditionResult);
if (parseResult.IsSuccess)
builder.AppendLine("(Success)");
else
{
builder.AppendLine("(Failed)");
builder.AppendLine($"{parseResult.Error}: {parseResult.ErrorReason}");
}
if (parseResult.IsSuccess || parseResult.Error == CommandError.MultipleMatches)
{
var argList = parseResult.ArgValues.ToArray();
var newArgList = new object[argList.Length];
for (int i = 0; i < argList.Length; i++)
{
builder.AppendLine($" {cmd.Parameters[i].Name} =");
var values = argList[i].Values.ToArray();
int bestIndex = values.Select((x, y) => new { Index = y, Score = x.Score }).OrderByDescending(x => x.Score).First().Index;
for (int j = 0; j < values.Length; j++)
{
builder.Append(" ");
if (j == bestIndex) builder.Append("**");
builder.AppendLine($"{values[j].Value?.ToString() ?? "null"} [{values[j].Score:0.00}]");
if (j == bestIndex) builder.Append("**");
}
newArgList[i] = values[bestIndex];
}
var paramList = parseResult.ParamValues.ToArray();
var newParamList = new object[paramList.Length];
for (int i = 0; i < paramList.Length; i++)
{
builder.AppendLine($" {cmd.Parameters[i].Name} =");
var values = paramList[i].Values.ToArray();
int bestIndex = values.Select((x, y) => new { Index = y, Score = x.Score }).OrderByDescending(x => x.Score).First().Index;
for (int j = 0; j < values.Length; j++)
{
builder.Append(" ");
if (j == bestIndex) builder.Append("**");
builder.AppendLine($"{values[j].Value?.ToString() ?? "null"} [{values[j].Score:0.00}]");
if (j == bestIndex) builder.Append("**");
}
newParamList[i] = values[bestIndex];
}
}
builder.AppendLine();
}
}
else
builder.AppendLine($"No matching commands");
await msg.Channel.SendMessageAsync(builder.ToString());
//////////
private static string GetParamName(CommandParameter p)
{
return $"{(p.IsMultiple ? "params " : "")}{p.ElementType.Name}{(p.IsOptional ? $" = {p.DefaultValue}" : "")}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment