Skip to content

Commit 24e529c

Browse files
authored
Merge pull request #638 from Aiko-IT-Systems/hatsune-miku/improvements-2
[Part 2] Some improvements made during working on Hatsune Miku
2 parents 0b85fab + db665cd commit 24e529c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+796
-442
lines changed

DisCatSharp.ApplicationCommands/ApplicationCommandsExtension.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,6 @@ private Task InteractionHandler(DiscordClient client, InteractionCreateEventArgs
11271127
GuildLocale = e.Interaction.GuildLocale,
11281128
AppPermissions = e.Interaction.AppPermissions,
11291129
Entitlements = e.Interaction.Entitlements,
1130-
EntitlementSkuIds = e.Interaction.EntitlementSkuIds,
11311130
UserId = e.Interaction.User.Id,
11321131
GuildId = e.Interaction.GuildId,
11331132
MemberId = e.Interaction.GuildId is not null ? e.Interaction.User.Id : null,
@@ -1255,7 +1254,6 @@ private Task InteractionHandler(DiscordClient client, InteractionCreateEventArgs
12551254
GuildLocale = e.Interaction.GuildLocale,
12561255
AppPermissions = e.Interaction.AppPermissions,
12571256
Entitlements = e.Interaction.Entitlements,
1258-
EntitlementSkuIds = e.Interaction.EntitlementSkuIds
12591257
};
12601258

12611259
var choices = await ((Task<IEnumerable<DiscordApplicationCommandAutocompleteChoice>>)providerMethod.Invoke(providerInstance, [context])).ConfigureAwait(false);
@@ -1288,7 +1286,6 @@ private Task InteractionHandler(DiscordClient client, InteractionCreateEventArgs
12881286
GuildLocale = e.Interaction.GuildLocale,
12891287
AppPermissions = e.Interaction.AppPermissions,
12901288
Entitlements = e.Interaction.Entitlements,
1291-
EntitlementSkuIds = e.Interaction.EntitlementSkuIds
12921289
};
12931290

12941291
var choices = await ((Task<IEnumerable<DiscordApplicationCommandAutocompleteChoice>>)providerMethod.Invoke(providerInstance, [context])).ConfigureAwait(false);
@@ -1322,7 +1319,6 @@ private Task InteractionHandler(DiscordClient client, InteractionCreateEventArgs
13221319
GuildLocale = e.Interaction.GuildLocale,
13231320
AppPermissions = e.Interaction.AppPermissions,
13241321
Entitlements = e.Interaction.Entitlements,
1325-
EntitlementSkuIds = e.Interaction.EntitlementSkuIds
13261322
};
13271323

13281324
var choices = await ((Task<IEnumerable<DiscordApplicationCommandAutocompleteChoice>>)providerMethod.Invoke(providerInstance, [context])).ConfigureAwait(false);
@@ -1414,7 +1410,6 @@ private Task ContextMenuHandler(DiscordClient client, ContextMenuInteractionCrea
14141410
GuildLocale = e.Interaction.GuildLocale,
14151411
AppPermissions = e.Interaction.AppPermissions,
14161412
Entitlements = e.Interaction.Entitlements,
1417-
EntitlementSkuIds = e.Interaction.EntitlementSkuIds,
14181413
UserId = e.Interaction.User.Id,
14191414
GuildId = e.Interaction.GuildId,
14201415
MemberId = e.Interaction.GuildId is not null ? e.Interaction.User.Id : null,

DisCatSharp.ApplicationCommands/Attributes/RequirePremiumAttribute.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
5+
using DisCatSharp.ApplicationCommands.Context;
6+
using DisCatSharp.Attributes;
7+
using DisCatSharp.Entities;
8+
using DisCatSharp.Enums;
9+
10+
namespace DisCatSharp.ApplicationCommands.Attributes;
11+
12+
// TODO: Add method to respond with button
13+
/// <summary>
14+
/// Defines that usage of this application command is restricted to users with a specified entitlement.
15+
/// </summary>
16+
/// <remarks>
17+
/// Defines that usage of this command is restricted to users with a specified entitlement.
18+
/// </remarks>
19+
/// <param name="skuId">Sku id for which an entitlement is required to execute this command.</param>
20+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false), RequiresFeature(Features.MonetizedApplication)]
21+
[method: RequiresFeature(Features.MonetizedApplication)]
22+
public sealed class ApplicationCommandRequireSkuEntitlementAttribute(ulong skuId) : ApplicationCommandCheckBaseAttribute
23+
{
24+
/// <summary>
25+
/// Gets the sku id requiring an entitlement.
26+
/// </summary>
27+
public ulong SkuId { get; } = skuId;
28+
29+
/// <summary>
30+
/// Runs checks.
31+
/// </summary>
32+
public override async Task<bool> ExecuteChecksAsync(BaseContext ctx)
33+
{
34+
if (ctx.Interaction.Entitlements.Any(x => x.SkuId == this.SkuId))
35+
return await Task.FromResult(true).ConfigureAwait(false);
36+
37+
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AddComponents(new DiscordPremiumButtonComponent(this.SkuId))).ConfigureAwait(false);
38+
return await Task.FromResult(false).ConfigureAwait(false);
39+
}
40+
}

DisCatSharp.ApplicationCommands/Context/BaseContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
44

5+
using DisCatSharp.Attributes;
56
using DisCatSharp.Entities;
67
using DisCatSharp.Entities.Core;
78
using DisCatSharp.Enums;
@@ -102,6 +103,7 @@ public override string FullCommandName
102103
/// <note type="warning">Can only be used if you have an associated application subscription sku.</note>
103104
/// </para>
104105
/// </summary>
106+
[DiscordDeprecated("Replaced by Entitlements"), Obsolete("Discord replaced this with Entitlements", true, DiagnosticId = "DCS0102")]
105107
public List<ulong> EntitlementSkuIds { get; internal set; } = [];
106108

107109
/// <summary>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
3+
using DisCatSharp.Entities;
4+
5+
namespace DisCatSharp.Interactivity;
6+
7+
/// <summary>
8+
/// The page.
9+
/// </summary>
10+
public class Page
11+
{
12+
/// <summary>
13+
/// <para>Initializes a new instance of the <see cref="Page" /> class.</para>
14+
/// <para>You need to specify at least <paramref name="content" /> or <paramref name="embed" /> or both.</para>
15+
/// </summary>
16+
/// <param name="content">The content.</param>
17+
/// <param name="embed">The embed.</param>
18+
public Page(string? content = null, DiscordEmbedBuilder? embed = null)
19+
{
20+
if (string.IsNullOrEmpty(content) && embed is null)
21+
throw new ArgumentException("You need to specify at least content or embed or both.");
22+
23+
this.Content = content;
24+
this.Embed = embed?.Build();
25+
}
26+
27+
/// <summary>
28+
/// Gets or sets the content.
29+
/// </summary>
30+
public string? Content { get; set; }
31+
32+
/// <summary>
33+
/// Gets or sets the embed.
34+
/// </summary>
35+
public DiscordEmbed? Embed { get; set; }
36+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using DisCatSharp.Entities;
2+
3+
namespace DisCatSharp.Interactivity;
4+
5+
/// <summary>
6+
/// The pagination emojis.
7+
/// </summary>
8+
public sealed class PaginationEmojis
9+
{
10+
/// <summary>
11+
/// Gets or sets the left emoji.
12+
/// </summary>
13+
public DiscordEmoji Left { get; init; }
14+
15+
/// <summary>
16+
/// Gets or sets the right emoji.
17+
/// </summary>
18+
public DiscordEmoji Right { get; init; }
19+
20+
/// <summary>
21+
/// Gets or sets the skip left emoji.
22+
/// </summary>
23+
public DiscordEmoji SkipLeft { get; init; }
24+
25+
/// <summary>
26+
/// Gets or sets the skip right emoji.
27+
/// </summary>
28+
public DiscordEmoji SkipRight { get; init; }
29+
30+
/// <summary>
31+
/// Gets or sets the stop emoji.
32+
/// </summary>
33+
public DiscordEmoji Stop { get; init; }
34+
35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="PaginationEmojis" /> class.
37+
/// </summary>
38+
public PaginationEmojis()
39+
{
40+
this.Left = DiscordEmoji.FromUnicode("◀");
41+
this.Right = DiscordEmoji.FromUnicode("▶");
42+
this.SkipLeft = DiscordEmoji.FromUnicode("⏮");
43+
this.SkipRight = DiscordEmoji.FromUnicode("⏭");
44+
this.Stop = DiscordEmoji.FromUnicode("⏹");
45+
}
46+
}

DisCatSharp.Interactivity/EventHandling/Components/ComponentEventWaiter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.Linq;
45
using System.Threading.Tasks;
56

@@ -59,7 +60,7 @@ public void Dispose()
5960
/// </summary>
6061
/// <param name="request">The request to wait for.</param>
6162
/// <returns>The returned args, or null if it timed out.</returns>
62-
public async Task<ComponentInteractionCreateEventArgs> WaitForMatchAsync(ComponentMatchRequest request)
63+
public async Task<ComponentInteractionCreateEventArgs?> WaitForMatchAsync(ComponentMatchRequest request)
6364
{
6465
this._matchRequests.Add(request);
6566

DisCatSharp.Interactivity/EventHandling/Components/ComponentPaginator.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,24 @@ private async Task HandlePaginationAsync(IPaginationRequest request, ComponentIn
144144

145145
if (request is InteractionPaginationRequest ipr)
146146
{
147-
var builder = new DiscordWebhookBuilder()
148-
.WithContent(page.Content)
149-
.AddEmbed(page.Embed)
150-
.AddComponents(bts);
147+
var builder = new DiscordWebhookBuilder();
148+
if (page.Content is not null)
149+
builder.WithContent(page.Content);
150+
if (page.Embed is not null)
151+
builder.AddEmbed(page.Embed);
152+
builder.AddComponents(bts);
151153

152154
await (await ipr.GetLastInteractionAsync()).EditOriginalResponseAsync(builder).ConfigureAwait(false);
153155
return;
154156
}
155157

156158
this._builder.Clear();
157159

158-
this._builder
159-
.WithContent(page.Content)
160-
.AddEmbed(page.Embed)
161-
.AddComponents(bts);
160+
if (page.Content is not null)
161+
this._builder.WithContent(page.Content);
162+
if (page.Embed is not null)
163+
this._builder.AddEmbed(page.Embed);
164+
this._builder.AddComponents(bts);
162165

163166
await this._builder.ModifyAsync(msg).ConfigureAwait(false);
164167
}

DisCatSharp.Interactivity/EventHandling/Components/ModalEventWaiter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void Dispose()
5454
/// </summary>
5555
/// <param name="request">The request to wait for.</param>
5656
/// <returns>The returned args, or null if it timed out.</returns>
57-
public async Task<ComponentInteractionCreateEventArgs> WaitForModalMatchAsync(ModalMatchRequest request)
57+
public async Task<ComponentInteractionCreateEventArgs?> WaitForModalMatchAsync(ModalMatchRequest request)
5858
{
5959
this._modalMatchRequests.Add(request);
6060

DisCatSharp.Interactivity/EventHandling/Components/Requests/ButtonPaginationRequest.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ CancellationToken token
6060
/// <summary>
6161
/// Gets the page count.
6262
/// </summary>
63-
public int PageCount => this._pages.Count;
63+
public int PageCount
64+
=> this._pages.Count;
6465

6566
/// <summary>
6667
/// Gets the page.
@@ -168,51 +169,59 @@ public Task PreviousPageAsync()
168169
/// <summary>
169170
/// Gets the emojis.
170171
/// </summary>
171-
public Task<PaginationEmojis> GetEmojisAsync() => Task.FromException<PaginationEmojis>(new NotSupportedException("Emojis aren't supported for this request."));
172+
public Task<PaginationEmojis> GetEmojisAsync()
173+
=> Task.FromException<PaginationEmojis>(new NotSupportedException("Emojis aren't supported for this request."));
172174

173175
/// <summary>
174176
/// Gets the buttons.
175177
/// </summary>
176178
public Task<IEnumerable<DiscordButtonComponent>> GetButtonsAsync()
177-
=> Task.FromResult((IEnumerable<DiscordButtonComponent>)this._buttons.ButtonArray);
179+
=> Task.FromResult<IEnumerable<DiscordButtonComponent>>(this._buttons.ButtonArray);
178180

179181
/// <summary>
180182
/// Gets the message.
181183
/// </summary>
182-
public Task<DiscordMessage> GetMessageAsync() => Task.FromResult(this._message);
184+
public Task<DiscordMessage> GetMessageAsync()
185+
=> Task.FromResult(this._message);
183186

184187
/// <summary>
185188
/// Gets the user.
186189
/// </summary>
187-
public Task<DiscordUser> GetUserAsync() => Task.FromResult(this._user);
190+
public Task<DiscordUser> GetUserAsync()
191+
=> Task.FromResult(this._user);
188192

189193
/// <summary>
190194
/// Gets the task completion source.
191195
/// </summary>
192-
public Task<TaskCompletionSource<bool>> GetTaskCompletionSourceAsync() => Task.FromResult(this._tcs);
196+
public Task<TaskCompletionSource<bool>> GetTaskCompletionSourceAsync()
197+
=> Task.FromResult(this._tcs);
193198

194199
/// <summary>
195200
/// Does the cleanup.
196201
/// </summary>
197202
public async Task DoCleanupAsync()
198203
{
204+
var page = this._pages[this._index];
205+
var builder = new DiscordMessageBuilder();
199206
switch (this._behaviorBehavior)
200207
{
201208
case ButtonPaginationBehavior.Disable:
202209
var buttons = this._buttons.ButtonArray.Select(b => b.Disable());
203210

204-
var builder = new DiscordMessageBuilder()
205-
.WithContent(this._pages[this._index].Content)
206-
.AddEmbed(this._pages[this._index].Embed)
207-
.AddComponents(buttons);
211+
if (page.Content is not null)
212+
builder.WithContent(page.Content);
213+
if (page.Embed is not null)
214+
builder.AddEmbed(page.Embed);
215+
builder.AddComponents(buttons);
208216

209217
await builder.ModifyAsync(this._message).ConfigureAwait(false);
210218
break;
211219

212220
case ButtonPaginationBehavior.DeleteButtons:
213-
builder = new DiscordMessageBuilder()
214-
.WithContent(this._pages[this._index].Content)
215-
.AddEmbed(this._pages[this._index].Embed);
221+
if (page.Content is not null)
222+
builder.WithContent(page.Content);
223+
if (page.Embed is not null)
224+
builder.AddEmbed(page.Embed);
216225

217226
await builder.ModifyAsync(this._message).ConfigureAwait(false);
218227
break;

0 commit comments

Comments
 (0)