Exemplo n.º 1
0
        /// <summary>
        /// Handles updating the permissions for guild specific slash commands
        /// </summary>
        /// <param name="c">The socket application command that needs their permissions updated</param>
        /// <returns>A task representing the completion of registering the guild command permissions</returns>
        public async Task HandleGuildCommandPerms(SocketApplicationCommand c)
        {
            if (!_commands.ReflectionCommands.ContainsKey(c.Name))
            {
                return;
            }

            if (_commands.ReflectionCommands[c.Name].Attribute is not GuildCommandAttribute gcmd ||
                gcmd.RoleIds.Length <= 0)
            {
                return;
            }

            var url = $"https://discord.com/api/v8/applications/{c.ApplicationId}/guilds/{gcmd.GuildId}/commands/{c.Id}/permissions";

            var perms = new List <ApplicationCommandPermission>();

            foreach (var roleId in gcmd.RoleIds)
            {
                perms.Add(new(roleId, ApplicationCommandPermissionType.Role, true));
            }

            var p = new ApplicationCommandPermissions(perms.ToArray());

            await _api.Put <object, ApplicationCommandPermissions>(url, p, c =>
            {
                c.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bot", Token);
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks the similarity between the already registered commands and the commands gotten from reflection
        /// </summary>
        /// <param name="a">The slash command from reflection</param>
        /// <param name="bc">The slash command registered with discord already</param>
        /// <returns>Whether or not the two commands are the same.</returns>
        public bool Validate(SlashCommandProperties a, SocketApplicationCommand bc)
        {
            var oc = a.Options.IsSpecified ? a.Options.Value.Count : 0;

            if (!ValidateOptional(a.Name, bc.Name) ||
                !ValidateOptional(a.Description, bc.Description) ||
                !ValidateOptional(a.IsDefaultPermission, bc.IsDefaultPermission) ||
                oc != bc.Options.Count)
            {
                return(false);
            }

            if (oc == 0 && bc.Options.Count == 0)
            {
                return(true);
            }

            return(ValidateOptions(a.Options.Value, bc.Options.ToList()));
        }