diff --git a/DisCatSharp.Interactivity/EventHandling/Requests/CollectRequest.cs b/DisCatSharp.Interactivity/EventHandling/Requests/CollectRequest.cs index 3d312e5f9..b6f92db0c 100644 --- a/DisCatSharp.Interactivity/EventHandling/Requests/CollectRequest.cs +++ b/DisCatSharp.Interactivity/EventHandling/Requests/CollectRequest.cs @@ -1,92 +1,81 @@ // This file is part of the DisCatSharp project, based off DSharpPlus. // // Copyright (c) 2021-2022 AITSYS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. using System; using System.Threading; using System.Threading.Tasks; using ConcurrentCollections; using DisCatSharp.Common.Utilities; namespace DisCatSharp.Interactivity.EventHandling; /// /// CollectRequest is a class that serves as a representation of /// EventArgs that are being collected within a specific time frame. /// /// internal class CollectRequest : IDisposable where T : AsyncEventArgs { internal TaskCompletionSource Tcs; internal CancellationTokenSource Ct; internal Func Predicate; internal TimeSpan Timeout; internal ConcurrentHashSet Collected; /// /// Creates a new CollectRequest object. /// /// Predicate to match /// Timeout time public CollectRequest(Func predicate, TimeSpan timeout) { this.Tcs = new TaskCompletionSource(); this.Ct = new CancellationTokenSource(timeout); this.Predicate = predicate; this.Ct.Token.Register(() => this.Tcs.TrySetResult(true)); this.Timeout = timeout; this.Collected = new ConcurrentHashSet(); } ~CollectRequest() { this.Dispose(); } /// /// Disposes this CollectRequest. /// public void Dispose() { this.Ct.Dispose(); this.Tcs = null; this.Predicate = null; if (this.Collected != null) { this.Collected.Clear(); this.Collected = null; } } } - - -/* - ^ ^ -( Quack! )> (ミචᆽචミ) - - -(somewhere on twitter I read amazon had a duck -that said meow so I had to add a cat that says quack) - -*/ diff --git a/DisCatSharp.Interactivity/EventHandling/Requests/PaginationRequest.cs b/DisCatSharp.Interactivity/EventHandling/Requests/PaginationRequest.cs index 1ee933edc..c7a9be82b 100644 --- a/DisCatSharp.Interactivity/EventHandling/Requests/PaginationRequest.cs +++ b/DisCatSharp.Interactivity/EventHandling/Requests/PaginationRequest.cs @@ -1,320 +1,316 @@ // This file is part of the DisCatSharp project, based off DSharpPlus. // // Copyright (c) 2021-2022 AITSYS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using DisCatSharp.Entities; using DisCatSharp.Interactivity.Enums; -namespace DisCatSharp.Interactivity.EventHandling +namespace DisCatSharp.Interactivity.EventHandling; + +/// +/// The pagination request. +/// +internal class PaginationRequest : IPaginationRequest { + private TaskCompletionSource _tcs; + private readonly CancellationTokenSource _ct; + private readonly TimeSpan _timeout; + private readonly List _pages; + private readonly PaginationBehaviour _behaviour; + private readonly DiscordMessage _message; + private readonly PaginationEmojis _emojis; + private readonly DiscordUser _user; + private int _index; + /// - /// The pagination request. + /// Creates a new Pagination request /// - internal class PaginationRequest : IPaginationRequest + /// Message to paginate + /// User to allow control for + /// Behaviour during pagination + /// Behavior on pagination end + /// Emojis for this pagination object + /// Timeout time + /// Pagination pages + internal PaginationRequest(DiscordMessage message, DiscordUser user, PaginationBehaviour behaviour, PaginationDeletion deletion, + PaginationEmojis emojis, TimeSpan timeout, params Page[] pages) { - private TaskCompletionSource _tcs; - private readonly CancellationTokenSource _ct; - private readonly TimeSpan _timeout; - private readonly List _pages; - private readonly PaginationBehaviour _behaviour; - private readonly DiscordMessage _message; - private readonly PaginationEmojis _emojis; - private readonly DiscordUser _user; - private int _index; - - /// - /// Creates a new Pagination request - /// - /// Message to paginate - /// User to allow control for - /// Behaviour during pagination - /// Behavior on pagination end - /// Emojis for this pagination object - /// Timeout time - /// Pagination pages - internal PaginationRequest(DiscordMessage message, DiscordUser user, PaginationBehaviour behaviour, PaginationDeletion deletion, - PaginationEmojis emojis, TimeSpan timeout, params Page[] pages) + this._tcs = new TaskCompletionSource(); + this._ct = new CancellationTokenSource(timeout); + this._ct.Token.Register(() => this._tcs.TrySetResult(true)); + this._timeout = timeout; + + this._message = message; + this._user = user; + + this.PaginationDeletion = deletion; + this._behaviour = behaviour; + this._emojis = emojis; + + this._pages = new List(); + foreach (var p in pages) { - this._tcs = new TaskCompletionSource(); - this._ct = new CancellationTokenSource(timeout); - this._ct.Token.Register(() => this._tcs.TrySetResult(true)); - this._timeout = timeout; - - this._message = message; - this._user = user; - - this.PaginationDeletion = deletion; - this._behaviour = behaviour; - this._emojis = emojis; - - this._pages = new List(); - foreach (var p in pages) - { - this._pages.Add(p); - } + this._pages.Add(p); } + } - /// - /// Gets the page count. - /// - public int PageCount => this._pages.Count; - - /// - /// Gets the pagination deletion. - /// - public PaginationDeletion PaginationDeletion { get; } - - /// - /// Gets the page async. - /// - /// A Task. - public async Task GetPageAsync() - { - await Task.Yield(); + /// + /// Gets the page count. + /// + public int PageCount => this._pages.Count; - return this._pages[this._index]; - } + /// + /// Gets the pagination deletion. + /// + public PaginationDeletion PaginationDeletion { get; } - /// - /// Skips the left async. - /// - /// A Task. - public async Task SkipLeftAsync() - { - await Task.Yield(); + /// + /// Gets the page async. + /// + /// A Task. + public async Task GetPageAsync() + { + await Task.Yield(); - this._index = 0; - } + return this._pages[this._index]; + } - /// - /// Skips the right async. - /// - /// A Task. - public async Task SkipRightAsync() - { - await Task.Yield(); + /// + /// Skips the left async. + /// + /// A Task. + public async Task SkipLeftAsync() + { + await Task.Yield(); - this._index = this._pages.Count - 1; - } + this._index = 0; + } - /// - /// Nexts the page async. - /// - /// A Task. - public async Task NextPageAsync() - { - await Task.Yield(); + /// + /// Skips the right async. + /// + /// A Task. + public async Task SkipRightAsync() + { + await Task.Yield(); - switch (this._behaviour) - { - case PaginationBehaviour.Ignore: - if (this._index == this._pages.Count - 1) - break; - else - this._index++; + this._index = this._pages.Count - 1; + } + /// + /// Nexts the page async. + /// + /// A Task. + public async Task NextPageAsync() + { + await Task.Yield(); + + switch (this._behaviour) + { + case PaginationBehaviour.Ignore: + if (this._index == this._pages.Count - 1) break; + else + this._index++; - case PaginationBehaviour.WrapAround: - if (this._index == this._pages.Count - 1) - this._index = 0; - else - this._index++; + break; - break; - } - } + case PaginationBehaviour.WrapAround: + if (this._index == this._pages.Count - 1) + this._index = 0; + else + this._index++; - /// - /// Previous the page async. - /// - /// A Task. - public async Task PreviousPageAsync() - { - await Task.Yield(); + break; + } + } - switch (this._behaviour) - { - case PaginationBehaviour.Ignore: - if (this._index == 0) - break; - else - this._index--; + /// + /// Previous the page async. + /// + /// A Task. + public async Task PreviousPageAsync() + { + await Task.Yield(); + switch (this._behaviour) + { + case PaginationBehaviour.Ignore: + if (this._index == 0) break; + else + this._index--; - case PaginationBehaviour.WrapAround: - if (this._index == 0) - this._index = this._pages.Count - 1; - else - this._index--; + break; - break; - } + case PaginationBehaviour.WrapAround: + if (this._index == 0) + this._index = this._pages.Count - 1; + else + this._index--; + + break; } + } - /// - /// Gets the buttons async. - /// - /// + /// + /// Gets the buttons async. + /// + /// #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously - public async Task> GetButtonsAsync() + public async Task> GetButtonsAsync() #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously - => throw new NotSupportedException("This request does not support buttons."); + => throw new NotSupportedException("This request does not support buttons."); - /// - /// Gets the emojis async. - /// - /// A Task. - public async Task GetEmojisAsync() - { - await Task.Yield(); + /// + /// Gets the emojis async. + /// + /// A Task. + public async Task GetEmojisAsync() + { + await Task.Yield(); - return this._emojis; - } + return this._emojis; + } - /// - /// Gets the message async. - /// - /// A Task. - public async Task GetMessageAsync() - { - await Task.Yield(); + /// + /// Gets the message async. + /// + /// A Task. + public async Task GetMessageAsync() + { + await Task.Yield(); - return this._message; - } + return this._message; + } - /// - /// Gets the user async. - /// - /// A Task. - public async Task GetUserAsync() - { - await Task.Yield(); + /// + /// Gets the user async. + /// + /// A Task. + public async Task GetUserAsync() + { + await Task.Yield(); - return this._user; - } + return this._user; + } - /// - /// Dos the cleanup async. - /// - /// A Task. - public async Task DoCleanupAsync() + /// + /// Dos the cleanup async. + /// + /// A Task. + public async Task DoCleanupAsync() + { + switch (this.PaginationDeletion) { - switch (this.PaginationDeletion) - { - case PaginationDeletion.DeleteEmojis: - await this._message.DeleteAllReactionsAsync().ConfigureAwait(false); - break; + case PaginationDeletion.DeleteEmojis: + await this._message.DeleteAllReactionsAsync().ConfigureAwait(false); + break; - case PaginationDeletion.DeleteMessage: - await this._message.DeleteAsync().ConfigureAwait(false); - break; + case PaginationDeletion.DeleteMessage: + await this._message.DeleteAsync().ConfigureAwait(false); + break; - case PaginationDeletion.KeepEmojis: - break; - } + case PaginationDeletion.KeepEmojis: + break; } + } - /// - /// Gets the task completion source async. - /// - /// A Task. - public async Task> GetTaskCompletionSourceAsync() - { - await Task.Yield(); + /// + /// Gets the task completion source async. + /// + /// A Task. + public async Task> GetTaskCompletionSourceAsync() + { + await Task.Yield(); - return this._tcs; - } + return this._tcs; + } - ~PaginationRequest() - { - this.Dispose(); - } + ~PaginationRequest() + { + this.Dispose(); + } - /// - /// Disposes this PaginationRequest. - /// - public void Dispose() - { - this._ct.Dispose(); - this._tcs = null; - } + /// + /// Disposes this PaginationRequest. + /// + public void Dispose() + { + this._ct.Dispose(); + this._tcs = null; } } -namespace DisCatSharp.Interactivity +/// +/// The pagination emojis. +/// +public class PaginationEmojis { + public DiscordEmoji SkipLeft; + public DiscordEmoji SkipRight; + public DiscordEmoji Left; + public DiscordEmoji Right; + public DiscordEmoji Stop; + /// - /// The pagination emojis. + /// Initializes a new instance of the class. /// - public class PaginationEmojis + public PaginationEmojis() { - public DiscordEmoji SkipLeft; - public DiscordEmoji SkipRight; - public DiscordEmoji Left; - public DiscordEmoji Right; - public DiscordEmoji Stop; - - /// - /// Initializes a new instance of the class. - /// - public PaginationEmojis() - { - this.Left = DiscordEmoji.FromUnicode("◀"); - this.Right = DiscordEmoji.FromUnicode("▶"); - this.SkipLeft = DiscordEmoji.FromUnicode("⏮"); - this.SkipRight = DiscordEmoji.FromUnicode("⏭"); - this.Stop = DiscordEmoji.FromUnicode("⏹"); - } + this.Left = DiscordEmoji.FromUnicode("◀"); + this.Right = DiscordEmoji.FromUnicode("▶"); + this.SkipLeft = DiscordEmoji.FromUnicode("⏮"); + this.SkipRight = DiscordEmoji.FromUnicode("⏭"); + this.Stop = DiscordEmoji.FromUnicode("⏹"); } +} + +/// +/// The page. +/// +public class Page +{ + /// + /// Gets or sets the content. + /// + public string Content { get; set; } + /// + /// Gets or sets the embed. + /// + public DiscordEmbed Embed { get; set; } /// - /// The page. + /// Initializes a new instance of the class. /// - public class Page + /// The content. + /// The embed. + public Page(string content = "", DiscordEmbedBuilder embed = null) { - /// - /// Gets or sets the content. - /// - public string Content { get; set; } - /// - /// Gets or sets the embed. - /// - public DiscordEmbed Embed { get; set; } - - /// - /// Initializes a new instance of the class. - /// - /// The content. - /// The embed. - public Page(string content = "", DiscordEmbedBuilder embed = null) - { - this.Content = content; - this.Embed = embed?.Build(); - } + this.Content = content; + this.Embed = embed?.Build(); } }