Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

State documentation uses outdated example for user discriminator query #552

Closed
jchristgit opened this issue Apr 19, 2024 · 3 comments · Fixed by #574
Closed

State documentation uses outdated example for user discriminator query #552

jchristgit opened this issue Apr 19, 2024 · 3 comments · Fixed by #574
Assignees

Comments

@jchristgit
Copy link
Collaborator

As brought up by @jb3, the state documentation currently mentions an example of
querying users within nosedrum by matching via username and discriminator,
whilst discriminator for active user accounts are no longer in use. This might
have to be fixed in nosedrum itself, but in the meantime we could update the
documentation to use the username attribute only as it is supposed to be unique
for user accounts these days. A question for @jb3 here, could the query return
multiple users then, if you query for, for instance, an inactive user?

@jb3
Copy link
Collaborator

jb3 commented Apr 21, 2024

I'm not sure what you mean by an inactive user here, but if you can clarify I can try answer.

I suggest we just swap out the query for something else that demonstrates more advanced state/QLC usage for cache querying, querying by just username does not feel like a great example, let's at least look for more fields.

I think we can show some more example uses, just a few off the top of my head (though I have tested them, and they do work), let me know what you think of these (they may also be a totally wrong approach to writing QLC queries, let me know if that is the case also):

-module(nostrum_queries).
-export([find_role_users/4, find_large_communities/2]).

-include_lib("stdlib/include/qlc.hrl").

% Find the Nostrum.Struct.User and Member objects of all members in a specific guild role.
find_role_users(RequestedGuildId, RoleId, MemberCache, UserCache) ->
    qlc:q([{User, Member} || {{GuildId, MemberId}, Member} <- MemberCache:query_handle(),
                   % Filter to member objects of the selected guild
                   GuildId =:= RequestedGuildId,
                   % Filter to members of the provided role
                   lists:member(RoleId, map_get(roles, Member)),
                   % Get a handle on the UserCache table
                   {UserId, User} <- UserCache:query_handle(),
                   % Find the User struct that matches the found Member
                   MemberId =:= UserId]).

% Find all communities in the Guild cache with the COMMUNITY guild feature
% that are over a certain threshold in user size
find_large_communities(Threshold, GuildCache) ->
    qlc:q([Guild || {_, Guild} <- GuildCache:query_handle(),
                    % Filter for guilds that are over the provided size
                    map_get(member_count, Guild) > Threshold,
                    % Filter for guilds that have COMMUNITY in the features field
                    lists:member(<<"COMMUNITY">>, map_get(features, Guild))]).

(Related, QLC cheatsheet for #553?)

@jb3 jb3 self-assigned this Apr 22, 2024
@jchristgit
Copy link
Collaborator Author

I like this idea and also the idea of a QLC cheatsheet, as far as I'm aware the queries above won't work as-is since the QLC compiler expects guard tests to be passed in the list comprehension, so a lists:member call does not work as far as I'm aware

@jb3
Copy link
Collaborator

jb3 commented Apr 24, 2024

Discussion in Discord shows that we can use lists:member and it does seem to be doing this efficiently. I think we're good to put in those two examples into the state documentation as well as a QLC cheatsheet (I can add into #559).

jb3 added a commit that referenced this issue May 3, 2024
Updates state docs with the previously agreed upon QLC examples in issue

Additionally, provides an example of how to evaluate a QLC query and
return the result to use within Elixir.

Closes #552
jb3 added a commit that referenced this issue May 3, 2024
Updates state docs with the previously agreed upon QLC examples in issue

Additionally, provides an example of how to evaluate a QLC query and
return the result to use within Elixir.

Closes #552
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants