-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCustomSelectModule.cs
179 lines (144 loc) · 6.9 KB
/
CustomSelectModule.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Fergun.Interactive;
using Fergun.Interactive.Selection;
namespace ExampleBot.Modules;
public partial class CustomModule
{
private readonly CommandService _commandService;
private readonly InteractiveService _interactive;
public CustomModule(CommandService commandService, InteractiveService interactive)
{
_commandService = commandService;
_interactive = interactive;
}
// Sends a multi selection (a message with multiple select menus with options)
[Command("select", RunMode = RunMode.Async)]
public async Task MultiSelectionAsync()
{
// Create CancellationTokenSource that will be canceled after 10 minutes.
using var cts = new CancellationTokenSource(TimeSpan.FromMinutes(10));
var modules = _commandService.Modules.ToArray();
var color = Utils.GetRandomColor();
// Used to track the selected module
string? selectedModule = null;
IUserMessage? message = null;
InteractiveMessageResult<MultiSelectionOption<string>>? result = null;
// This timeout page is used for both cancellation (from the cancellation token) and timeout (specified in the SendSelectionAsync method).
var timeoutPage = new PageBuilder()
.WithDescription("Timeout!")
.WithColor(color);
// Here we will use a "menu" message, that is, it can be reused multiple times until a timeout is received.
// In this case, we will reuse the message with the multi selection until we get a result (multi selection option) with a row value of 1.
// This can only happen if the result is successful and an option in the second select menu (the one that contains the commands) in selected.
while (result is null || (result.IsSuccess && result.Value?.Row != 1))
{
// A multi selection uses the Row property of MultiSelectionOption to determine the position (the select menu) the options will appear.
// The modules will appear in the first select menu.
// There's also an IsDefault property used to determine whether an option is the default for a specific row.
var options = modules.Select(x => new MultiSelectionOption<string>(option: x.Name, row: 0, isDefault: x.Name == selectedModule));
string description = "Select a module";
// result will be null until a module is selected once.
// we know result won't be a command here because of the condition in the while loop.
if (result != null)
{
description = "Select a command\nNote: You can also update your selected module.";
var commands = modules
.First(x => x.Name == result.Value!.Option)
.Commands
.Select(x => new MultiSelectionOption<string>(x.Name, 1));
options = options.Concat(commands);
}
var pageBuilder = new PageBuilder()
.WithDescription(description)
.WithColor(color);
var multiSelection = new MultiSelectionBuilder<string>()
.WithSelectionPage(pageBuilder)
.WithTimeoutPage(timeoutPage)
.WithCanceledPage(timeoutPage)
.WithActionOnTimeout(ActionOnStop.ModifyMessage | ActionOnStop.DeleteInput)
.WithActionOnCancellation(ActionOnStop.ModifyMessage | ActionOnStop.DeleteInput)
.WithOptions(options.ToArray())
.WithStringConverter(x => x.Option)
.AddUser(Context.User)
.Build();
result = message is null
? await _interactive.SendSelectionAsync(multiSelection, Context.Channel, TimeSpan.FromMinutes(2), null, cts.Token)
: await _interactive.SendSelectionAsync(multiSelection, message, TimeSpan.FromMinutes(2), null, cts.Token);
message = result.Message;
if (result.IsSuccess && result.Value!.Row == 0)
{
// We need to track the selected module so we can set it as the default option.
selectedModule = result.Value!.Option;
}
}
if (!result.IsSuccess)
return;
var embed = new EmbedBuilder()
.WithDescription($"You selected:\n**Module**: {selectedModule}\n**Command**: {result.Value!.Option}")
.WithColor(color)
.Build();
await message!.ModifyAsync(x =>
{
x.Embed = embed;
x.Components = new ComponentBuilder().Build(); // Remove components
});
}
}
public class MultiSelectionBuilder<T> : BaseSelectionBuilder<MultiSelection<T>, MultiSelectionOption<T>, MultiSelectionBuilder<T>>
where T : notnull
{
public override InputType InputType => InputType.SelectMenus;
public override MultiSelection<T> Build() => new(this);
}
public class MultiSelection<T>(MultiSelectionBuilder<T> builder) : BaseSelection<MultiSelectionOption<T>>(builder)
where T : notnull
{
public override ComponentBuilder GetOrAddComponents(bool disableAll, ComponentBuilder? builder = null)
{
builder ??= new ComponentBuilder();
var selectMenus = new Dictionary<int, SelectMenuBuilder>();
foreach (var option in Options)
{
if (!selectMenus.TryGetValue(option.Row, out var value))
{
value = new SelectMenuBuilder()
.WithCustomId($"selectmenu{option.Row}")
.WithDisabled(disableAll);
selectMenus[option.Row] = value;
}
var emote = EmoteConverter?.Invoke(option);
string? label = StringConverter?.Invoke(option);
if (emote is null && label is null)
{
throw new InvalidOperationException($"Neither {nameof(EmoteConverter)} nor {nameof(StringConverter)} returned a valid emote or string.");
}
var optionBuilder = new SelectMenuOptionBuilder()
.WithLabel(label)
.WithEmote(emote)
.WithValue(emote?.ToString() ?? label)
.WithDefault(option.IsDefault);
value.AddOption(optionBuilder);
}
foreach ((int row, var selectMenu) in selectMenus)
{
builder.WithSelectMenu(selectMenu, row);
}
return builder;
}
}
public class MultiSelectionOption<T>(T option, int row, bool isDefault = false)
where T : notnull
{
public T Option { get; } = option;
public int Row { get; } = row;
public bool IsDefault { get; set; } = isDefault;
public override string? ToString() => Option.ToString();
public override int GetHashCode() => Option.GetHashCode();
public override bool Equals(object? obj) => Equals(Option, obj);
}