Skip to content

Commit 43660bc

Browse files
committed
File upload support and some patches being made
1 parent 56642d4 commit 43660bc

28 files changed

+1833
-1726
lines changed

dimscord/constants.nim

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,11 @@ type
414414
acotNumber = 10 ## A double
415415
acotAttachment = 11
416416
ApplicationCommandType* = enum
417-
atNothing = 0 ## Should never appear
418-
atSlash = 1 ## CHAT_INPUT
419-
atUser ## USER
420-
atMessage ## MESSAGE
417+
atNothing = 0 ## Should never appear
418+
atSlash = 1 ## CHAT_INPUT
419+
atUser ## USER
420+
atMessage ## MESSAGE
421+
atPrimaryEntryPoint ## PRIMARY_ENTRY_POINT
421422
ApplicationCommandPermissionType* = enum
422423
acptRole = 1
423424
acptUser = 2
@@ -502,6 +503,7 @@ type
502503
mctSeparator = 14
503504
mctContainer = 17
504505
mctLabel = 18
506+
mctFileUpload = 19
505507
StickerType* = enum
506508
stUnknown
507509
stStandard = 1

dimscord/gateway.nim

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ proc startSession*(discord: DiscordClient,
724724
max_message_size = 5_000_000;
725725
gateway_version = 10;
726726
max_shards = none int; shard_id = 0;
727-
cache_users, cache_guilds, guild_subscriptions = true;
727+
cache_users, cache_guilds = true;
728728
cache_guild_channels, cache_dm_channels = true) {.async.} =
729729
## Connects the client to Discord via gateway.
730730
##
@@ -751,14 +751,6 @@ proc startSession*(discord: DiscordClient,
751751

752752
discord.largeThreshold = large_threshold
753753

754-
if guild_subscriptions:
755-
log("Warning: guild_subscriptions is deprecated.")
756-
discord.intents = discord.intents + {
757-
giGuildMessageTyping,
758-
giDirectMessageTyping,
759-
giGuildPresences
760-
}
761-
discord.guildSubscriptions = true
762754

763755
discord.max_shards = max_shards.get(-1)
764756
discord.gatewayVersion = gateway_version

dimscord/helpers.nim

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,30 @@ proc `$`*(u: User): string =
150150
proc `@`*(u: User, nick = false): string =
151151
## Mentions a user.
152152
let n = if nick: "!" else: ""
153-
result = &"<@{n}{u.id}>"
153+
&"<@{n}{u.id}>"
154154

155155
proc `@`*(r: Role): string =
156156
## Mentions a role.
157-
result = &"<@&{r.id}>"
157+
&"<@&{r.id}>"
158158

159159
proc `@`*(g: GuildChannel): string =
160160
## Mentions a guild channel.
161-
result = &"<#{g.id}>"
161+
&"<#{g.id}>"
162162

163163
proc `@`*(a: ApplicationCommand): string =
164164
## Mentions a slash command.
165-
result = &"</{a.name}:{a.id}>"
165+
&"</{a.name}:{a.id}>"
166+
167+
proc `@`*(e: Emoji): string =
168+
## Mentions emoji
169+
&"<{$e}>"
170+
171+
proc `>`*(e: string): Emoji =
172+
## Converts string to Emoji
173+
## "something:123" -> `Emoji(id: "123", name: "something")`
174+
var elems = e.split(":")
175+
Emoji(name: some elems[0],
176+
id: if elems.len > 1: some elems[1] else: none string)
166177

167178
proc `$`*(g: GuildChannel): string =
168179
## Stringifies a guild channel.

dimscord/helpers/message.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ template delete*(msgs: seq[Message]; reason = ""): Future[void] =
7575
getClient.api.bulkDeleteMessages(msgs[0].channel_id, msgs)
7676

7777
template getMessages*(ch: SomeChannel;
78-
around, before, after = "";
78+
around = "", before = "", after = "";
7979
limit: range[1..100] = 50): Future[seq[Message]] =
8080
## Gets channel messages.
8181
getClient.api.getChannelMessages(

dimscord/helpers/user.nim

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ template getCommand*(app: Application;
5454
app.id, guild_id, command_id
5555
)
5656

57-
template registerCommand*(app: Application; name: string;
58-
description, guild_id = "";
59-
name_localizations,description_localizations=none Table[string,string];
57+
template registerCommand*(
58+
app: Application | User | Shard | DiscordClient;
59+
name: string; description = "", guild_id = "";
60+
name_localizations =none Table[string,string];
61+
description_localizations=none Table[string,string];
6062
kind = atSlash; nsfw = false;
6163
default_member_permissions = none set[PermissionFlags];
6264
options: seq[ApplicationCommandOption] = @[];
@@ -72,6 +74,16 @@ template registerCommand*(app: Application; name: string;
7274
## **NOTE:** Creating a command with the same name
7375
## as an existing command for your application will
7476
## overwrite the old command.
77+
var id = block:
78+
when app is Application or app is User:
79+
app.id
80+
when app is Shard:
81+
assert app.user != nil
82+
app.user.id
83+
when app is DiscordClient:
84+
assert app.shards[0].user != nil
85+
app.shards[0].user.id
86+
7587
getClient.api.registerApplicationCommand(
7688
app.id, name,
7789
description, guild_id,

dimscord/objects.nim

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,14 @@ proc postHook(p: var Presence) =
415415
if p.client_status.mobile == "":
416416
p.client_status.mobile = "offline"
417417

418+
proc postHook(m: var Message) =
419+
if m.member.isSome:
420+
if m.member.get.user == nil and m.author != nil:
421+
m.member.get.user = m.author
422+
m.member.get.presence.user = m.author
423+
if m.member.get.presence.guild_id == "":
424+
m.member.get.presence.guild_id = m.guild_id.get
425+
418426
proc parseHook*(s: string, i: var int, v: var OverwriteType) =
419427
var data: JsonNode
420428
parseHook(s, i, data)
@@ -437,7 +445,9 @@ proc newHook(m: var Member) =
437445
)
438446

439447
proc postHook(m: var Member) =
440-
m.presence.user = m.user
448+
if m.user != nil: m.presence.user = m.user
449+
if m.presence.guild_id == "" and m.guild_id != "":
450+
m.presence.guild_id = m.guild_id
441451

442452
proc parseHook(s: string, i: var int, v: var Table[string, Overwrite]) =
443453
var overwrites: seq[Overwrite]
@@ -854,39 +864,31 @@ proc parseHook(s: string, n: var int, a: var ApplicationCommandInteractionData)
854864
kind: ($data["type"]).fromJson(ApplicationCommandType)
855865
)
856866

857-
a.resolved=ApplicationCommandResolution(kind: a.kind)
858-
if "resolved" in data:
859-
for key, values in data["resolved"].getFields.pairs:
860-
case key:
861-
of "users":
862-
for k, v in values.pairs:
863-
a.resolved.users[k] = v.newUser
864-
of "attachments":
865-
for k, v in values.pairs:
866-
a.resolved.attachments[k] = v.newAttachment
867-
else: discard
868-
869-
if a.kind == atUser:
870-
case key:
871-
of "members":
872-
for k, v in values.pairs:
873-
a.resolved.members[k] = v.newMember
874-
of "roles":
875-
for k, v in values.pairs:
876-
a.resolved.roles[k] = v.newRole
877-
else: discard
878-
879-
if a.kind == atMessage:
880-
case key:
881-
of "channels":
882-
for k, v in values.pairs:
883-
a.resolved.channels[k] = ($v).fromJson(
884-
ResolvedChannel
885-
)
886-
of "messages":
887-
for k, v in values.pairs:
888-
a.resolved.messages[k] = v.newMessage
889-
else: discard
867+
a.resolved=ResolvedData()
868+
if "resolved" in data:
869+
for key, values in data["resolved"].getFields.pairs:
870+
case key:
871+
of "users":
872+
for k, v in values.pairs:
873+
a.resolved.users[k] = v.newUser
874+
of "attachments":
875+
for k, v in values.pairs:
876+
a.resolved.attachments[k] = v.newAttachment
877+
of "members":
878+
for k, v in values.pairs:
879+
a.resolved.members[k] = v.newMember
880+
of "roles":
881+
for k, v in values.pairs:
882+
a.resolved.roles[k] = v.newRole
883+
of "channels":
884+
for k, v in values.pairs:
885+
a.resolved.channels[k] = ($v).fromJson(
886+
ResolvedChannel
887+
)
888+
of "messages":
889+
for k, v in values.pairs:
890+
a.resolved.messages[k] = v.newMessage
891+
else: discard
890892

891893
for k, val in data.pairs:
892894
case val.kind:
@@ -948,7 +950,8 @@ proc `%%*`*(a: ApplicationCommand): JsonNode =
948950
# This ternary is needed so that the enums can stay similar to
949951
# the discord api
950952

951-
# <TODO> PLEASE CLEAN UP THE CODE
953+
# <TODO> PLEASE CLEAN UP THE CODE -> im postponing this cause ibr code cleanup is for later
954+
# if anyone cares enough to read this :<
952955

953956
let commandKind = if a.kind == atNothing: atSlash else: a.kind
954957
result = %*{
@@ -1074,29 +1077,28 @@ proc `%%*`*(comp: MessageComponent): JsonNode =
10741077
# I thought I'd just keep it as it is and make a `%` that would redirect the proc.
10751078
result = %*{"type": comp.kind.ord}
10761079

1077-
result.loadOpts(comp, spoiler, placeholder, disabled, id, label, description)
1080+
result.loadOpts(comp, spoiler, placeholder,
1081+
disabled, id, label, description,
1082+
custom_id, min_values, max_values, required)
1083+
10781084
case comp.kind:
1079-
of mctNone: discard
1085+
of mctNone, mctFileUpload: discard
10801086
of mctActionRow, mctContainer:
10811087
result["components"] = %comp.components.mapIt(%%*it)
10821088
result.loadOpts(comp, accent_color)
10831089
of mctButton:
10841090
result &= %*{"style": comp.style.ord}
10851091

1086-
result.loadOpts(comp, custom_id, url, sku_id)
1092+
result.loadOpts(comp, url, sku_id)
10871093
if comp.emoji.isSome:
10881094
result["emoji"] = comp.emoji.get.toPartial
10891095
of mctSelectMenu, mctUserSelect, mctRoleSelect, mctMentionableSelect, mctChannelSelect:
1090-
result &= %*{"custom_id": comp.custom_id.get,
1091-
"options": comp.options,
1096+
result &= %*{"options": comp.options,
10921097
"placeholder": comp.placeholder,
1093-
"min_values": comp.minValues,
1094-
"max_values": comp.maxValues,
10951098
"channel_types": comp.channel_types.mapIt(it.ord),
10961099
"default_values": comp.default_values.mapIt(%it)}
10971100
of mctTextInput:
1098-
result &= %*{"custom_id": comp.custom_id.get,
1099-
"placeholder": comp.placeholder,
1101+
result &= %*{"placeholder": comp.placeholder,
11001102
"style": ord comp.input_style.get}
11011103

11021104
result.loadOpts(comp, value, required, min_length, max_length)

dimscord/objects/typedefs.nim

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -746,21 +746,21 @@ type
746746
version*: int
747747
entitlements*: seq[Entitlement]
748748
authorizing_integration_owners*: Table[string, JsonNode]
749-
context*: Option[ApplicationIntegrationType]
749+
context*: Option[InteractionContextType]
750750
attachment_size_limit*: int
751751
ApplicationCommandInteractionData* = ref object
752752
## - `options` A Table consisting of the option name and the respective option object.
753+
resolved*: ResolvedData
753754
case interaction_type*: InteractionDataType
754755
of idtApplicationCommand:
755756
id*, name*: string
756757
guild_id*: Option[string]
757-
resolved*: ApplicationCommandResolution
758758
case kind*: ApplicationCommandType
759759
of atSlash:
760760
options*: Table[string, ApplicationCommandInteractionDataOption]
761761
of atUser, atMessage:
762762
target_id*: string
763-
of atNothing: discard
763+
of atNothing, atPrimaryEntryPoint: discard
764764
of idtMessageComponent, idtModalSubmit:
765765
case component_type*: MessageComponentType:
766766
of mctSelectMenu, mctUserSelect, mctRoleSelect, mctMentionableSelect, mctChannelSelect:
@@ -783,17 +783,6 @@ type
783783
roles*: Table[string, Role]
784784
channels*: Table[string, ResolvedChannel]
785785
messages*: Table[string, Message]
786-
ApplicationCommandResolution* = object
787-
users*: Table[string, User]
788-
attachments*: Table[string, Attachment]
789-
case kind*: ApplicationCommandType
790-
of atUser:
791-
members*: Table[string, Member]
792-
roles*: Table[string, Role]
793-
of atMessage:
794-
channels*: Table[string, ResolvedChannel]
795-
messages*: Table[string, Message]
796-
else: discard
797786
ApplicationCommandInteractionDataOption* = object
798787
name*: string
799788
case kind*: ApplicationCommandOptionType
@@ -935,6 +924,8 @@ type
935924
label*: Option[string]
936925
description*: Option[string]
937926
components*: seq[MessageComponent]
927+
min_values*, max_values*: Option[int]
928+
required*: Option[bool]
938929
case kind*: MessageComponentType
939930
of mctNone: discard
940931
of mctActionRow, mctContainer:
@@ -947,11 +938,9 @@ type
947938
default_values*: seq[tuple[id, kind: string]]
948939
options*: seq[SelectMenuOption]
949940
channel_types*: seq[ChannelType]
950-
min_values*, max_values*: Option[int]
951941
of mctTextInput:
952942
input_style*: Option[TextInputStyle]
953943
value*: Option[string]
954-
required*: Option[bool]
955944
min_length*, max_length*: Option[int]
956945
of mctThumbnail:
957946
media*: UnfurledMediaItem
@@ -971,6 +960,8 @@ type
971960
content*: string
972961
of mctLabel:
973962
component*: MessageComponent
963+
of mctFileUpload:
964+
discard
974965
GuildPreview* = object
975966
id*, name*: string
976967
system_channel_flags*: set[SystemChannelFlags]
@@ -1227,7 +1218,6 @@ proc dm*(c: CacheTable, obj: ref object | object | string): DMChannel =
12271218
compiles(obj.channel_id),
12281219
"channel_id field does not exist in " & $typeof(obj)
12291220
)
1230-
12311221
when obj.channel_id is Option[string]:
12321222
assert obj.channel_id.isSome, $typeof(obj) & ".channel_id is none!"
12331223
c.dmchannels[obj.channel_id.get]
@@ -1258,6 +1248,26 @@ proc `$`*(a: Attachment): string =
12581248
proc `$`*(a: ref object): string =
12591249
$a[]
12601250

1251+
template guild*(o: (object | ref object | tuple)): Guild =
1252+
## Get guild from object via cache.
1253+
when declared(s) and s is Shard:
1254+
s.cache.guild(o)
1255+
else:
1256+
{.error: "Cannot find Shard, the arg value must be referred to as 's' which represents Shard".}
1257+
1258+
template gchannel*(o: (object | ref object | tuple)): GuildChannel =
1259+
## Get guild channel from object via cache
1260+
when declared(s) and s is Shard:
1261+
s.cache.gchannel(o)
1262+
else:
1263+
{.error: "Cannot find Shard, the arg value must be referred to as 's' which represents Shard".}
1264+
1265+
template dm*(o: (object | ref object | tuple)): DmChannel =
1266+
when declared(s) and s is Shard:
1267+
s.cache.dm(o)
1268+
else:
1269+
{.error: "Cannot find Shard, the arg value must be referred to as 's' which represents Shard".}
1270+
12611271
proc getCurrentDiscordHttpError*(): DiscordHttpError =
12621272
## Use this proc instead of getCurrentException() for advanced details.
12631273
let err = getCurrentException()

0 commit comments

Comments
 (0)