Skip to content

Commit

Permalink
Update objects, new events, some bug fixes like kind field in objects…
Browse files Browse the repository at this point in the history
… not being properly set to some objects and also updating messages table in GuildChannel.
  • Loading branch information
krisppurg committed Jul 11, 2024
1 parent 0ba8315 commit d093a03
Show file tree
Hide file tree
Showing 9 changed files with 505 additions and 67 deletions.
99 changes: 83 additions & 16 deletions dimscord/constants.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type
permCreateEvents
permUseExternalSounds
permSendVoiceMessages
permUsePolls = 49
permUseExternalApps
GatewayIntent* = enum
giGuilds,
giGuildMembers,
Expand All @@ -77,7 +79,9 @@ type
giMessageContent,
giGuildScheduledEvents = 16,
giAutoModerationConfiguration = 20,
giAutoModerationExecution
giAutoModerationExecution,
giGuildMessagePolls = 24,
giDirectMessagePolls
AuditLogChangeType* = enum
alcString,
alcInt,
Expand Down Expand Up @@ -223,6 +227,11 @@ type
mtStageSpeaker = 29
mtStageTopic = 31
mtGuildApplicationPremiumSubscription = 32
mtGuildIncidentAlertModeEnabled = 36
mtGuildIncidentAlertModeDisabled = 37
mtGuildIncidentReportRaid = 38
mtGuildIncidentReportFalseAlarm = 39
mtPurchaseNotification = 44
MessageActivityType* = enum
matJoin = 1
matSpectate = 2
Expand All @@ -243,8 +252,14 @@ type
ctGuildDirectory = 14
ctGuildForum = 15
ctGuildMedia = 16
VideoQualityMode* = enum
vqmAuto = 0
vqmFull = 1
ReactionType* = enum
rtNormal = 0
rtBurst = 1
MessageNotificationLevel* = enum
mnlAllMessages = 0
mnlAllMessages = 0
mnlOnlyMentions = 1
ExplicitContentFilter* = enum
ecfDisabled = 0
Expand All @@ -270,12 +285,13 @@ type
ptTier2 = 2
ptTier3 = 3
ActivityType* = enum
atPlaying = 0
atStreaming = 1
atListening = 2
atWatching = 3
atCustom = 4
atCompeting = 5
atPlaying = 0
atStreaming = 1
atListening = 2
atWatching = 3
atCustom = 4
atCompeting = 5
atCustomState = 6
WebhookType* = enum
whIncoming = 1
whFollower = 2
Expand Down Expand Up @@ -332,14 +348,21 @@ type
aleThreadUpdate = 111
aleThreadDelete = 112
aleApplicationCommandPermissionUpdate = 121
aleAutoModerationRuleCreate = 140
aleAutoModerationRuleUpdate = 141
aleAutoModerationRuleDelete = 142
aleAutoModerationBlockMessage = 143
aleAutoModerationFlagToChannel = 144
aleAutoModerationUserMuted = 145
aleCreatorMonetizationRequestCreated = 150
aleCreatorMonetizationTermsAccepted = 151
aleAutoModerationRuleCreate = 140
aleAutoModerationRuleUpdate = 141
aleAutoModerationRuleDelete = 142
aleAutoModerationBlockMessage = 143
aleAutoModerationFlagToChannel = 144
aleAutoModerationUserMuted = 145
aleCreatorMonetizationRequestCreated = 150
aleCreatorMonetizationTermsAccepted = 151
aleOnboardingPromptCreate = 163
aleOnboardingPromptUpdate = 164
aleOnboardingPromptDelete = 165
aleOnboardingCreate = 166
aleOnboardingUpdate = 167
aleHomeSettingsCreate = 190
aleHomeSettingsUpdate = 191
TeamMembershipState* = enum
tmsInvited = 1 # not to be confused with "The Mysterious Song" lol
tmsAccepted = 2
Expand Down Expand Up @@ -399,6 +422,13 @@ type
irtUpdateMessage = 7
irtAutoCompleteResult = 8
irtModal = 9
ApplicationIntegrationType* = enum
aitGuildInstall = 0
aitUserInstall = 1
InviteType* = enum
itGuild = 0
itGroupDm = 1
itFriend = 2
InviteTargetType* = enum
ittStream = 1
ittEmbeddedApplication = 2
Expand Down Expand Up @@ -431,6 +461,8 @@ type
StickerType* = enum
stStandard = 1
stGuild = 2
PollLayoutType* = enum
plDefault = 1
GuildScheduledEventPrivacyLevel* = enum
splGuildOnly = 2
GuildScheduledEventStatus* = enum
Expand Down Expand Up @@ -469,6 +501,15 @@ type
GuildOnboardingPromptType* = enum
ptMultipleChoice = 0,
ptDropdown = 1
EntitlementType* = enum
etPurchase = 1
etPremiumSubscription,
etDeveloperGift,
etTestModePurchase,
etFreePurchase,
etUserGift,
etPremiumPurchase,
etApplicationSubscription
DispatchEvent* = enum
Unknown
VoiceStateUpdate = "VOICE_STATE_UPDATE"
Expand Down Expand Up @@ -526,6 +567,14 @@ type
AutoModerationRuleUpdate = "AUTO_MODERATION_RULE_UPDATE"
AutoModerationRuleDelete = "AUTO_MODERATION_RULE_DELETE"
AutoModerationActionExecution = "AUTO_MODERATION_ACTION_EXECUTION"
MessagePollVoteAdd = "MESSAGE_POLL_VOTE_ADD"
MessagePollVoteRemove = "MESSAGE_POLL_VOTE_REMOVE"
IntegrationCreate = "INTEGRATION_CREATE"
IntegrationUpdate = "INTEGRATION_UPDATE"
IntegrationDelete = "INTEGRATION_DELETE"
EntitlementCreate = "ENTITLEMENT_CREATE"
EntitlementUpdate = "ENTITLEMENT_UPDATE"
EntitlementDelete = "ENTITLEMENT_DELETE"

const
deGuildMembersChunk* = DispatchEvent.GuildMembersChunk
Expand Down Expand Up @@ -850,6 +899,12 @@ proc endpointChannelThreadsMembers*(cid: string; uid = ""): string =
if uid != "":
result = result & "/" & uid

proc endpointChannelPollsAnswer*(cid, mid, aid: string): string =
result = endpointChannels(cid) & "/polls/" & mid & "/answers/" & aid

proc endpointChannelPollsExpire*(cid, mid: string): string =
result = endpointChannels(cid) & "/polls/" & mid & "/expire"

proc endpointChannelMessagesCrosspost*(cid, mid: string): string =
result = endpointChannelMessages(cid, mid) & "/crosspost"

Expand All @@ -859,6 +914,9 @@ proc endpointChannelInvites*(cid: string): string =
proc endpointChannelPermissions*(cid, oid: string): string =
result = endpointChannels(cid) & "/permissions/" & oid

proc endpointGuildBanBulk*(gid: string; uid = ""): string =
result = endpointGuilds(gid) & "/bulk-ban"

proc endpointGuildBans*(gid: string; uid = ""): string =
result = endpointGuilds(gid) & "/bans" & (if uid != "": "/" & uid else: "")

Expand All @@ -883,9 +941,18 @@ proc endpointReactions*(cid, mid: string; e, uid = ""): string =
if uid != "":
result = result & "/" & uid

proc endpointApplications*(aid="@me"): string =
result = "applications/"&aid

proc endpointOAuth2Application*(): string =
result = "oauth2/applications/@me"

proc endpointEntitlements*(aid: string; eid = ""): string =
result = "applications/"&aid&"/entitlements"&(if eid!="":"/"&eid else:"")

proc endpointEntitlementConsume*(aid, eid: string): string =
result = endpointEntitlements(aid, eid) & "/consume"

proc endpointGlobalCommands*(aid: string; cid = ""): string =
result = "applications/" & aid & "/commands" & (if cid!="":"/"&cid else:"")

Expand Down
136 changes: 136 additions & 0 deletions dimscord/dispatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ proc messageCreate(s: Shard, data: JsonNode) {.async.} =

asyncCheck chan.addMsg(msg, $data, s.cache.preferences)
chan.last_message_id = msg.id
s.cache.guild(chan).channels[chan.id] = chan # because it's updated.

elif msg.channel_id in s.cache.dmChannels:
let chan = s.cache.dmChannels[msg.channel_id]
Expand Down Expand Up @@ -247,11 +248,17 @@ proc messageReactionAdd(s: Shard, data: JsonNode) {.async.} =

if data["user_id"].str == s.user.id:
reaction.reacted = true
reaction.me_burst = data["burst"].getBool

msg.reactions[$emoji] = reaction
else:
reaction.count += 1
reaction.reacted = data["user_id"].str == s.user.id
reaction.burst = data["burst"].getBool
reaction.me_burst = reaction.reacted and reaction.burst
if "burst_colors" in data:
reaction.burst_colors=data["burst_colors"].getElems.mapIt(it.getStr)

msg.reactions[$emoji] = reaction

s.checkAndCall(MessageReactionAdd, msg, user, emoji, exists)
Expand Down Expand Up @@ -292,6 +299,7 @@ proc messageReactionRemove(s: Shard, data: JsonNode) {.async.} =

if data["user_id"].str == s.user.id:
reaction.reacted = false
if reaction.burst: reaction.me_burst = false

msg.reactions[$emoji] = reaction
else:
Expand Down Expand Up @@ -577,6 +585,13 @@ proc guildMemberUpdate(s: Shard, data: JsonNode) {.async.} =
for role in data["roles"].elems:
member.roles.add(role.str)

if "flags" in data and data["flags"].kind != JNull:
member.flags = cast[set[GuildMemberFlags]](data["flags"].getInt)
if "avatar_decoration_data" in data and data["avatar_decoration_data"].kind!=JNull:
member.avatar_decoration_data=($data["avatar_decoration_data"]).fromJson(
typeof(member.avatar_decoration_data)
)

s.checkAndCall(GuildMemberUpdate, guild, member, oldMember)

proc guildMemberRemove(s: Shard, data: JsonNode) {.async.} =
Expand Down Expand Up @@ -999,6 +1014,116 @@ proc voiceServerUpdate(s: Shard, data: JsonNode) {.async.} =
guild, data["token"].str,
endpoint, initial)

proc messagePollVoteAdd(s: Shard, data: JsonNode) {.async.} =
var
gc: GuildChannel = nil
dm: DMChannel = nil
user = s.cache.users.getOrDefault(data["user_id"].str,
User(id: data["user_id"].str))
msg = Message(id: data["message_id"].str,
channel_id: data["channel_id"].str)
counts: seq[PollAnswerCount] = @[]

if "guild_id" in data:
let guild = s.cache.guilds.getOrDefault(data["guild_id"].str,
Guild(id: data["guild_id"].str)
)
msg.guild_id = some guild.id

if msg.channel_id in guild.channels:
gc = guild.channels[msg.channel_id]
if msg.id in gc.messages:
msg = gc.messages[msg.id]
counts = msg.poll.get.results.get(PollResults()).answer_counts

elif msg.channel_id in s.cache.dmChannels:
dm = s.cache.dmchannels[msg.channel_id]
msg = dm.messages.getOrDefault(msg.id, msg)

counts = msg.poll.get.results.get(PollResults()).answer_counts

for ac in counts:
if ac.id != data["answer_id"].getInt: continue
ac.count += 1
ac.me_voted = user.id == s.user.id

if msg.poll.isSome: # just fyi, it will always be true for cached messages only both dm and guild.
if gc != nil:
gc.messages[msg.id] = msg
s.cache.gchannel(msg).messages[msg.id] = msg
else:
dm.messages[msg.id] = msg

s.checkAndCall(MessagePollVoteAdd,
data["answer_id"].getInt, msg, user)

proc messagePollVoteRemove(s: Shard, data: JsonNode) {.async.} =
var
gc: GuildChannel = nil
dm: DMChannel = nil
user = s.cache.users.getOrDefault(data["user_id"].str,
User(id: data["user_id"].str))
msg = Message(id: data["message_id"].str,
channel_id: data["channel_id"].str)
counts: seq[PollAnswerCount] = @[]

if "guild_id" in data:
let guild = s.cache.guilds.getOrDefault(data["guild_id"].str,
Guild(id: data["guild_id"].str)
)
msg.guild_id = some guild.id

if msg.channel_id in guild.channels:
gc = guild.channels[msg.channel_id]
msg = gc.messages.getOrDefault(msg.id, msg)
counts = msg.poll.get.results.get(PollResults()).answer_counts

elif msg.channel_id in s.cache.dmChannels:
dm = s.cache.dmchannels[msg.channel_id]
msg = dm.messages.getOrDefault(msg.id, msg)

counts = msg.poll.get.results.get(PollResults()).answer_counts

for ac in counts:
if ac.id != data["answer_id"].getInt: continue
if ac.count != 0: ac.count -= 1
ac.me_voted = user.id == s.user.id

if msg.poll.isSome:
if gc != nil:
gc.messages[msg.id] = msg
s.cache.gchannel(msg).messages[msg.id] = msg
else:
dm.messages[msg.id] = msg

s.checkAndCall(MessagePollVoteRemove,
data["answer_id"].getInt, msg, user)

proc integrationCreate(s: Shard, data: JsonNode) {.async.} =
let user = newUser(data["user"])
if user.id in s.cache.users: s.cache.users[user.id] = user
let guild = s.cache.guilds.getOrDefault(data["guild_id"].str,
Guild(id: data["guild_id"].str)
)
s.checkAndCall(IntegrationCreate, user, guild)

proc integrationUpdate(s: Shard, data: JsonNode) {.async.} =
let user = newUser(data["user"])
if user.id in s.cache.users: s.cache.users[user.id] = user
let guild = s.cache.guilds.getOrDefault(data["guild_id"].str,
Guild(id: data["guild_id"].str)
)
s.checkAndCall(IntegrationUpdate, user, guild)

proc integrationDelete(s: Shard, data: JsonNode) {.async.} =
var app_id = none(string)
if "application_id" in data and data["application_id"].kind != JNull:
app_id = some data["application_id"].str
let guild = s.cache.guilds.getOrDefault(data["guild_id"].str,
Guild(id: data["guild_id"].str)
)
s.checkAndCall(IntegrationDelete, data["id"].str, guild, app_id)

proc handleEventDispatch*(s:Shard, event:DispatchEvent, data:JsonNode){.async.} =
case event:
of VoiceStateUpdate: await s.voiceStateUpdate(data)
Expand Down Expand Up @@ -1075,4 +1200,15 @@ proc handleEventDispatch*(s:Shard, event:DispatchEvent, data:JsonNode){.async.}
of AutoModerationRuleDelete: await s.autoModerationRuleDelete data
of AutoModerationActionExecution:
await s.autoModerationActionExecution(data)
of MessagePollVoteAdd: await s.messagePollVoteAdd(data)
of MessagePollVoteRemove: await s.messagePollVoteRemove(data)
of IntegrationCreate: await s.integrationCreate(data)
of IntegrationUpdate: await s.integrationUpdate(data)
of IntegrationDelete: await s.integrationDelete(data)
of EntitlementCreate:
s.checkAndCall(EntitlementCreate, newEntitlement(data))
of EntitlementUpdate:
s.checkAndCall(EntitlementUpdate, newEntitlement(data))
of EntitlementDelete:
s.checkAndCall(EntitlementDelete, newEntitlement(data))
of Unknown: discard
Loading

0 comments on commit d093a03

Please sign in to comment.