You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have tried to get all reactions to a message, but Api.get_reactions only returns the first 25.
So I thought, yeah, this needs pagination.
I have implemented a way to get all reactions to a message and have used Elixir's own Streams for this.
Here's my example code, it's a dirty "proof of concept" that is copypasted together a little, but here:
defmoduleBot.Library.ReactionsStreamdorequireLoggerdefhandle_request_with_decode(response)defhandle_request_with_decode({:ok,body}),do: {:ok,Jason.decode!(body,keys: :atoms)}defhandle_request_with_decode({:error,_}=error),do: errordefhandle_request_with_decode(response,type)# add_guild_member/3 can return both a 201 and a 204defhandle_request_with_decode({:ok},_type),do: {:ok}defhandle_request_with_decode({:error,_}=error,_type),do: errordefhandle_request_with_decode({:ok,body},type)doconvert=body|>Jason.decode!(keys: :atoms)|>Nostrum.Util.cast(type){:ok,convert}enddefrequest(channel_id,message_id,emoji,after_id\\0)doparams=caseafter_iddo0->[limit: 100]num->[after: num,limit: 100]endroute=Nostrum.Constants.channel_reactions_get(channel_id,message_id,emoji)Nostrum.Api.request(:get,route,"",params: params)|>handle_request_with_decodeenddefpstart(channel_id,message_id,emoji)dofn->{[],channel_id,message_id,emoji,0}endenddefpnext_item({items,channel_id,message_id,emoji,after_id})whenlength(items)==0do{:ok,list}=request(channel_id,message_id,emoji,after_id)Logger.info("fetched new items"<>inspectlength(list))caseEnum.count(list)do0->{:halt,{[],channel_id,message_id,emoji,after_id}}_->next_item({list,channel_id,message_id,emoji,List.last(list).id})endenddefpnext_item({items,channel_id,message_id,emoji,after_id})whenlength(items)>0do{item,items}=List.pop_at(items,0){[item],{items,channel_id,message_id,emoji,after_id}}enddefpstop(_acc)doenddefget_reactions_stream(channel_id,message_id,emoji)doStream.resource(start(channel_id,message_id,emoji),&next_item/1,&stop/1)endend
I have tried to get all reactions to a message, but
Api.get_reactions
only returns the first 25.So I thought, yeah, this needs pagination.
I have implemented a way to get all reactions to a message and have used Elixir's own Streams for this.
Here's my example code, it's a dirty "proof of concept" that is copypasted together a little, but here:
Usage is like this, for example:
Should we add enumerable streams to the API package for paginated queries?
The text was updated successfully, but these errors were encountered: