-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiscordCommandParameter.cs
143 lines (120 loc) · 5 KB
/
DiscordCommandParameter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using Discord;
using Discord.WebSocket;
namespace KostyasLairBot;
public abstract class DiscordCommandParameter
{
internal SlashCommandOptionBuilder SlashCommandOptionBuilder { get; private set; } = new();
protected DiscordCommandParameter(Type innerType, ApplicationCommandOptionType type)
{
SlashCommandOptionBuilder.Type = type;
IsRequired = true;
Description = "No description.";
if (type == ApplicationCommandOptionType.Channel)
{
if (innerType != typeof(SocketChannel) && innerType != typeof(SocketGuildChannel))
{
SlashCommandOptionBuilder.ChannelTypes = new();
if (innerType == typeof(SocketTextChannel))
{
SlashCommandOptionBuilder.ChannelTypes.Add(ChannelType.Text);
SlashCommandOptionBuilder.ChannelTypes.Add(ChannelType.PublicThread);
SlashCommandOptionBuilder.ChannelTypes.Add(ChannelType.PrivateThread);
SlashCommandOptionBuilder.ChannelTypes.Add(ChannelType.NewsThread);
SlashCommandOptionBuilder.ChannelTypes.Add(ChannelType.News);
}
else if (innerType == typeof(SocketVoiceChannel))
{
SlashCommandOptionBuilder.ChannelTypes.Add(ChannelType.Voice);
SlashCommandOptionBuilder.ChannelTypes.Add(ChannelType.Stage);
}
else if (innerType == typeof(SocketForumChannel))
SlashCommandOptionBuilder.ChannelTypes.Add(ChannelType.Forum);
else if (innerType == typeof(SocketCategoryChannel))
SlashCommandOptionBuilder.ChannelTypes.Add(ChannelType.Category);
else if (innerType == typeof(SocketStageChannel))
SlashCommandOptionBuilder.ChannelTypes.Add(ChannelType.Stage);
else if (innerType == typeof(SocketNewsChannel))
SlashCommandOptionBuilder.ChannelTypes.Add(ChannelType.News);
else if (innerType == typeof(SocketThreadChannel))
{
SlashCommandOptionBuilder.ChannelTypes.Add(ChannelType.PublicThread);
SlashCommandOptionBuilder.ChannelTypes.Add(ChannelType.PrivateThread);
SlashCommandOptionBuilder.ChannelTypes.Add(ChannelType.NewsThread);
}
else
throw new NotSupportedException($"The channel type {innerType} is unsupported.");
}
}
}
public string Name
{
get => SlashCommandOptionBuilder.Name;
set => SlashCommandOptionBuilder.Name = value.ToLower();
}
public string Description
{
get => SlashCommandOptionBuilder.Description;
set => SlashCommandOptionBuilder.Description = value;
}
public int? MinStringLength
{
get => SlashCommandOptionBuilder.MinLength;
set => SlashCommandOptionBuilder.MinLength = value;
}
public int? MaxStringLength
{
get => SlashCommandOptionBuilder.MaxLength;
set => SlashCommandOptionBuilder.MaxLength = value;
}
public double? MinValue
{
get => SlashCommandOptionBuilder.MinValue;
set => SlashCommandOptionBuilder.MinValue = value;
}
public double? MaxValue
{
get => SlashCommandOptionBuilder.MaxValue;
set => SlashCommandOptionBuilder.MaxValue = value;
}
public bool IsRequired
{
get => SlashCommandOptionBuilder.IsRequired ?? false;
set => SlashCommandOptionBuilder.IsRequired = value;
}
}
public sealed class DiscordCommandParameter<T> : DiscordCommandParameter
{
public DiscordCommandParameter() : base(typeof(T), GetCommandTypeFromT())
{
}
private static ApplicationCommandOptionType GetCommandTypeFromT()
{
var type = typeof(T);
var typeCode = Type.GetTypeCode(type);
if (typeCode >= TypeCode.SByte && typeCode <= TypeCode.UInt64)
return ApplicationCommandOptionType.Integer;
switch (typeCode)
{
case TypeCode.Boolean:
return ApplicationCommandOptionType.Boolean;
case TypeCode.Decimal:
case TypeCode.Single:
case TypeCode.Double:
return ApplicationCommandOptionType.Number;
case TypeCode.String:
return ApplicationCommandOptionType.String;
}
if (typeCode == TypeCode.Object)
{
if (type == typeof(SocketUser))
return ApplicationCommandOptionType.User;
if (type.IsAssignableTo(typeof(SocketChannel)))
return ApplicationCommandOptionType.Channel;
if (type == typeof(IAttachment))
return ApplicationCommandOptionType.Attachment;
if (type == typeof(SocketRole))
return ApplicationCommandOptionType.Role;
}
throw new NotSupportedException($"The argument type {type} is not supported");
}
}