SinisterRectus / Discordia

Discord API library written in Lua for the Luvit runtime environment
MIT License
711 stars 145 forks source link

Resolve Emoji hashes in Resolvers.emojiId #148

Open Mehgugs opened 6 years ago

Mehgugs commented 6 years ago

Resolvers.emojiId should be able to resolve a custom emoji's hash since it contains an id. This would make emoji functions work for both emoji types when called with data from uncached events, i.e manipulating a reaction after reactionAddUncached.

SinisterRectus commented 6 years ago

Can you give examples? Users generally don't need to deal with name:id, since they shouldn't be making API calls or mentionStrings directly.

Mehgugs commented 6 years ago

https://discordapp.com/channels/81384788765712384/381890411414683648/494619294055923713

myFunction takes an argument which is passed to add reaction and the issue arrises when uncached is called; vanilla emoji still work but custom do not.

SinisterRectus commented 6 years ago

Oh I see. Resolver, by design, does not have the ability to get objects by ID. It only ever returns primitive types. We probably should have different events for custom vs unicode emojis or different parameters passed for the emoji id and name. For now, I think the solution is for the user to do a little leg work:

client:on("reactionAddUncached", function(channel, messageId, hash, userId)
  local message = channel:getMessage(messageId)
  if message and tonumber(hash) then
    local emoji = client:getEmoji(hash)
    if emoji then
      message:addReaction(emoji)
    end
  end
end)

Reactions are one of Discordia's weak points. I'm not sure if I can fix this in 2.x without breaking things.

Mehgugs commented 6 years ago

Yeah, and you can extract an id from a custom hash (afaik) so it's not really an problem for users, but Resolver could in theory reduce a hash to just the id part.

SinisterRectus commented 6 years ago

This will almost certainly not change until the next major version of Discordia.

SinisterRectus commented 3 years ago

In 3.0, I have eliminated "uncached" events and we are now left with just reactionAdd.

In the new ReactionEmoji class, there is now a hash property that handles custom and default emojis:

function get:hash()
    if self._id then
        return self._name .. ':' .. self._id
    else
        return self._name
    end
end

Although, I have not yet wrapped the event's emoji with ReactionEmoji. I still need to figure out how to clean up event payloads.

Additionally, as mentioned in #139, I've added a hash property to the Reaction class:

function get:hash()
    return self.emoji.id or self.emoji.name
end

I think this solves the issue at hand.