Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link

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?

Copy link
Author

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 PaginatedMessageCallback as a template.


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;
}
}
}
32 changes: 32 additions & 0 deletions Discord.Addons.Interactive/InlineReaction/ReactionCallbackData.cs
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;
}
}
}
18 changes: 18 additions & 0 deletions Discord.Addons.Interactive/InlineReaction/ReactionCallbackItem.cs
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;
}
}
}
3 changes: 3 additions & 0 deletions Discord.Addons.Interactive/InteractiveBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public Task<IUserMessage> PagedReplyAsync(PaginatedMessage pager, bool fromSourc
public Task<IUserMessage> PagedReplyAsync(PaginatedMessage pager, ICriterion<SocketReaction> criterion)
=> Interactive.SendPaginatedMessageAsync(Context, pager, criterion);

public Task<IUserMessage> InlineReactionReplyAsync(ReactionCallbackData data, bool fromSourceUser = true)
=> Interactive.SendMessageWithReactionCallbacksAsync(Context, data, fromSourceUser);

public RuntimeResult Ok(string reason = null) => new OkResult(reason);
}
}
15 changes: 12 additions & 3 deletions Discord.Addons.Interactive/InteractiveService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Discord.Commands;
using Discord.WebSocket;
using System.Collections.Generic;
using System.Linq;

namespace Discord.Addons.Interactive
{
Expand Down Expand Up @@ -46,7 +45,7 @@ async Task Handler(SocketMessage message)
}

context.Client.MessageReceived += Handler;

var trigger = eventTrigger.Task;
var delay = Task.Delay(timeout.Value);
var task = await Task.WhenAny(trigger, delay).ConfigureAwait(false);
Expand Down Expand Up @@ -76,6 +75,16 @@ public async Task<IUserMessage> SendPaginatedMessageAsync(SocketCommandContext c
return callback.Message;
}

public async Task<IUserMessage> SendMessageWithReactionCallbacksAsync(SocketCommandContext context, ReactionCallbackData callbacks, bool fromSourceUser = true)
{
var criterion = new Criteria<SocketReaction>();
if(fromSourceUser)
criterion.AddCriterion(new EnsureReactionFromSourceUserCriterion());
var callback = new InlineReactionCallback(this, context, callbacks, criterion);
await callback.DisplayAsync().ConfigureAwait(false);
return callback.Message;
}

public void AddReactionCallback(IMessage message, IReactionCallback callback)
=> _callbacks[message.Id] = callback;
public void RemoveReactionCallback(IMessage message)
Expand All @@ -84,7 +93,7 @@ public void RemoveReactionCallback(ulong id)
=> _callbacks.Remove(id);
public void ClearReactionCallbacks()
=> _callbacks.Clear();

private async Task HandleReactionAsync(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction)
{
if (reaction.UserId == Discord.CurrentUser.Id) return;
Expand Down
35 changes: 33 additions & 2 deletions SampleApp/Modules/SampleModule.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System.Threading.Tasks;
using Discord;
using Discord.Addons.Interactive;
using Discord.Commands;
using System;
using System.Threading.Tasks;

namespace SampleApp.Modules
{
Expand Down Expand Up @@ -40,5 +41,35 @@ public async Task Test_Paginator()
var pages = new[] { "Page 1", "Page 2", "Page 3", "aaaaaa", "Page 5" };
await PagedReplyAsync(pages);
}

// InlineReactionReplyAsync will send a message and adds reactions on it.
// Once an user adds a reaction, the callback is fired.
// If callback was successfull next callback is not handled (message is unsubscribed).
// Unsuccessful callback is a reaction that did not have a callback.
[Command("reaction")]
public async Task Test_ReactionReply()
{
await InlineReactionReplyAsync(new ReactionCallbackData("text")
.WithCallback(new Emoji("👍"), c => c.Channel.SendMessageAsync("You've replied with 👍"))
.WithCallback(new Emoji("👎"), c => c.Channel.SendMessageAsync("You've replied with 👎"))
);
}
[Command("embedreaction")]
public async Task Test_EmedReactionReply()
{
var one = new Emoji("1⃣");
var two = new Emoji("2⃣");

var embed = new EmbedBuilder()
.WithTitle("Choose one")
.AddInlineField(one.Name, "Beer")
.AddInlineField(two.Name, "Drink")
.Build();

await InlineReactionReplyAsync(new ReactionCallbackData("text", embed)
.WithCallback(one, c => c.Channel.SendMessageAsync("Here you go :beer:"))
.WithCallback(two, c => c.Channel.SendMessageAsync("Here you go :tropical_drink:"))
);
}
}
}
4 changes: 2 additions & 2 deletions SampleApp/SampleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This update should be noted. Is this really necessary?

Copy link
Author

Choose a reason for hiding this comment

The 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
System.InvalidOperationException: Unknown guild channel type.

<PackageReference Include="Discord.Net.WebSocket" Version="1.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down