-
Notifications
You must be signed in to change notification settings - Fork 53
Inline reaction #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| using Discord.Commands; | ||
| using Discord.WebSocket; | ||
| using System; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Discord.Addons.Interactive | ||
| { | ||
| public class InlineReactionCallback : IReactionCallback | ||
| { | ||
| public RunMode RunMode => RunMode.Sync; | ||
|
|
||
| public ICriterion<SocketReaction> Criterion { get; } | ||
|
|
||
| public TimeSpan? Timeout { get; } | ||
|
|
||
| public SocketCommandContext Context { get; } | ||
|
|
||
| public IUserMessage Message { get; private set; } | ||
|
|
||
| private readonly InteractiveService _interactive; | ||
| private readonly ReactionCallbackData _data; | ||
|
|
||
| public InlineReactionCallback( | ||
| InteractiveService interactive, | ||
| SocketCommandContext context, | ||
| ReactionCallbackData data, | ||
| ICriterion<SocketReaction> criterion = null) | ||
| { | ||
| _interactive = interactive; | ||
| Context = context; | ||
| _data = data; | ||
| Criterion = criterion ?? new EmptyCriterion<SocketReaction>(); | ||
| Timeout = data.Timeout ?? TimeSpan.FromSeconds(30); | ||
| } | ||
|
|
||
| public async Task DisplayAsync() | ||
| { | ||
| var message = await Context.Channel.SendMessageAsync(_data.Text, embed: _data.Embed).ConfigureAwait(false); | ||
| Message = message; | ||
| _interactive.AddReactionCallback(message, this); | ||
|
|
||
| _ = Task.Run(async () => | ||
| { | ||
| foreach (var item in _data.Callbacks) | ||
| await message.AddReactionAsync(item.Reaction); | ||
| }); | ||
|
|
||
| if (Timeout.HasValue) | ||
| { | ||
| _ = Task.Delay(Timeout.Value) | ||
| .ContinueWith(_ => _interactive.RemoveReactionCallback(message)); | ||
| } | ||
| } | ||
|
|
||
| public async Task<bool> HandleCallbackAsync(SocketReaction reaction) | ||
| { | ||
| var reactionCallbackItem = _data.Callbacks.FirstOrDefault(t => t.Reaction.Equals(reaction.Emote)); | ||
| if (reactionCallbackItem == null) | ||
| return false; | ||
|
|
||
| await reactionCallbackItem.Callback(Context); | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using Discord.Commands; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Discord.Addons.Interactive | ||
| { | ||
| public class ReactionCallbackData | ||
| { | ||
| private readonly ICollection<ReactionCallbackItem> _items; | ||
|
|
||
| public string Text { get; } | ||
| public Embed Embed { get; } | ||
| public TimeSpan? Timeout { get; } | ||
| public IEnumerable<ReactionCallbackItem> Callbacks => _items; | ||
|
|
||
| public ReactionCallbackData(string text, Embed embed = null, TimeSpan? timeout = null) | ||
| { | ||
| Text = text; | ||
| Embed = embed; | ||
| Timeout = timeout; | ||
| _items = new List<ReactionCallbackItem>(); | ||
| } | ||
|
|
||
| public ReactionCallbackData WithCallback(IEmote reaction, Func<SocketCommandContext, Task> callback) | ||
| { | ||
| var item = new ReactionCallbackItem(reaction, callback); | ||
| _items.Add(item); | ||
| return this; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using Discord.Commands; | ||
| using System; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Discord.Addons.Interactive | ||
| { | ||
| public class ReactionCallbackItem | ||
| { | ||
| public IEmote Reaction { get; } | ||
| public Func<SocketCommandContext, Task> Callback { get; } | ||
|
|
||
| public ReactionCallbackItem(IEmote reaction, Func<SocketCommandContext, Task> callback) | ||
| { | ||
| Reaction = reaction; | ||
| Callback = callback; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,8 +6,8 @@ | |
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Discord.Net.Commands" Version="1.0.1" /> | ||
| <PackageReference Include="Discord.Net.WebSocket" Version="1.0.1" /> | ||
| <PackageReference Include="Discord.Net.Commands" Version="1.0.2" /> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This update should be noted. Is this really necessary?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is. The 1.0.1 version has some bug and result is following exception |
||
| <PackageReference Include="Discord.Net.WebSocket" Version="1.0.2" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this not be
RunMode.Async?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea to be honest, I used
PaginatedMessageCallbackas a template.