private static async void OnChatMessage(ChatMessage message) { // reply only to private text messages that start with "hello" if (message.IsPrivateMessage && message.IsText && message.Text.StartsWith("hello", StringComparison.OrdinalIgnoreCase)) { await _client.ReplyTextAsync(message, "Hello there (using dispatcher)!!!"); // an example showing how listener can be removed _client.RemoveMessageListener <ChatMessage>(OnChatMessage); } }
/// <inheritdoc/> public async Task <T> AwaitNextAsync(IWolfClient client, CancellationToken cancellationToken = default) { TaskCompletionSource <T> tcs = new TaskCompletionSource <T>(); using (cancellationToken.Register(() => tcs.TrySetCanceled(cancellationToken))) { Action <T> callback = null; callback = message => { if (!this._conditions(message)) { return; } client.RemoveMessageListener <T>(callback); tcs.TrySetResult(message); }; client.AddMessageListener(callback); return(await tcs.Task); } }
/// <summary>Removes event listener.</summary> /// <remarks>Provided type <typeparamref name="T"/> must be the same as the type used when adding the listener.</remarks> /// <param name="callback">Callback to remove.</param> public static void RemoveMessageListener <T>(this IWolfClient client, Action <T> callback) where T : IWolfMessage => client.RemoveMessageListener(new TypedMessageCallback <T>(callback));