yourWaifu / sleepy-discord

C++ library for the Discord chat client. Please use Rust for new bots
https://yourWaifu.github.io/sleepy-discord/
MIT License
707 stars 95 forks source link

Update server cache when role or member is edited #91

Open Qzername opened 6 years ago

Qzername commented 6 years ago

How I can check users permissions?

yourWaifu commented 6 years ago

Issues isn't really for asking questions

roles are stored in the server and serverMember. However the current server class doesn't have roles or members.

Qzername commented 6 years ago

So that library can't check users permissions?

yourWaifu commented 6 years ago

It can but not easily. To do so you'll have to parse the server object to get the list of roles.

Qzername commented 6 years ago

Then, what class I need to use?

yourWaifu commented 6 years ago

You'll need a few things. I wouldn't call them classes as they are JSON object or number. We will need the Guild Member, which you'll need the server or server ID and user ID to get. This will give us the IDs of roles that the user has on a server. From there we check their permissions from their role.

Not to mention, channels can override permissions so you'll also need to check those as well.

Once you have the premission, you can check if the user has a specific permission using a bitwise and.

Qzername commented 6 years ago

So I have ServerID and UserID, What now?

yourWaifu commented 6 years ago

I would wait for me to make the above much easier. I've already done most of the work, however I ran into performance issues when dealing with large servers.

yourWaifu commented 6 years ago

Sorry it took so long, I was working on other project also adding this feature also made the library use a bit more ram and cpu. On Debug, it's a massive difference, but on release, the difference is noticeable but not as big.

Other then that, in commit fb0e517, I added

Permission getPermissions(const Server& server, const ServerMember& member, const Channel& channel) 

and

constexpr bool hasPremission(const Permission& target, const Permission& permission)

Here is an example,

SleepyDiscord::Permission permissions = SleepyDiscord::getPermissions(server, server.members[1], server.channels[0]);
if (hasPremission(permissions, SleepyDiscord::Permission::MANAGE_CHANNELS))
    std::cout << "True\n";

possible output

True
Qzername commented 6 years ago

No problem and Thanks

Qzername commented 6 years ago

One problem, I have got that erros: https://pastebin.com/S0znVgH5 code

if (message.startsWith("test"))
{
SleepyDiscord::Permission permissions = SleepyDiscord::getPermissions(server, server.members[1], server.channels[0]);
if (hasPremission(permissions, SleepyDiscord::Permission::MANAGE_CHANNELS))
    deleteChannel(message.channelID);
else 
    sendMessage(message.channelID, "Nope");
}
yourWaifu commented 6 years ago

It looks like you declared server to be a std:: list. Can I see the declaration of server?

Maybe use server->members instead of server.member

Qzername commented 6 years ago
using SleepyDiscord::DiscordClient::DiscordClient;

    void onServer(SleepyDiscord::Server server) {
        servers.push_front(server);
    }

    std::list<SleepyDiscord::Server> servers;

    void onMessage(SleepyDiscord::Message message)
    {

        SleepyDiscord::Snowflake<SleepyDiscord::Server> serverID;
        auto server = std::find_if(servers.begin(), servers.end(), [&message](SleepyDiscord::Server& server) {
            auto result = message.channelID.findObject(server.channels);
            return result != server.channels.end();
        });
yourWaifu commented 6 years ago

Ahh, I see. server is a std::list::iterator. You may want to read up on InputIterators here https://en.cppreference.com/w/cpp/named_req/InputIterator. In summery, To fix your issue, you need to using *server get a reference of the server and server->members to access members.

I also recommend read up on find as well https://en.cppreference.com/w/cpp/algorithm/find.

Qzername commented 6 years ago

How can I do it?

yourWaifu commented 6 years ago

SleepyDiscord::getPermissions(*server, server->members[1], server->channels[0]);

You may also want to do if (server != servers.end()) to check if a server was found.

I recommend you read up on iterators and find_if and getting an understanding of what the code above is doing before blindly copying and pasting code to avoid confusion.

Qzername commented 6 years ago

Works, but I have problem. It didn't refresh! Example: image


yourWaifu commented 6 years ago

Are those two the same user? Other then that, looking at your code, the behavior makes sense if that is the same user or you edited someone's role. Your servers variable is never updated when a member or role is edited. Now you can still do it but there's nothing in the library to make it easy to do yet. It's actually being worked on, but for now, you'll need to do it the hard way, which I think may be too technical for someone that doesn't know much about the Discord API.

Qzername commented 6 years ago

So I have to wait for new update?