Skip to content

Commit 55c50af

Browse files
Make interactivity responses conditionally follow up (#2282)
This commit fixes a few issues, including bot not limited to: - Requiring a static response message instead of the factory - Subsequently using the static response over the factory - Unconditionally sending a followup instead of checking the response status
1 parent ce9637b commit 55c50af

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

DSharpPlus.Interactivity/EventHandling/ComponentBased/ComponentEventWaiter.cs

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ internal class ComponentEventWaiter : IDisposable
1919
private readonly ConcurrentHashSet<ComponentMatchRequest> matchRequests = [];
2020
private readonly ConcurrentHashSet<ComponentCollectRequest> collectRequests = [];
2121

22-
private readonly DiscordFollowupMessageBuilder message;
2322
private readonly InteractivityConfiguration config;
2423

2524
public ComponentEventWaiter(DiscordClient client, InteractivityConfiguration config)
2625
{
2726
this.client = client;
2827
this.config = config;
29-
30-
this.message = new() { Content = config.ResponseMessage ?? "This message was not meant for you.", IsEphemeral = true };
3128
}
3229

3330
/// <summary>
@@ -90,17 +87,27 @@ internal async Task HandleAsync(DiscordClient _, ComponentInteractionCreatedEven
9087
{
9188
try
9289
{
93-
string responseMessage = this.config.ResponseMessage ??
94-
this.config.ResponseMessageFactory(args, client.ServiceProvider);
95-
96-
await args.Interaction.CreateFollowupMessageAsync
97-
(
98-
new DiscordFollowupMessageBuilder { Content = responseMessage, IsEphemeral = true }
99-
);
90+
string responseMessage = this.config.ResponseMessage ?? this.config.ResponseMessageFactory(args, this.client.ServiceProvider);
91+
92+
if (args.Interaction.ResponseState is DiscordInteractionResponseState.Unacknowledged)
93+
{
94+
await args.Interaction.CreateResponseAsync
95+
(
96+
DiscordInteractionResponseType.ChannelMessageWithSource,
97+
new() { Content = responseMessage, IsEphemeral = true }
98+
);
99+
}
100+
else
101+
{
102+
await args.Interaction.CreateFollowupMessageAsync
103+
(
104+
new() { Content = responseMessage, IsEphemeral = true }
105+
);
106+
}
100107
}
101108
catch (Exception e)
102109
{
103-
client.Logger.LogWarning(e, "An exception was thrown during an interactivity response.");
110+
this.client.Logger.LogWarning(e, "An exception was thrown during an interactivity response.");
104111
}
105112
}
106113
}
@@ -119,17 +126,27 @@ await args.Interaction.CreateFollowupMessageAsync
119126
{
120127
try
121128
{
122-
string responseMessage = this.config.ResponseMessage ??
123-
this.config.ResponseMessageFactory(args, client.ServiceProvider);
124-
125-
await args.Interaction.CreateFollowupMessageAsync
126-
(
127-
new DiscordFollowupMessageBuilder { Content = responseMessage, IsEphemeral = true }
128-
);
129+
string responseMessage = this.config.ResponseMessage ?? this.config.ResponseMessageFactory(args, this.client.ServiceProvider);
130+
131+
if (args.Interaction.ResponseState is DiscordInteractionResponseState.Unacknowledged)
132+
{
133+
await args.Interaction.CreateResponseAsync
134+
(
135+
DiscordInteractionResponseType.ChannelMessageWithSource,
136+
new() { Content = responseMessage, IsEphemeral = true }
137+
);
138+
}
139+
else
140+
{
141+
await args.Interaction.CreateFollowupMessageAsync
142+
(
143+
new() { Content = responseMessage, IsEphemeral = true }
144+
);
145+
}
129146
}
130147
catch (Exception e)
131148
{
132-
client.Logger.LogWarning(e, "An exception was thrown during an interactivity response.");
149+
this.client.Logger.LogWarning(e, "An exception was thrown during an interactivity response.");
133150
}
134151
}
135152
}

DSharpPlus.Interactivity/InteractivityConfiguration.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,5 @@ public InteractivityConfiguration(InteractivityConfiguration other)
9191
this.ResponseMessage = other.ResponseMessage;
9292
this.PollBehaviour = other.PollBehaviour;
9393
this.Timeout = other.Timeout;
94-
95-
if (this.ResponseBehavior is InteractionResponseBehavior.Respond && string.IsNullOrWhiteSpace(this.ResponseMessage))
96-
{
97-
throw new ArgumentException($"{nameof(this.ResponseMessage)} cannot be null, empty, or whitespace when {nameof(this.ResponseBehavior)} is set to respond.");
98-
}
9994
}
10095
}

0 commit comments

Comments
 (0)