zaida04 / guilded.js

A library for creating bots with the guilded.gg bot API.
https://guilded.js.org
MIT License
67 stars 15 forks source link

De-complex event types #212

Closed zaida04 closed 1 year ago

zaida04 commented 1 year ago

Is your feature request related to a problem? Please describe. I've realized that a lot of our types for events are overly complex and can lead to a frustrating developer experience when it comes to handling these events. Take the memberUnbanned event. The parameter that's emitted could be either a MemberBan object, or the raw ws event data itself. This leads to having to do the following:

client.on("memberUnbanned", (memberBan) => {
        const isMemberBanObj = memberBan instanceof MemberBan;
        const reason = isMemberBanObj ? memberBan.reason : memberBan.serverMemberBan.reason;
        const userId = isMemberBanObj ? memberBan.target.id : memberBan.serverMemberBan.user.id;
        const authorId = isMemberBanObj ? memberBan.createdById : memberBan.serverMemberBan.createdBy;
});

This is a sub-par developer experience. A developer shouldn't have to structure their event to handle two different potential objects. This isn't the only event that this happens in, as it also happens in:

Describe the solution you'd like For events that emit either a structure or the ws data dependent on if a structure is cached or not, just go with the ws data (example, just make the type WSChatMessageDeletedPayload["d"]). That makes it a more simple interface, and also lets the user decide if they need the actual structure from cache or not.