Discord.Addons.Collectors 1.0.0

dotnet add package Discord.Addons.Collectors --version 1.0.0
NuGet\Install-Package Discord.Addons.Collectors -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Discord.Addons.Collectors" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Discord.Addons.Collectors --version 1.0.0
#r "nuget: Discord.Addons.Collectors, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Discord.Addons.Collectors as a Cake Addin
#addin nuget:?package=Discord.Addons.Collectors&version=1.0.0

// Install Discord.Addons.Collectors as a Cake Tool
#tool nuget:?package=Discord.Addons.Collectors&version=1.0.0

Discord.Addons.Collectors

An extension for Discord.Net that provides classes to filter and collect messages.

Usage

To initialize a new MessageCollector, you require an instance of a Discord.Websocket.BaseSocketClient. Any inheriting types of this class also works.

private readonly BaseSocketClient _client;
var collector = new MessageCollector(_client);

It might also help to set up a default filter method that can be used as a reference for any future matches you intend to use. All of the required parameters can be omitted if you wish to use a custom filter of your own, as long as the contents of your filter are stored in this method. Here is an example of a MessageCollector in applied use:

public class DummyModule : ModuleBase<SocketCommandContext>
{
	private readonly MessageCollector _collector;

	// The MessageCollector class can be specified as a singleton for dependency injection instead
	// This is a 'hacky' way to get around that for this example
	public DummyModule(DiscordSocketClient client)
	{
		_collector = new MessageCollector(client);
	}

	private bool Judge(SocketMessage message, int index)
	{
		return Context.User.Id == message.Author.Id  && Context.Channel.Id == message.Channel.Id && message.Content == "4";		
	}

	[Command("demo")]
	public async Task DummyCommandAsync()
	{
		var options = new MatchOptions
		{
			Timeout = TimeSpan.FromSeconds(10),
			ResetTimeoutOnAttempt = false
		};

		IUserMessage message = await Context.Channel.SendMessageAsync("What is 2+2?");

		MessageMatch match = await _collector.MatchAsync(Judge, options);

		// If the match is null, this means that the collector failed to receive any matches within the specified time limit
		if (match == null)
		{
			await message.ModifyAsync(m => m.Content = "No response was given. The answer was 4.");
		}
		else
		{
			await message.ModifyAsync(m => m.Content = "Correct! You get a gold star in my book.");
		}
	}
}

There are three methods that can be used in a MessageCollector:

  • MessageCollector.MatchAsync(FilterDelegate, MatchOptions): Attempt to find a single message that matches the specified filter
  • MessageCollector.CollectAsync(FilterCollectionDelegate, CollectionOptions): Attempt to collect all messages that match the specified filter
  • MessageCollector.RunSessionAsync(MessageSession, FilterDelegate, SessionOptions): Starts the specified message session

Message sessions are a useful way to handle messages in an advanced manner. These can be built in any way to give you freedom on how a session should be handled. Here is an example of a simple message session:

public class DummySession : MessageSession
{
	public DummySession(SocketCommandContext context)
	{
		Context = context;
		Attempts = 0;
	}

	private SocketCommandContext Context { get; }
	private int Attempts { get; set; }
	private IUserMessage MessageReference { get; set; }


	public override async Task OnStartAsync()
	{
		MessageReference = await Context.Channel.SendMessageAsync("What is 2+2?");
	}

	public override async Task<SessionResult> OnMessageReceivedAsync(SocketMessage message)
	{
		if (message.Content == "4")
		{
			await MessageReference.ModifyAsync(delegate (MessageProperties x)
			{
				x.Content = "Correct!";
			});

			return SessionResult.Success;
		}

		Attempts++;

		if (Attempts >= 3)
		{
			await MessageReference.ModifyAsync(delegate (MessageProperties x)
			{
				x.Content = "You ran out of attempts. The answer was 4.";
			});
		}
	}

	public override async Task OnTimeoutAsync()
	{
		await MessageReference.ModifyAsync(delegate (MessageProperties x)
		{
			x.Content = "You ran out of time. The answer was 4.";
		});
	}
}

Using this custom session, that example command from before can be tweaked:

public class DummyModule : ModuleBase<SocketCommandContext>
{
	private readonly MessageCollector _collector;

	public DummyModule(DiscordSocketClient client)
	{
		_collector = new MessageCollector(client);
	}

	private bool Judge(SocketMessage message, int index)
	{
		return Context.User.Id == message.Author.Id && Context.Channel.Id == message.Channel.Id;		
	}

	[Command("demo")]
	public async Task DummyCommandAsync()
	{
		var options = new MatchOptions
		{
			Timeout = TimeSpan.FromSeconds(5),
			ResetTimeoutOnAttempt = true
		};

		var session = new DummySession(Context);
		// The result of this method returns a TimeSpan containing the time that was elapsed for this session
		// This can easily be omitted if this is undesired
		TimeSpan elapsedTime = await _collector.RunSessionAsync(session, Judge, options);
	}
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 539 10/11/2020