forked from d4n3436/Fergun.Interactive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaitModule.cs
71 lines (57 loc) · 2.6 KB
/
WaitModule.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
using System;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Fergun.Interactive;
namespace ExampleBot.Modules;
[Group("next")]
public class WaitModule : ModuleBase
{
private readonly InteractiveService _interactive;
public WaitModule(InteractiveService interactive)
{
_interactive = interactive;
}
[Command("message", RunMode = RunMode.Async)]
public async Task NextMessageAsync()
{
var msg = await ReplyAsync("Waiting for a message...");
// 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));
await msg.ModifyAsync(x => x.Content = result.IsSuccess ? $"{result.Value!.Author} said: {result.Value.Content}" : $"Failed to get message. Status: {result.Status}");
}
[Command("reaction", RunMode = RunMode.Async)]
public async Task NextReactionAsync()
{
var msg = await ReplyAsync("Add a reaction to this message.");
// Wait for a reaction in the message.
var result = await _interactive.NextReactionAsync(x => x.MessageId == msg.Id, timeout: TimeSpan.FromSeconds(30));
await msg.ModifyAsync(x =>
{
x.Content = result.IsSuccess ? $"{MentionUtils.MentionUser(result.Value!.UserId)} reacted: {result.Value.Emote}" : $"Failed to get reaction. Status: {result.Status}";
x.AllowedMentions = AllowedMentions.None;
x.Embeds = Array.Empty<Embed>(); // workaround for d.net bug
});
}
[Command("interaction", RunMode = RunMode.Async)]
public async Task NextInteractionAsync()
{
var builder = new ComponentBuilder()
.WithButton("Hey", "id");
var msg = await ReplyAsync("Press this button!", components: builder.Build());
// Wait for a user to press the button
var result = await _interactive.NextMessageComponentAsync(x => x.Message.Id == msg.Id, timeout: TimeSpan.FromSeconds(30));
if (result.IsSuccess)
{
// Acknowledge the interaction
await result.Value!.DeferAsync();
}
await msg.ModifyAsync(x =>
{
x.Content = result.IsSuccess ? $"{MentionUtils.MentionUser(result.Value!.User.Id)} pressed the button!" : $"Failed to get interaction. Status: {result.Status}";
x.Components = new ComponentBuilder().Build(); // No components
x.AllowedMentions = AllowedMentions.None;
x.Embeds = Array.Empty<Embed>(); // workaround for d.net bug
});
}
}