PsychoTea / Oxide.Ext.Discord

Discord extension and integration
https://umod.org/extensions/discord
12 stars 26 forks source link

Reaction errors #2

Closed Slut closed 6 years ago

Slut commented 6 years ago

I am having an issue with reactions where the following will work correctly Message.CreateReaction(Client, "\u2705");

But message.GetReactions(Client, \u2705, (List reactionUsers) =>{}); won't work. "[Discord Ext] An error occured whilst submitting a request to https://discordapp.com/api/channels/294799450331938817/messages/383233261364576267/reactions/✅/ (code NotFound): {"code": 0, "message": "404: Not Found"}"

PsychoTea commented 6 years ago

Just from a quick look it seems like that message ID doesn't exist, hence the 404 error. Would you be able to provide some sample code so I can test? Thanks.

Slut commented 6 years ago

Sorry totally forgot about this issue as I haven't bothered to work on the plugin, here's my code below.

CreateMessageTest() should work perfectly and add reactions to it while HandleReactionAdd won't. HandleReactionAdd checks to see if the Reaction added is from an ID that is allowed because discord doesn't have a perm that stops them from adding to existing reactions, as you can see they are both using the same reactions string which works for CreateReaction. If there's anything more you need lemme know because it's just snippets from my code.

Also I think it is urgent to add a queue for requests because for some reason I get ratelimit issues before my plugin can fully execute the code. Thanks.

public Channel getTextChannelByName(string name)
        {
            foreach (var channel in Client.DiscordServer.channels)
            {
                if (!channel.name.Equals(name)) continue;
                return channel;
            }
            return null;
        }
        private const string yesReaction = "\u2705";
        private const string noReaction = "\u274C";
        private void CreateMessageTest()
        {
            Message msg = new Message();
            msg.content = "test";
            var channel = getTextChannelByName("reports");
            if (channel == null)
                LogError("Failed to find channel #reports");
            channel.CreateMessage(Client, msg, (Message) =>
            {
                Message.CreateReaction(Client, yesReaction);
                Message.CreateReaction(Client, noReaction);
            });
        }
        private void Discord_MessageReactionAdd(MessageReactionUpdate Update)
        {
            User.GetCurrentUser(Client, (User) => { selfUserId = User.id; HandleReactionAdd(Update); });
        }
        private void HandleReactionAdd(MessageReactionUpdate Update)
        {
            if (Update.user_id.Equals(selfUserId)) return;
            if (!allowedIDs.Contains(Update.user_id))
            {
                getTextChannelByName("reports").GetChannelMessages(Client, (List<Message> Messages) =>
                {
                    foreach (var message in Messages)
                    {
                        if (!message.id.Equals(Update.message_id)) continue;
                        message.GetReactions(Client, yesReaction, (List<User> reactionUsers) =>
                        {
                            User.GetUser(Client, Update.user_id, (User) =>
                            {
                                if (reactionUsers.Contains(User))
                                {
                                    //Remove reactions (how?)
                                }
                            });
                        });
                        message.GetReactions(Client, noReaction, (List<User> reactionUsers) =>
                        {
                            User.GetUser(Client, Update.user_id, (User) =>
                            {
                                if (reactionUsers.Contains(User))
                                {
                                    //Remove reactions (how?)
                                }
                            });
                        });
                    }
                });
            }
        }
PsychoTea commented 6 years ago

The latest PR to master should fix the issue. It turns out there is some unusual behaviour on Discord's side where some REST methods are happy to accept unicode characters in the literal form, whereas the 'GetReactions' endpoint requires the character in a hexcode form. This is now done manually within the Ext.

Also I think it is urgent to add a queue for requests because for some reason I get ratelimit issues before my plugin can fully execute the code. Thanks. The project is open source, feel free to clone and PR it ;) Sarcasm aside, I'll work on it when I get some time. It's quite a big element to implement and will require quite some time. For now, I would recommend making as few REST calls as possible at once (read: not in foreach loops 😛 ).

You also may now need to install the System.Web library in your game server's managed folder. This can be found at C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Web.dll, at least in my case.