Files
EventDiscordBot/EventBot/Misc/Extensions.cs
Karolis2011 8058bffba5 Help part 2
2019-06-20 01:01:49 +03:00

48 lines
1.5 KiB
C#

using Discord;
using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EventBot.Misc
{
public static class Extensions
{
public static ContextType GetContextType(this ICommandContext context)
{
ContextType type = 0;
if (context.Channel is IGuildChannel)
type |= ContextType.Guild;
if (context.Channel is IDMChannel)
type |= ContextType.DM;
if (context.Channel is IGroupChannel)
type |= ContextType.Group;
return type;
}
public static bool IsContextType(this ICommandContext context, ContextType type) => (context.GetContextType() & type) != 0;
public static string FormatCallHelpString(this CommandInfo command)
{
return $"{command.Aliases[0]} {string.Join(" ", command.Parameters.Select(p => p.FormatParameter()))}";
}
public static string FormatParameter(this ParameterInfo p) => p.IsOptional ? $"[{p.Name}]" : $"<{p.Name}>";
public static string FormatParameterType(this ParameterInfo p)
{
var @switch = new Dictionary<Type, Func<ParameterInfo, string>>()
{
{ typeof(int), (_) => "Number" },
};
if (@switch.ContainsKey(p.Type))
return @switch[p.Type](p);
else
return p.Type.Name;
}
}
}