Skip to content

Commit

Permalink
Added guild template endpoints, stickers, user flags enum, message re…
Browse files Browse the repository at this point in the history
…ference in sendmessage, couple of changes as well
  • Loading branch information
krisppurg committed Dec 25, 2020
1 parent 64525dd commit 2ba022d
Show file tree
Hide file tree
Showing 24 changed files with 1,028 additions and 478 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@

#### Please Do NOT:
* Make spam comments or less detailed issues.
* Make command handler PRs or issues. There is no need for a command handler (read https://github.com/krisppurg/dimscord/pull/6#issuecomment-620716071)
* Make command handler PRs or issues. [There is already a command handler](https://github.com/ire4ever1190/dimscmd)
* Make lines over 80-81 characters, unless it's comments which limits to around 120 characters.
37 changes: 31 additions & 6 deletions dimscord/constants.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ type
mfSupressEmbeds,
mfSourceMessageDeleted
mfUrgent
UserFlags* = enum
ufNone,
ufDiscordEmployee,
ufPartneredServerOwner,
ufHypesquadEvents,
ufBugHunterLevel1,
ufHouseBravery = 64,
ufHouseBrilliance,
ufHouseBalance,
ufEarlySupporter,
ufTeamUser,
ufSystem = 4096
ufBugHunterLevel2 = 16384
ufVerifiedBot = 65536,
ufEarlyVerifiedBotDeveloper
const
libName* = "Dimscord"
libVer* = "1.2.0"
Expand Down Expand Up @@ -201,6 +216,10 @@ type
TeamMembershipState* = enum
tmsInvited = 1 # not to be confused with "The Mysterious Song" lol
tmsAccepted = 2
MessageStickerFormat* = enum
msfPng = 1
msfAPng = 2
msfLottie = 3
ApplicationCommandOptionType* = enum
acotSubCommand = 1
acotSubCommandGroup = 2
Expand Down Expand Up @@ -299,7 +318,7 @@ proc endpointGuildAuditLogs*(gid: string): string =
result = endpointGuilds(gid) & "/audit-logs"

proc endpointGuildMembers*(gid: string; mid = ""): string =
result = endpointGuilds(gid) & "/members" & (if mid != "": "/" & mid else: "")
result = endpointGuilds(gid) & "/members" & (if mid != "":"/"&mid else: "")

proc endpointGuildMembersNick*(gid: string; mid = "@me"): string =
result = endpointGuildMembers(gid, mid) & "/nick"
Expand All @@ -308,7 +327,7 @@ proc endpointGuildMembersRole*(gid, mid, rid: string): string =
result = endpointGuildMembers(gid, mid) & "/roles/" & rid

proc endpointGuildIntegrations*(gid: string; iid = ""): string =
result = endpointGuilds(gid) & "/integrations" & (if iid != "": "/" & iid else: "")
result = endpointGuilds(gid)&"/integrations"&(if iid!="":"/"&iid else:"")

proc endpointGuildIntegrationsSync*(gid, iid: string): string =
result = endpointGuildIntegrations(gid, iid) & "/sync"
Expand All @@ -317,10 +336,10 @@ proc endpointGuildWidget*(gid: string): string =
result = endpointGuilds(gid) & "/widget"

proc endpointGuildEmojis*(gid: string; eid = ""): string =
result = endpointGuilds(gid) & "/emojis" & (if eid != "": "/" & eid else: "")
result = endpointGuilds(gid)&"/emojis"&(if eid != "": "/" & eid else: "")

proc endpointGuildRoles*(gid: string; rid = ""): string =
result = endpointGuilds(gid) & "/roles" & (if rid != "": "/" & rid else: "")
result = endpointGuilds(gid) & "/roles" & (if rid!="": "/" & rid else: "")

proc endpointGuildPrune*(gid: string): string =
result = endpointGuilds(gid) & "/prune"
Expand All @@ -335,7 +354,7 @@ proc endpointGuildVanity*(gid: string): string =
result = endpointGuilds(gid) & "/vanity-url"

proc endpointGuildChannels*(gid: string; cid = ""): string =
result = endpointGuilds(gid) & "/channels" & (if cid != "": "/" & cid else: "")
result = endpointGuilds(gid) & "/channels" & (if cid != "":"/"&cid else:"")

proc endpointChannelOverwrites*(cid, oid: string): string =
result = endpointChannels(cid) & "/permissions/" & oid
Expand All @@ -346,6 +365,9 @@ proc endpointWebhooks*(wid: string): string =
proc endpointChannelWebhooks*(cid: string): string =
result = endpointChannels(cid) & "/webhooks"

proc endpointGuildTemplates*(gid, tid = ""): string =
result = endpointGuilds(gid) & "/templates" & (if tid!="": "/"&tid else:"")

proc endpointGuildWebhooks*(gid: string): string =
result = endpointGuilds(gid) & "/webhooks"

Expand All @@ -365,6 +387,9 @@ proc endpointChannelMessages*(cid: string; mid = ""): string =
result = endpointChannels(cid) & "/messages"
if mid != "": result = result & "/" & mid

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

proc endpointChannelInvites*(cid: string): string =
result = endpointChannels(cid) & "/invites"

Expand Down Expand Up @@ -399,7 +424,7 @@ proc endpointOAuth2Application*(): string =
result = "oauth2/applications/@me"

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

proc endpointGuildCommands*(aid, gid: string; cid = ""): string =
result = "applications/" & aid & "/guilds/" & gid & "/commands"
Expand Down
8 changes: 4 additions & 4 deletions dimscord/dispatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,10 @@ proc handleEventDispatch*(s: Shard, event: string, data: JsonNode) {.async.} =
of "GUILD_EMOJIS_UPDATE": await s.guildEmojisUpdate(data)
of "PRESENCE_UPDATE": await s.presenceUpdate(data)
of "MESSAGE_CREATE": await s.messageCreate(data)
of "MESSAGE_REACTION_ADD": await s.messageReactionAdd(data)
of "MESSAGE_REACTION_REMOVE": await s.messageReactionRemove(data)
of "MESSAGE_REACTION_REMOVE_EMOJI": await s.messageReactionRemoveEmoji(data)
of "MESSAGE_REACTION_REMOVE_ALL": await s.messageReactionRemoveAll(data)
of "MESSAGE_REACTION_ADD": await s.messageReactionAdd data
of "MESSAGE_REACTION_REMOVE": await s.messageReactionRemove data
of "MESSAGE_REACTION_REMOVE_EMOJI": await s.messageReactionRemoveEmoji data
of "MESSAGE_REACTION_REMOVE_ALL": await s.messageReactionRemoveAll data
of "MESSAGE_DELETE": await s.messageDelete(data)
of "MESSAGE_UPDATE": await s.messageUpdate(data)
of "MESSAGE_DELETE_BULK": await s.messageDeleteBulk(data)
Expand Down
22 changes: 11 additions & 11 deletions dimscord/gateway.nim
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ proc handleSocketMessage(s: Shard) {.async.} =

var
packet: (Opcode, string)
shouldReconnect = s.client.autoreconnect
autoreconnect = s.client.autoreconnect

while not s.sockClosed:
try:
Expand Down Expand Up @@ -444,14 +444,14 @@ proc handleSocketMessage(s: Shard) {.async.} =
if not s.hbAck:
s.logShard("A zombied connection was detected.")
else:
s.logShard("An error occurred while parsing data: " & packet[1])
shouldReconnect = s.handleDisconnect(packet[1])
s.logShard("An error occurred while parsing data: "&packet[1])
autoreconnect = s.handleDisconnect(packet[1])

await s.disconnect(should_reconnect = shouldReconnect)
await s.disconnect(should_reconnect = autoreconnect)
break

if data["s"].kind != JNull and not s.resuming:
s.sequence = data["s"].getInt()
s.sequence = data["s"].getInt

case data["op"].num:
of opHello:
Expand All @@ -469,13 +469,13 @@ proc handleSocketMessage(s: Shard) {.async.} =
s.hbAck = true
of opHeartbeat:
s.logShard("Discord is requesting for a heartbeat.")
await s.heartbeat(true)
await s.heartbeat true
of opDispatch:
asyncCheck s.handleDispatch(data["t"].str, data["d"])
of opReconnect:
s.logShard("Discord is requesting for a client reconnect.")

await s.disconnect(should_reconnect = shouldReconnect)
await s.disconnect(should_reconnect = autoreconnect)
await s.client.events.on_disconnect(s)
of opInvalidSession:
var interval = 5000
Expand Down Expand Up @@ -506,13 +506,13 @@ proc handleSocketMessage(s: Shard) {.async.} =
raise newException(Exception, "Fatal error occurred.")

if packet[0] == Close:
if not shouldReconnect:
shouldReconnect = s.handleDisconnect(packet[1])
if not autoreconnect:
autoreconnect = s.handleDisconnect(packet[1])

s.stop = true
s.reset()

if shouldReconnect:
if autoreconnect:
await s.reconnect()
await sleepAsync 2000

Expand Down Expand Up @@ -628,7 +628,7 @@ proc startSession*(discord: DiscordClient,
log("WARNING: Your session start limit has reached to 10.")

if info.session_start_limit.remaining == 0:
let time = getTime().toUnix() - info.session_start_limit.reset_after
let time = getTime().toUnix()-info.session_start_limit.reset_after

log("Your session start limit has reached its limit", (
sleep_time: time
Expand Down
10 changes: 7 additions & 3 deletions dimscord/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ proc computePerms*(guild: Guild, member: Member): PermObj =

result = PermObj(allowed: perms)

proc computePerms*(guild: Guild, member: Member, channel: GuildChannel): PermObj =
proc computePerms*(guild: Guild;
member: Member, channel: GuildChannel): PermObj =
## Returns the permissions for the guild member of the channel.
## For permission checking you can do something like this:
##
Expand Down Expand Up @@ -185,9 +186,12 @@ proc computePerms*(guild: Guild, member: Member, channel: GuildChannel): PermObj
deny = deny or cast[int](overwrites[m].deny)

perms = (perms and deny - deny - deny - 1) or allow
result = PermObj(allowed: cast[set[PermissionFlags]](allow), denied: cast[set[PermissionFlags]](deny))
result = PermObj(
allowed: cast[set[PermissionFlags]](allow),
denied: cast[set[PermissionFlags]](deny)
)

proc createBotInvite*(client_id: string, permissions: set[PermissionFlags] = {};
proc createBotInvite*(client_id: string, permissions: set[PermissionFlags]={};
guild_id = ""; disable_guild_select = false): string =
## Creates an invite link for the bot of the form.
##
Expand Down
33 changes: 28 additions & 5 deletions dimscord/objects.nim
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ proc newDiscordClient*(token: string;
r: Role) {.async.} = discard,
guild_role_update: proc (s: Shard, g: Guild,
r: Role, o: Option[Role]) {.async.} = discard,
invite_create: proc (s: Shard, i: InviteCreate) {.async.} = discard,
invite_create: proc(s: Shard, i: InviteCreate) {.async.} = discard,
invite_delete: proc (s: Shard, g: Option[Guild],
c, code: string) {.async.} = discard,
user_update: proc (s: Shard, u: User) {.async.} = discard,
Expand All @@ -115,7 +115,7 @@ proc newDiscordClient*(token: string;
webhooks_update: proc (s: Shard, g: Guild,
c: GuildChannel) {.async.} = discard,
on_disconnect: proc (s: Shard) {.async.} = discard,
interaction_create: proc (s:Shard, i:Interaction){.async.} = discard
interaction_create: proc(s:Shard, i:Interaction){.async.} = discard
))

proc newInviteMetadata*(data: JsonNode): InviteMetadata =
Expand Down Expand Up @@ -152,7 +152,9 @@ proc newRole*(data: JsonNode): Role =
mentionable: data["mentionable"].bval
)
when defined(discordv8):
result.permissions = cast[set[PermissionFlags]](data["permissions"].str.parseInt)
result.permissions = cast[set[PermissionFlags]](
data["permissions"].str.parseInt
)
else:
result.permissions = cast[set[PermissionFlags]](
data["permissions"].getInt
Expand Down Expand Up @@ -482,6 +484,10 @@ proc newMessage*(data: JsonNode): Message =
pinned: data["pinned"].bval,
kind: MessageType data["type"].getInt,
flags: cast[set[MessageFlags]](data["flags"].getInt),
stickers: data{"stickers"}.getElems.map(
proc (x: JsonNode): Sticker =
x.to(Sticker)
),
reactions: initTable[string, Reaction]()
)
data.keyCheckOptStr(result, edited_timestamp,
Expand All @@ -491,6 +497,8 @@ proc newMessage*(data: JsonNode): Message =
result.author = newUser(data["author"])
if "member" in data and data["member"].kind != JNull:
result.member = some newMember(data["member"])
if "referenced_message" in data and data["referenced_message"].kind!=JNull:
result.referenced_message = some data["referenced_message"].newMessage

for r in data{"mention_roles"}.getElems:
result.mention_roles.add(r.str)
Expand Down Expand Up @@ -703,6 +711,21 @@ proc newGuild*(data: JsonNode): Guild =
result.members[uid].presence = presence
result.presences[uid] = presence

proc newGuildTemplate*(data: JsonNode): GuildTemplate =
result = GuildTemplate(
code: data["code"].str,
name: data["name"].str,
usage_count: data["usage_count"].getInt,
creator_id: data["creator_id"].str,
creator: newUser(data["creator"]),
created_at: data["created_at"].str,
updated_at: data["updated_at"].str,
source_guild_id: data["source_guild_id"].str,
serialized_source_guild:data["serialized_source_guild"].to PartialGuild
)
data.keyCheckOptBool(result, is_dirty)
data.keyCheckOptStr(result, description)

proc newApplicationCommandInteractionDataOption(
data: JsonNode
): ApplicationCommandInteractionDataOption =
Expand Down Expand Up @@ -758,8 +781,8 @@ proc newApplicationCommandOption*(data: JsonNode): ApplicationCommandOption =
result = ApplicationCommandOptionChoice(
name: x["name"].str)
if x["value"].kind == JInt:
result.value[1] = some x["value"].getInt # this is a tuple btw
if x["value"].kind == JString:
result.value[1] = some x["value"].getInt # this is
if x["value"].kind == JString: # a tuple btw
result.value[0] = some x["value"].str
),
options: data{"options"}.getElems.map newApplicationCommandOption
Expand Down
16 changes: 15 additions & 1 deletion dimscord/objects/typedefs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ type
activity*: Option[tuple[kind: int, party_id: string]]
application*: Option[Application]
message_reference*: Option[MessageReference]
stickers*: seq[Sticker]
referenced_message*: Option[Message]
User* = ref object
id*, username*, discriminator*: string
bot*, system*: bool
Expand Down Expand Up @@ -152,6 +154,11 @@ type
Application* = object
id*, cover_image*: string
description*, icon*, name*: string
Sticker* = object
id*, pack_id*: string
name*, description*: string
tags*, asset*, format_asset*: Option[string]
format_type*: MessageStickerFormat
RestApi* = ref object
token*: string
endpoints*: Table[string, Ratelimit]
Expand Down Expand Up @@ -242,6 +249,14 @@ type
color*, position*: int
permissions*: set[PermissionFlags]
hoist*, managed*, mentionable*: bool
GuildTemplate* = object
code*, name*, creator_id*: string
description*: Option[string]
usage_count*: int
creator*: User
source_guild_id*, updated_at*, created_at*: string
serialized_source_guild*: PartialGuild
is_dirty*: Option[bool]
ActivityStatus* = object
## This is used for status updates.
name*: string
Expand Down Expand Up @@ -292,7 +307,6 @@ type
name*, description*: string
options*: seq[ApplicationCommandOption]
ApplicationCommandOption* = object
## `choices` reference: Table[choice_name, (Some("..."), None[int])]
kind*: ApplicationCommandOptionType
name*, description*: string
default*, required*: Option[bool]
Expand Down
Loading

0 comments on commit 2ba022d

Please sign in to comment.