Skip to content

Commit

Permalink
Improve contexts and refactor them
Browse files Browse the repository at this point in the history
  • Loading branch information
KubaZ2 committed Oct 1, 2024
1 parent 1446d60 commit 2341605
Show file tree
Hide file tree
Showing 4 changed files with 393 additions and 217 deletions.
97 changes: 77 additions & 20 deletions NetCord.Services/ApplicationCommands/ApplicationCommandContexts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,127 @@

namespace NetCord.Services.ApplicationCommands;

public class ApplicationCommandContext(ApplicationCommandInteraction interaction) : IApplicationCommandContext
public class BaseApplicationCommandContext(ApplicationCommandInteraction interaction) : IApplicationCommandContext
{
public virtual ApplicationCommandInteraction Interaction { get; } = interaction;
public ApplicationCommandInteraction Interaction => interaction;
}

public class BaseSlashCommandContext(SlashCommandInteraction interaction) : ApplicationCommandContext(interaction)
public class ApplicationCommandContext(ApplicationCommandInteraction interaction, GatewayClient client)
: BaseApplicationCommandContext(interaction),
IGatewayClientContext,
IGuildContext,
IChannelContext,
IUserContext
{
public override SlashCommandInteraction Interaction { get; } = interaction;
public GatewayClient Client => client;
public Guild? Guild => Interaction.Guild;
public TextChannel Channel => Interaction.Channel;
public User User => Interaction.User;
}

public class HttpApplicationCommandContext(ApplicationCommandInteraction interaction, RestClient client)
: BaseApplicationCommandContext(interaction),
IRestClientContext,
IChannelContext,
IUserContext
{
public RestClient Client => client;
public TextChannel Channel => Interaction.Channel;
public User User => Interaction.User;
}

public class SlashCommandContext(SlashCommandInteraction interaction, GatewayClient client) : BaseSlashCommandContext(interaction), IGatewayClientContext, IGuildContext, IChannelContext, IUserContext
public class BaseSlashCommandContext(SlashCommandInteraction interaction) : IApplicationCommandContext
{
public GatewayClient Client { get; } = client;
public SlashCommandInteraction Interaction => interaction;

ApplicationCommandInteraction IApplicationCommandContext.Interaction => interaction;
}

public class SlashCommandContext(SlashCommandInteraction interaction, GatewayClient client)
: BaseSlashCommandContext(interaction),
IGatewayClientContext,
IGuildContext,
IChannelContext,
IUserContext
{
public GatewayClient Client => client;
public Guild? Guild => Interaction.Guild;
public TextChannel Channel => Interaction.Channel;
public User User => Interaction.User;
}

public class HttpSlashCommandContext(SlashCommandInteraction interaction, RestClient client) : BaseSlashCommandContext(interaction), IRestClientContext, IChannelContext, IUserContext
public class HttpSlashCommandContext(SlashCommandInteraction interaction, RestClient client)
: BaseSlashCommandContext(interaction),
IRestClientContext,
IChannelContext,
IUserContext
{
public RestClient Client { get; } = client;
public RestClient Client => client;
public TextChannel Channel => Interaction.Channel;
public User User => Interaction.User;
}

public class BaseUserCommandContext(UserCommandInteraction interaction) : ApplicationCommandContext(interaction)
public class BaseUserCommandContext(UserCommandInteraction interaction) : IApplicationCommandContext
{
public override UserCommandInteraction Interaction { get; } = interaction;
public UserCommandInteraction Interaction => interaction;

ApplicationCommandInteraction IApplicationCommandContext.Interaction => interaction;
}

public class UserCommandContext(UserCommandInteraction interaction, GatewayClient client) : BaseUserCommandContext(interaction), IGatewayClientContext, IGuildContext, IChannelContext, IUserContext
public class UserCommandContext(UserCommandInteraction interaction, GatewayClient client)
: BaseUserCommandContext(interaction),
IGatewayClientContext,
IGuildContext,
IChannelContext,
IUserContext
{
public GatewayClient Client { get; } = client;
public GatewayClient Client => client;
public Guild? Guild => Interaction.Guild;
public TextChannel Channel => Interaction.Channel;
public User User => Interaction.User;
public User Target => Interaction.Data.TargetUser;
}

public class HttpUserCommandContext(UserCommandInteraction interaction, RestClient client) : BaseUserCommandContext(interaction), IRestClientContext, IChannelContext, IUserContext
public class HttpUserCommandContext(UserCommandInteraction interaction, RestClient client)
: BaseUserCommandContext(interaction),
IRestClientContext,
IChannelContext,
IUserContext
{
public RestClient Client { get; } = client;
public RestClient Client => client;
public TextChannel Channel => Interaction.Channel;
public User User => Interaction.User;
public User Target => Interaction.Data.TargetUser;
}

public class BaseMessageCommandContext(MessageCommandInteraction interaction) : ApplicationCommandContext(interaction)
public class BaseMessageCommandContext(MessageCommandInteraction interaction) : IApplicationCommandContext
{
public override MessageCommandInteraction Interaction { get; } = interaction;
public MessageCommandInteraction Interaction => interaction;

ApplicationCommandInteraction IApplicationCommandContext.Interaction => interaction;
}

public class MessageCommandContext(MessageCommandInteraction interaction, GatewayClient client) : BaseMessageCommandContext(interaction), IGatewayClientContext, IGuildContext, IChannelContext, IUserContext
public class MessageCommandContext(MessageCommandInteraction interaction, GatewayClient client)
: BaseMessageCommandContext(interaction),
IGatewayClientContext,
IGuildContext,
IChannelContext,
IUserContext
{
public GatewayClient Client { get; } = client;
public GatewayClient Client => client;
public Guild? Guild => Interaction.Guild;
public TextChannel Channel => Interaction.Channel;
public User User => Interaction.User;
public RestMessage Target => Interaction.Data.TargetMessage;
}

public class HttpMessageCommandContext(MessageCommandInteraction interaction, RestClient client) : BaseMessageCommandContext(interaction), IRestClientContext, IChannelContext, IUserContext
public class HttpMessageCommandContext(MessageCommandInteraction interaction, RestClient client)
: BaseMessageCommandContext(interaction),
IRestClientContext,
IChannelContext,
IUserContext
{
public RestClient Client { get; } = client;
public RestClient Client => client;
public TextChannel Channel => Interaction.Channel;
public User User => Interaction.User;
public RestMessage Target => Interaction.Data.TargetMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ namespace NetCord.Services.ApplicationCommands;

public class BaseAutocompleteInteractionContext(AutocompleteInteraction interaction) : IAutocompleteInteractionContext
{
public virtual AutocompleteInteraction Interaction { get; } = interaction;
public AutocompleteInteraction Interaction => interaction;
}

public class AutocompleteInteractionContext(AutocompleteInteraction interaction, GatewayClient client) : BaseAutocompleteInteractionContext(interaction), IGatewayClientContext, IGuildContext, IChannelContext, IUserContext
public class AutocompleteInteractionContext(AutocompleteInteraction interaction, GatewayClient client)
: BaseAutocompleteInteractionContext(interaction),
IGatewayClientContext,
IGuildContext,
IChannelContext,
IUserContext
{
public GatewayClient Client { get; } = client;
public GatewayClient Client => client;
public Guild? Guild => Interaction.Guild;
public TextChannel Channel => Interaction.Channel;
public User User => Interaction.User;
Expand Down
11 changes: 8 additions & 3 deletions NetCord.Services/Commands/CommandContexts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ namespace NetCord.Services.Commands;

public class BaseCommandContext(Message message) : ICommandContext
{
public Message Message { get; } = message;
public Message Message => message;
}

public class CommandContext(Message message, GatewayClient client) : BaseCommandContext(message), IGatewayClientContext, IGuildContext, IChannelContext, IUserContext
public class CommandContext(Message message, GatewayClient client)
: BaseCommandContext(message),
IGatewayClientContext,
IGuildContext,
IChannelContext,
IUserContext
{
public GatewayClient Client { get; } = client;
public GatewayClient Client => client;
public Guild? Guild => Message.Guild;
public TextChannel? Channel => Message.Channel;
public User User => Message.Author;
Expand Down
Loading

0 comments on commit 2341605

Please sign in to comment.