Skip to content

Commit

Permalink
Merge branch 'oolunar/docs/commands' into checks-article
Browse files Browse the repository at this point in the history
  • Loading branch information
akiraveliara committed Mar 31, 2024
2 parents ddcc801 + eb38e61 commit 1ae8b76
Show file tree
Hide file tree
Showing 35 changed files with 551 additions and 52 deletions.
8 changes: 8 additions & 0 deletions DSharpPlus.Commands/CommandsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ public sealed record CommandsConfiguration
/// as this configuration option will only add the default processors if they're not found in the list.
/// </remarks>
public bool RegisterDefaultCommandProcessors { get; set; } = true;

/// <summary>
/// The command executor to use for command execution.
/// </summary>
/// <remarks>
/// The command executor is responsible for executing context checks, making full use of the dependency injection system, executing the command method itself, and handling errors.
/// </remarks>
public ICommandExecutor CommandExecutor { get; set; } = new CommandExecutor();
}
4 changes: 3 additions & 1 deletion DSharpPlus.Commands/CommandsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public sealed class CommandsExtension : BaseExtension
/// <inheritdoc cref="CommandsConfiguration.RegisterDefaultCommandProcessors"/>
public bool RegisterDefaultCommandProcessors { get; init; }

public CommandExecutor CommandExecutor { get; init; } = new();
public ICommandExecutor CommandExecutor { get; init; }

/// <summary>
/// The registered commands that the users can execute.
Expand Down Expand Up @@ -98,6 +98,8 @@ internal CommandsExtension(CommandsConfiguration configuration)
this.ServiceProvider = configuration.ServiceProvider;
this.DebugGuildId = configuration.DebugGuildId;
this.UseDefaultCommandErrorHandler = configuration.UseDefaultCommandErrorHandler;
this.RegisterDefaultCommandProcessors = configuration.RegisterDefaultCommandProcessors;
this.CommandExecutor = configuration.CommandExecutor;
if (this.UseDefaultCommandErrorHandler)
{
this.CommandErrored += DefaultCommandErrorHandlerAsync;
Expand Down
4 changes: 2 additions & 2 deletions DSharpPlus.Commands/Converters/StringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Task<Optional<string>> ConvertAsync(TextConverterContext context, Message
{
if (attribute is RemainingTextAttribute)
{
return Task.FromResult(Optional.FromValue(context.Argument));
return Task.FromResult(Optional.FromValue(context.RawArguments[context.CurrentArgumentIndex..]));
}
else if (attribute is FromCodeAttribute codeAttribute)
{
Expand All @@ -32,7 +32,7 @@ public Task<Optional<string>> ConvertAsync(TextConverterContext context, Message
}
}

return Task.FromResult(Optional.FromValue(context.RawArguments[context.CurrentArgumentIndex..]));
return Task.FromResult(Optional.FromValue(context.Argument));
}

[SuppressMessage("Roslyn", "IDE0046", Justification = "Ternary rabbit hole.")]
Expand Down
2 changes: 1 addition & 1 deletion DSharpPlus.CommandsNext/CommandsNextConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public sealed class CommandsNextConfiguration
/// <para>Gets or sets the default command executor.</para>
/// <para>This alters the behaviour, execution, and scheduling method of command execution.</para>
/// </summary>
public ICommandExecutor CommandExecutor { internal get; set; } = new ParallelQueuedCommandExecutor();
public ICommandExecutor CommandExecutor { internal get; set; } = new AsynchronousCommandExecutor();

/// <summary>
/// Creates a new instance of <see cref="CommandsNextConfiguration"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class AsynchronousCommandExecutor : ICommandExecutor
{
Task ICommandExecutor.ExecuteAsync(CommandContext ctx)
{
_ = Task.Run(() => ctx.CommandsNext.ExecuteCommandAsync(ctx));
_ = ctx.CommandsNext.ExecuteCommandAsync(ctx);
return Task.CompletedTask;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace DSharpPlus.CommandsNext.Executors;
/// </summary>
public sealed class SynchronousCommandExecutor : ICommandExecutor
{
async Task ICommandExecutor.ExecuteAsync(CommandContext ctx)
=> await ctx.CommandsNext.ExecuteCommandAsync(ctx);
Task ICommandExecutor.ExecuteAsync(CommandContext ctx)
=> ctx.CommandsNext.ExecuteCommandAsync(ctx);

void IDisposable.Dispose()
{ }
Expand Down
6 changes: 6 additions & 0 deletions DSharpPlus.Lavalink/DiscordClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static class DiscordClientExtensions
/// </summary>
/// <param name="client">Discord client to create Lavalink instance for.</param>
/// <returns>Lavalink client instance.</returns>
[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
public static LavalinkExtension UseLavalink(this DiscordClient client)
{
if (client.GetExtension<LavalinkExtension>() != null)
Expand All @@ -37,6 +38,8 @@ public static LavalinkExtension UseLavalink(this DiscordClient client)
/// </summary>
/// <param name="client">Discord sharded client to create Lavalink instances for.</param>
/// <returns>A dictionary of created Lavalink clients.</returns>

[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
public static async Task<IReadOnlyDictionary<int, LavalinkExtension>> UseLavalinkAsync(this DiscordShardedClient client)
{
Dictionary<int, LavalinkExtension> modules = new Dictionary<int, LavalinkExtension>();
Expand All @@ -56,6 +59,7 @@ public static LavalinkExtension UseLavalink(this DiscordClient client)
/// </summary>
/// <param name="client">Discord client to get Lavalink instance for.</param>
/// <returns>Lavalink client instance.</returns>
[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
public static LavalinkExtension GetLavalink(this DiscordClient client)
=> client.GetExtension<LavalinkExtension>();

Expand All @@ -64,6 +68,7 @@ public static LavalinkExtension GetLavalink(this DiscordClient client)
/// </summary>
/// <param name="client">The shard client to retrieve <see cref="LavalinkExtension"/> instances from.</param>
/// <returns>A dictionary containing <see cref="LavalinkExtension"/> instances for each shard.</returns>
[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
public static async Task<IReadOnlyDictionary<int, LavalinkExtension>> GetLavalinkAsync(this DiscordShardedClient client)
{
await client.InitializeShardsAsync();
Expand All @@ -83,6 +88,7 @@ public static LavalinkExtension GetLavalink(this DiscordClient client)
/// <param name="channel">Channel to connect to.</param>
/// <param name="node">Lavalink node to connect through.</param>
/// <returns>If successful, the Lavalink client.</returns>
[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
public static Task ConnectAsync(this DiscordChannel channel, LavalinkNodeConnection node)
{
if (channel == null)
Expand Down
9 changes: 9 additions & 0 deletions DSharpPlus.Lavalink/Entities/LavalinkCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DSharpPlus.Lavalink.Entities;

[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
internal sealed class LavalinkConfigureResume : LavalinkPayload
{
[JsonProperty("key")]
Expand All @@ -20,13 +21,15 @@ public LavalinkConfigureResume(string key, int timeout)
}
}

[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
internal sealed class LavalinkDestroy : LavalinkPayload
{
public LavalinkDestroy(LavalinkGuildConnection lvl)
: base("destroy", lvl.GuildIdString)
{ }
}

[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
internal sealed class LavalinkPlay : LavalinkPayload
{
[JsonProperty("track")]
Expand All @@ -36,6 +39,7 @@ public LavalinkPlay(LavalinkGuildConnection lvl, LavalinkTrack track)
: base("play", lvl.GuildIdString) => this.Track = track.TrackString;
}

[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
internal sealed class LavalinkPlayPartial : LavalinkPayload
{
[JsonProperty("track")]
Expand All @@ -56,6 +60,7 @@ public LavalinkPlayPartial(LavalinkGuildConnection lvl, LavalinkTrack track, Tim
}
}

[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
internal sealed class LavalinkPause : LavalinkPayload
{
[JsonProperty("pause")]
Expand All @@ -65,13 +70,15 @@ public LavalinkPause(LavalinkGuildConnection lvl, bool pause)
: base("pause", lvl.GuildIdString) => this.Pause = pause;
}

[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
internal sealed class LavalinkStop : LavalinkPayload
{
public LavalinkStop(LavalinkGuildConnection lvl)
: base("stop", lvl.GuildIdString)
{ }
}

[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
internal sealed class LavalinkSeek : LavalinkPayload
{
[JsonProperty("position")]
Expand All @@ -81,6 +88,7 @@ public LavalinkSeek(LavalinkGuildConnection lvl, TimeSpan position)
: base("seek", lvl.GuildIdString) => this.Position = (long)position.TotalMilliseconds;
}

[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
internal sealed class LavalinkVolume : LavalinkPayload
{
[JsonProperty("volume")]
Expand All @@ -90,6 +98,7 @@ public LavalinkVolume(LavalinkGuildConnection lvl, int volume)
: base("volume", lvl.GuildIdString) => this.Volume = volume;
}

[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
internal sealed class LavalinkEqualizer : LavalinkPayload
{
[JsonProperty("bands")]
Expand Down
3 changes: 3 additions & 0 deletions DSharpPlus.Lavalink/EventArgs/NodeDisconnectedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;

using DSharpPlus.AsyncEvents;

namespace DSharpPlus.Lavalink.EventArgs;

/// <summary>
/// Represents event arguments for Lavalink node disconnection.
/// </summary>
[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
public sealed class NodeDisconnectedEventArgs : AsyncEventArgs
{
/// <summary>
Expand Down
1 change: 1 addition & 0 deletions DSharpPlus.Lavalink/EventArgs/PlayerUpdateEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace DSharpPlus.Lavalink.EventArgs;
/// <summary>
/// Represents arguments for player update event.
/// </summary>
[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
public sealed class PlayerUpdateEventArgs : AsyncEventArgs
{
/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions DSharpPlus.Lavalink/EventArgs/TrackEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using DSharpPlus.AsyncEvents;

namespace DSharpPlus.Lavalink.EventArgs;
Expand All @@ -14,6 +16,7 @@ internal enum EventType
/// <summary>
/// Represents arguments for a track playback start event.
/// </summary>
[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
public sealed class TrackStartEventArgs : AsyncEventArgs
{
/// <summary>
Expand Down Expand Up @@ -42,6 +45,7 @@ internal struct TrackFinishData
/// <summary>
/// Represents arguments for a track playback finish event.
/// </summary>
[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
public sealed class TrackFinishEventArgs : AsyncEventArgs
{
/// <summary>
Expand Down Expand Up @@ -107,6 +111,7 @@ internal struct TrackStuckData
/// <summary>
/// Represents event arguments for a track stuck event.
/// </summary>
[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
public sealed class TrackStuckEventArgs : AsyncEventArgs
{
/// <summary>
Expand Down Expand Up @@ -141,6 +146,7 @@ internal struct TrackExceptionData
/// <summary>
/// Represents event arguments for a track exception event.
/// </summary>
[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
public sealed class TrackExceptionEventArgs : AsyncEventArgs
{
/// <summary>
Expand Down
1 change: 1 addition & 0 deletions DSharpPlus.Lavalink/LavalinkExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace DSharpPlus.Lavalink;

[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
public sealed class LavalinkExtension : BaseExtension
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions DSharpPlus.Lavalink/LavalinkGuildConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

namespace DSharpPlus.Lavalink;

[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
internal delegate void ChannelDisconnectedEventHandler(LavalinkGuildConnection node);

/// <summary>
/// Represents a Lavalink connection to a channel.
/// </summary>
[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
public sealed class LavalinkGuildConnection
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions DSharpPlus.Lavalink/LavalinkNodeConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

namespace DSharpPlus.Lavalink;

[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
internal delegate void NodeDisconnectedEventHandler(LavalinkNodeConnection node);

/// <summary>
/// Represents a connection to a Lavalink node.
/// </summary>
[Obsolete("DSharpPlus.Lavalink is deprecated for removal.", true)]
public sealed class LavalinkNodeConnection
{
/// <summary>
Expand Down
11 changes: 11 additions & 0 deletions DSharpPlus.Rest/DiscordRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,17 @@ public async Task<DiscordBan> GetGuildBanAsync(ulong guild_id, ulong user_id)
/// <returns></returns>
public async Task CreateGuildBanAsync(ulong guild_id, ulong user_id, int delete_message_days, string reason)
=> await this.ApiClient.CreateGuildBanAsync(guild_id, user_id, delete_message_days, reason);

/// <summary>
/// Creates multiple guild bans
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="userIds">Collection of user ids to ban</param>
/// <param name="deleteMessageSeconds">Timespan in seconds to delete messages from the banned users</param>
/// <param name="reason">Auditlog reason</param>
/// <returns></returns>
public async Task<DiscordBulkBan> CreateGuildBansAsync(ulong guildId, IEnumerable<ulong> userIds, int deleteMessageSeconds, string reason)
=> await this.ApiClient.CreateGuildBulkBanAsync(guildId, userIds, deleteMessageSeconds, reason);

/// <summary>
/// Removes a guild ban
Expand Down
2 changes: 1 addition & 1 deletion DSharpPlus/Clients/DiscordShardedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public async Task<int> InitializeShardsAsync()
}

ShardedLoggerFactory loggerFactory = new ShardedLoggerFactory(this.Configuration.LoggerFactory);
RestClient restClient = new RestClient(this.Configuration, loggerFactory.CreateLogger("Rest"));
RestClient restClient = new RestClient(this.Configuration, loggerFactory.CreateLogger<RestClient>());
DiscordApiClient apiClient = new DiscordApiClient(restClient);

this.GatewayInfo = await apiClient.GetGatewayInfoAsync();
Expand Down
32 changes: 32 additions & 0 deletions DSharpPlus/Entities/Guild/DiscordBulkBan.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace DSharpPlus.Entities;

/// <summary>
/// Result of a bulk ban. Contains the ids of users that were successfully banned and the ids of users that failed to be banned.
/// </summary>
public class DiscordBulkBan
{
/// <summary>
/// Ids of users that were successfully banned.
/// </summary>
[JsonProperty("banned_users")]
public IEnumerable<ulong> BannedUserIds { get; internal set; }

/// <summary>
/// Ids of users that failed to be banned (Already banned or not possible to ban).
/// </summary>
[JsonProperty("failed_users")]
public IEnumerable<ulong> FailedUserIds { get; internal set; }

/// <summary>
/// Users that were successfully banned.
/// </summary>
public IEnumerable<DiscordUser> BannedUsers { get; internal set; }

/// <summary>
/// Users that failed to be banned (Already banned or not possible to ban).
/// </summary>
public IEnumerable<DiscordUser> FailedUsers { get; internal set; }
}
24 changes: 24 additions & 0 deletions DSharpPlus/Entities/Guild/DiscordGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,30 @@ public async Task BanMemberAsync(DiscordUser member, int delete_message_days = 0
public async Task BanMemberAsync(ulong user_id, int delete_message_days = 0, string reason = null)
=> await this.Discord.ApiClient.CreateGuildBanAsync(this.Id, user_id, delete_message_days, reason);

/// <summary>
/// Bans multiple users from this guild.
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="userIds">Collection of users to ban</param>
/// <param name="deleteMessageSeconds">Timespan in seconds to delete messages from the banned users</param>
/// <returns>Response contains a which users were banned and which were not.</returns>
public async Task<DiscordBulkBan> BulkBanMembersAsync(IEnumerable<DiscordUser> users, int deleteMessageSeconds = 0, string reason = null)
{
IEnumerable<ulong> userIds = users.Select(x => x.Id);
return await this.Discord.ApiClient.CreateGuildBulkBanAsync(this.Id, userIds, deleteMessageSeconds, reason);
}

/// <summary>
/// Bans multiple users from this guild by their id
/// </summary>
/// <param name="guildId">Guild ID</param>
/// <param name="userIds">Collection of user ids to ban</param>
/// <param name="deleteMessageSeconds">Timespan in seconds to delete messages from the banned users</param>
/// <returns>Response contains a which users were banned and which were not.</returns>
public async Task<DiscordBulkBan> BulkBanMembersAsync(IEnumerable<ulong> userIds, int deleteMessageSeconds = 0, string reason = null)
=> await this.Discord.ApiClient.CreateGuildBulkBanAsync(this.Id, userIds, deleteMessageSeconds, reason);


/// <summary>
/// Unbans a user from this guild.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace DSharpPlus;

/// <summary>
/// Type of a default value for a select component.
/// </summary>
public enum DiscordSelectDefaultValueType
{
User,
Role,
Channel
}

0 comments on commit 1ae8b76

Please sign in to comment.