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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,5 @@ paket-files/

# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
*.pyc
/SampleApp/*.ignore
2 changes: 1 addition & 1 deletion Discord.Addons.Interactive/Criteria/Criteria.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Discord.Addons.Interactive
{
public class Criteria<T> : ICriterion<T>
{
private List<ICriterion<T>> _critiera = new List<ICriterion<T>>();
private readonly List<ICriterion<T>> _critiera = new List<ICriterion<T>>();

public Criteria<T> AddCriterion(ICriterion<T> criterion)
{
Expand Down
4 changes: 2 additions & 2 deletions Discord.Addons.Interactive/Discord.Addons.Interactive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Discord.Net.Commands" Version="2.2.0" />
<PackageReference Include="Discord.Net.WebSocket" Version="2.2.0" />
<PackageReference Include="Discord.Net.Commands" Version="3.1.0" />
<PackageReference Include="Discord.Net.WebSocket" Version="3.1.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=callbacks/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=criteria/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=paginator/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=results/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
12 changes: 8 additions & 4 deletions Discord.Addons.Interactive/InteractiveBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@

namespace Discord.Addons.Interactive
{
public abstract class InteractiveBase : InteractiveBase<SocketCommandContext>
public abstract class InteractiveBase
: InteractiveBase<SocketCommandContext>
{
}

public abstract class InteractiveBase<T> : ModuleBase<T>
public abstract class InteractiveBase<T>
: ModuleBase<T>
where T : SocketCommandContext
{
public InteractiveService Interactive { get; set; }

public Task<SocketMessage> NextMessageAsync(ICriterion<SocketMessage> criterion, TimeSpan? timeout = null, CancellationToken token = default(CancellationToken))
public Task<SocketMessage> NextMessageAsync(ICriterion<SocketMessage> criterion, TimeSpan? timeout = null, CancellationToken token = default)
=> Interactive.NextMessageAsync(Context, criterion, timeout, token);
public Task<SocketMessage> NextMessageAsync(bool fromSourceUser = true, bool inSourceChannel = true, TimeSpan? timeout = null, CancellationToken token = default(CancellationToken))
public Task<SocketMessage> NextMessageAsync(bool fromSourceUser = true, bool inSourceChannel = true, TimeSpan? timeout = null, CancellationToken token = default)
=> Interactive.NextMessageAsync(Context, fromSourceUser, inSourceChannel, timeout, token);

public Task<IUserMessage> ReplyAndDeleteAsync(string content, bool isTTS = false, Embed embed = null, TimeSpan? timeout = null, RequestOptions options = null)
Expand All @@ -32,13 +34,15 @@ public Task<IUserMessage> PagedReplyAsync(IEnumerable<object> pages, bool fromSo
};
return PagedReplyAsync(pager, fromSourceUser);
}

public Task<IUserMessage> PagedReplyAsync(PaginatedMessage pager, bool fromSourceUser = true)
{
var criterion = new Criteria<SocketReaction>();
if (fromSourceUser)
criterion.AddCriterion(new EnsureReactionFromSourceUserCriterion());
return PagedReplyAsync(pager, criterion);
}

public Task<IUserMessage> PagedReplyAsync(PaginatedMessage pager, ICriterion<SocketReaction> criterion)
=> Interactive.SendPaginatedMessageAsync(Context, pager, criterion);

Expand Down
48 changes: 28 additions & 20 deletions Discord.Addons.Interactive/InteractiveService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@

namespace Discord.Addons.Interactive
{
public class InteractiveService : IDisposable
public class InteractiveService
: IDisposable
{
public BaseSocketClient Discord { get; }

private Dictionary<ulong, IReactionCallback> _callbacks;
private TimeSpan _defaultTimeout;
private readonly Dictionary<ulong, IReactionCallback> _callbacks;
private readonly TimeSpan _defaultTimeout;

// helpers to allow DI containers to resolve without a custom factory
public InteractiveService(DiscordSocketClient discord, InteractiveServiceConfig config = null)
: this((BaseSocketClient)discord, config) {}

: this((BaseSocketClient)discord, config)
{
}

public InteractiveService(DiscordShardedClient discord, InteractiveServiceConfig config = null)
: this((BaseSocketClient)discord, config) {}
: this((BaseSocketClient)discord, config)
{
}

public InteractiveService(BaseSocketClient discord, InteractiveServiceConfig config = null)
{
Expand All @@ -32,24 +37,27 @@ public InteractiveService(BaseSocketClient discord, InteractiveServiceConfig con
_callbacks = new Dictionary<ulong, IReactionCallback>();
}

public Task<SocketMessage> NextMessageAsync(SocketCommandContext context,
public Task<SocketMessage> NextMessageAsync(
SocketCommandContext context,
bool fromSourceUser = true,
bool inSourceChannel = true,
TimeSpan? timeout = null,
CancellationToken token = default(CancellationToken))
CancellationToken token = default)
{
var criterion = new Criteria<SocketMessage>();
if (fromSourceUser)
criterion.AddCriterion(new EnsureSourceUserCriterion());
if (inSourceChannel)
criterion.AddCriterion(new EnsureSourceChannelCriterion());

return NextMessageAsync(context, criterion, timeout, token);
}

public async Task<SocketMessage> NextMessageAsync(SocketCommandContext context,
public async Task<SocketMessage> NextMessageAsync(
SocketCommandContext context,
ICriterion<SocketMessage> criterion,
TimeSpan? timeout = null,
CancellationToken token = default(CancellationToken))
CancellationToken token = default)
{
timeout = timeout ?? _defaultTimeout;

Expand Down Expand Up @@ -103,17 +111,17 @@ public async Task<IUserMessage> SendPaginatedMessageAsync(SocketCommandContext c
return callback.Message;
}

public void AddReactionCallback(IMessage message, IReactionCallback callback)
=> _callbacks[message.Id] = callback;
public void RemoveReactionCallback(IMessage message)
=> RemoveReactionCallback(message.Id);
public void RemoveReactionCallback(ulong id)
=> _callbacks.Remove(id);
public void ClearReactionCallbacks()
=> _callbacks.Clear();
public void AddReactionCallback(IMessage message, IReactionCallback callback) => _callbacks[message.Id] = callback;

public void RemoveReactionCallback(IMessage message) => RemoveReactionCallback(message.Id);

public void RemoveReactionCallback(ulong id) => _callbacks.Remove(id);

public void ClearReactionCallbacks() => _callbacks.Clear();

private async Task HandleReactionAsync(Cacheable<IUserMessage, ulong> message,
ISocketMessageChannel channel,
private async Task HandleReactionAsync(
Cacheable<IUserMessage, ulong> message,
Cacheable<IMessageChannel, ulong> cacheable,
SocketReaction reaction)
{
if (reaction.UserId == Discord.CurrentUser.Id) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Discord.Addons.Interactive
{
internal class EnsureReactionFromSourceUserCriterion : ICriterion<SocketReaction>
internal class EnsureReactionFromSourceUserCriterion
: ICriterion<SocketReaction>
{
public Task<bool> JudgeAsync(SocketCommandContext sourceContext, SocketReaction parameter)
{
bool ok = parameter.UserId == sourceContext.User.Id;
return Task.FromResult(ok);
return Task.FromResult(parameter.UserId == sourceContext.User.Id);
}
}
}
14 changes: 10 additions & 4 deletions SampleApp/SampleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Discord.Net.Commands" Version="2.2.0" />
<PackageReference Include="Discord.Net.WebSocket" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.3" />
<PackageReference Include="Discord.Net.Commands" Version="3.1.0" />
<PackageReference Include="Discord.Net.WebSocket" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Discord.Addons.Interactive\Discord.Addons.Interactive.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="token.ignore">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>