Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helpers fix #126

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft

Helpers fix #126

wants to merge 15 commits into from

Conversation

nayr7
Copy link
Contributor

@nayr7 nayr7 commented Aug 9, 2024

implementation fixes

Some helpers were preventing code to compile because of bad implementation causing compiler errors. Those were fixed in this PR.

template renaming

Some helpers have been renamed because of overloading too much which was especially the case of the edit identifier which confused the compiler sometimes.

getClient and mainClient

In some Nim versions, users may encounter "client not found" or "client not registered" errors due to compiler bugs that have now been resolved. As a result, this feature works on recent versions of the compiler but not on older ones.

mainClient is now a no-op. We can reserve it for future versions of the library in case we need it again (reverting to the old behavior or implementing another solution mentioned down below). We can also deprecate it if we decide that it's no longer necessary.

getClient now works by finding the 's' symbol which is always available within event handlers or command handlers like in dimscmd. It also checks if the 's' symbol is a Shard in case the user (for some obscure reason) decides to shadow it. This should work fine in older versions since it relies on less complex features of the language.

However, this means we can no longer use the helpers outside of event or command handlers, for example after this patch it won't be possible to use helpers with a client that has rest_mode = true because they cannot use event handlers. I decided the fix was worth it because it makes the most common use-cases more reliable.

One idea I had to mitigate this problem is to modify mainClient again to generate a template that aliases the user's client, allowing us to use the real symbol by calling getClient without relying on the old method again:

# objects/macros
import  macros, strutils

macro mainClient*(variable: untyped): untyped =
  let templateName = ident("dimscordPrivateClient")
  result = quote do: # 
    template `templateName`*(): untyped {.dirty.} =
      `variable`

template getClient*: DiscordClient = 
  ## Tries to access DiscordClient by using a Shard or an alias. Internal use only.
  when (declared(dimscordPrivateClient)):
    var dc {.cursor.} = dimscordPrivateClient
  elif (declared(s)) and (typeof(s) is Shard):
    var dc {.cursor.} = s.client
    when defined(dimscordDebug): 
      if dc.isNil: raise (ref AccessViolationDefect)(msg: "Client is nil: Please check if you have a properly initialized client.") 
    dc
  else:
    {.error: "Error: Cannot find any client in scope. You must use `mainClient` or have a 's' variable of type `Shard` in the current scope in order to work".}

Example usage:

let discord {.mainClient.} = newDiscordClient("token", rest_mode = true)  
var channel: GuildChannel = waitFor discord.api.getChannel("channel_id")

# when a helper is used:
var channel: GuildChannel = waitFor discord.api.getChannel("channel_id")
var msg = waitFor channel.send("hello!")

# it should become..
var channel: GuildChannel = waitFor discord.api.getChannel("channel_id")
var msg = waitFor discord.api.sendMessage(
        ch.id, "hello", false,
        none(int), @[], @[],
        @[], none AllowedMention, none MessageReference,
        newSeq[MessageComponent](), newSeq[string]()
    )

Future work

Because nim templates are lazy we have to use them so that they are instantiated, the compiler cannot tell us if the template is good otherwise. So a CI/test-suite will follow-up this PR in order to test if they work correctly.

@nayr7
Copy link
Contributor Author

nayr7 commented Aug 9, 2024

Oops, didnt update the examples. I'll patch them

@krisppurg krisppurg added enhancement New feature or request todo TODO list labels Aug 9, 2024
@krisppurg krisppurg self-assigned this Aug 9, 2024
@krisppurg krisppurg marked this pull request as draft August 9, 2024 23:23
@nayr7 nayr7 mentioned this pull request Aug 9, 2024
91 tasks
@krisppurg krisppurg linked an issue Aug 10, 2024 that may be closed by this pull request
91 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request todo TODO list
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fixing and improving helpers
2 participants