-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathWaitModule.cs
More file actions
82 lines (67 loc) · 2.96 KB
/
WaitModule.cs
File metadata and controls
82 lines (67 loc) · 2.96 KB
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
using System;
using System.Threading.Tasks;
using Discord;
using Discord.Interactions;
using Fergun.Interactive;
using JetBrains.Annotations;
namespace ExampleBot.Modules;
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
[Group("next", "Commands that demonstrate waiting for inputs (messages, reactions, interactions).")]
public class WaitModule : InteractionModuleBase
{
private readonly InteractiveService _interactive;
public WaitModule(InteractiveService interactive)
{
_interactive = interactive;
}
[SlashCommand("message", "Waits for an incoming message.")]
public async Task NextMessageAsync()
{
await RespondAsync("Waiting for a message...");
var message = await GetOriginalResponseAsync();
// Wait for a message in the same channel the command was executed.
var result = await _interactive.NextMessageAsync(x => x.Channel.Id == Context.Channel.Id, timeout: TimeSpan.FromSeconds(30));
string content = result.IsSuccess
? $"{result.Value!.Author} said: {result.Value.Content}"
: $"Failed to get message. Status: {result.Status}";
await message.ModifyAsync(x => x.Content = content);
}
[SlashCommand("reaction", "Waits for a reaction on a message.")]
public async Task NextReactionAsync()
{
await RespondAsync("Add a reaction to this message.");
var message = await GetOriginalResponseAsync();
// Wait for a reaction in the message.
var result = await _interactive.NextReactionAsync(x => x.MessageId == message.Id, timeout: TimeSpan.FromSeconds(30));
string content = result.IsSuccess
? $"{MentionUtils.MentionUser(result.Value!.UserId)} reacted: {result.Value.Emote}"
: $"Failed to get reaction. Status: {result.Status}";
await message.ModifyAsync(x => x.Content = content);
}
[SlashCommand("interaction", "Waits for an incoming interaction.")]
public async Task NextInteractionAsync()
{
var builder = new ComponentBuilder()
.WithButton("Hey", "id");
await RespondAsync("Press this button!", components: builder.Build());
var message = await GetOriginalResponseAsync();
// Wait for a user to press the button
var result = await _interactive.NextMessageComponentAsync(x => x.Message.Id == message.Id, timeout: TimeSpan.FromSeconds(30));
if (result.IsSuccess)
{
await result.Value.UpdateAsync(x =>
{
x.Content = $"{MentionUtils.MentionUser(result.Value!.User.Id)} pressed the button!";
x.Components = new ComponentBuilder().Build(); // No components
});
}
else
{
await ModifyOriginalResponseAsync(x =>
{
x.Content = $"Failed to get interaction. Status: {result.Status}";
x.Components = new ComponentBuilder().Build(); // No components
});
}
}
}