Skip to content

Commit

Permalink
Added default perms to slash commands
Browse files Browse the repository at this point in the history
  • Loading branch information
BrammyS committed Aug 5, 2021
1 parent c08077e commit 960bfa0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SlashCommandAttribute : Attribute
/// </summary>
/// <param name="name">The name of the command.</param>
/// <param name="description">The description of what the command does.</param>
/// <param name="defaultPermission">Whether the command is enabled by default when the app is added to a guild. Default: true.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="name" /> or <paramref name="description" /> doesn't
/// match the command name requirements.
Expand All @@ -24,7 +25,7 @@ public class SlashCommandAttribute : Attribute
/// Thrown when <paramref name="name" /> or <paramref name="description" /> is
/// null.
/// </exception>
public SlashCommandAttribute(string name, string description)
public SlashCommandAttribute(string name, string description, bool defaultPermission = true)
{
if (name.Length is < 1 or > 32) throw new ArgumentException("Command names must be between 1 and 32 characters.");

Expand All @@ -36,6 +37,7 @@ public SlashCommandAttribute(string name, string description)

Name = name ?? throw new ArgumentNullException(nameof(name), "Command name can not be null");
Description = description ?? throw new ArgumentNullException(nameof(description), "Command description can not be null");
DefaultPermission = defaultPermission;
}

/// <summary>
Expand All @@ -47,5 +49,10 @@ public SlashCommandAttribute(string name, string description)
/// The description of what the command does.
/// </summary>
public string Description { get; }

/// <summary>
/// Whether the command is enabled by default when the app is added to a guild.
/// </summary>
public bool DefaultPermission { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SlashCommandGroupAttribute : Attribute
/// </summary>
/// <param name="name">The name of the command group.</param>
/// <param name="description">The description of what the command group does.</param>
/// <param name="defaultPermission">Whether the command is enabled by default when the app is added to a guild. Default: true.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="name" /> or <paramref name="description" /> doesn't
/// match the command group name requirements.
Expand All @@ -24,7 +25,7 @@ public class SlashCommandGroupAttribute : Attribute
/// Thrown when <paramref name="name" /> or <paramref name="description" /> is
/// null.
/// </exception>
public SlashCommandGroupAttribute(string name, string description)
public SlashCommandGroupAttribute(string name, string description, bool defaultPermission = true)
{
if (name.Length is < 1 or > 32) throw new ArgumentException("Command group names must be between 1 and 32 characters.");

Expand All @@ -36,6 +37,7 @@ public SlashCommandGroupAttribute(string name, string description)

Name = name ?? throw new ArgumentNullException(nameof(name), "Command group name can not be null");
Description = description ?? throw new ArgumentNullException(nameof(description), "Command group description can not be null");
DefaultPermission = defaultPermission;
}

/// <summary>
Expand All @@ -47,5 +49,10 @@ public SlashCommandGroupAttribute(string name, string description)
/// The description of what the command group is.
/// </summary>
public string Description { get; }

/// <summary>
/// Whether the command is enabled by default when the app is added to a guild.
/// </summary>
public bool DefaultPermission { get; }
}
}
5 changes: 5 additions & 0 deletions src/Color-Chan.Discord.Commands/Info/ISlashCommandInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public interface ISlashCommandInfo
/// </summary>
public string Description { get; set; }

/// <summary>
/// Whether the command is enabled by default when the app is added to a guild.
/// </summary>
public bool DefaultPermission { get; set; }

/// <summary>
/// The <see cref="MethodInfo" /> containing the method of the command.
/// </summary>
Expand Down
11 changes: 9 additions & 2 deletions src/Color-Chan.Discord.Commands/Info/SlashCommandInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public class SlashCommandInfo : ISlashCommandInfo
/// </summary>
/// <param name="name">The name of the command.</param>
/// <param name="description">The description of the command.</param>
/// <param name="defaultPermission">Whether the command is enabled by default when the app is added to a guild.</param>
/// <param name="command">The <see cref="MethodInfo" /> containing the method of the command.</param>
/// <param name="module">The command module containing the <see cref="CommandMethod" />.</param>
public SlashCommandInfo(string name, string description, MethodInfo command, TypeInfo module)
public SlashCommandInfo(string name, string description, bool defaultPermission, MethodInfo command, TypeInfo module)
{
CommandName = name;
Description = description;
DefaultPermission = defaultPermission;
CommandMethod = command;
ParentModule = module;
}
Expand All @@ -27,11 +29,13 @@ public SlashCommandInfo(string name, string description, MethodInfo command, Typ
/// </summary>
/// <param name="name">The name of the command.</param>
/// <param name="description">The description of the command.</param>
/// <param name="defaultPermission">Whether the command is enabled by default when the app is added to a guild.</param>
/// <param name="module">The command module containing the <see cref="CommandMethod" />.</param>
public SlashCommandInfo(string name, string description, TypeInfo module)
public SlashCommandInfo(string name, string description, bool defaultPermission, TypeInfo module)
{
CommandName = name;
Description = description;
DefaultPermission = defaultPermission;
ParentModule = module;
}

Expand All @@ -41,6 +45,9 @@ public SlashCommandInfo(string name, string description, TypeInfo module)
/// <inheritdoc />
public string Description { get; set; }

/// <inheritdoc />
public bool DefaultPermission { get; set; }

/// <inheritdoc />
public MethodInfo? CommandMethod { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public IEnumerable<DiscordCreateApplicationCommand> BuildSlashCommandsParams(IEn
{
Name = commandInfo.CommandName,
Description = commandInfo.Description,
Options = options
Options = options,
DefaultPermission = commandInfo.DefaultPermission
});
}

Expand All @@ -136,7 +137,7 @@ public IEnumerable<DiscordCreateApplicationCommand> BuildSlashCommandsParams(IEn
var commandAttribute = validMethod.GetCustomAttribute<SlashCommandAttribute>();
if (commandAttribute is not null)
{
var commandInfo = new SlashCommandInfo(commandAttribute.Name, commandAttribute.Description, validMethod, parentModule)
var commandInfo = new SlashCommandInfo(commandAttribute.Name, commandAttribute.Description, commandAttribute.DefaultPermission, validMethod, parentModule)
{
Guilds = _guildBuildService.GetCommandGuilds(validMethod),
CommandOptions = _optionBuildService.GetCommandOptions(validMethod).ToList(),
Expand All @@ -153,7 +154,7 @@ public IEnumerable<DiscordCreateApplicationCommand> BuildSlashCommandsParams(IEn
private KeyValuePair<string, ISlashCommandInfo> BuildCommandGroupInfoKeyValuePair(SlashCommandGroupAttribute groupAttribute, TypeInfo parentModule)
{
// Build the command group.
var commandGroup = new SlashCommandInfo(groupAttribute.Name, groupAttribute.Description, parentModule)
var commandGroup = new SlashCommandInfo(groupAttribute.Name, groupAttribute.Description, groupAttribute.DefaultPermission, parentModule)
{
CommandOptions = new List<ISlashCommandOptionInfo>(),
Guilds = _guildBuildService.GetCommandGuilds(parentModule),
Expand Down

0 comments on commit 960bfa0

Please sign in to comment.