Je Suis Un Ami
Explore posts from serversHow to Execute Server Action on Each Route Visit?
I have a home component (code in comments)
In summary, what this component does is it calls a server action to check if the user is authenticated. If the user is authenticated, it redirects the user to the dashboard route. Otherwise, it redirects the user to the login route.
Now, here's my question. What I want to do is perform this verification every time the home route is visited (even from a client-side redirect). From my logs however, it doesn't look like the server action is being called in subsequent visits to this route (i.e. after the account setup process is complete. The reason I know this is because "Executing checks to determine redirect" is being printed on my console. But, none of the other logs that would be printed if the action completed are. In other words
authenticatedUser.result
is always falsy. How would I go about doing what I am trying to do? Is there a different approach I should be taking to accomplish what I am trying to do?
Thanks. Like I said, component code is in the first comment of this thread.16 replies
Server Actions and Cookies sending error: Cannot set headers after they are sent to the clien
Maybe I am misunderstanding something here. I am following the docs for Actions. Basically, what this component is supposed to do is check for the pressence of a session cookie. And, if the user is authenticated, redirects the client to the dashboard page. And if not, it redirects the client to the login page.
I included the action definition in the comments.
29 replies
SIASapphire - Imagine a framework
•Created by Je Suis Un Ami on 3/11/2024 in #sapphire-support
Formatted Logs
Is there a way to extend the existing logger within Sapphire?
I saw that in the logger argument of the client constructor I can pass an instance of
ILogger
. So, I thought I could just extend the Logger
class from @sapphire/framework
and pass that to the instance argument.
Doesn’t seem to work. It’s not formatting my logs. Seems to stick to the default logger. Are there any other options?
I am kind of looking for a way to just work with the existing logger instead of creating an entirely new plugin. The only real change I am making is formatting log messages like so: [context]: message
, where context shows where the log is coming from (I.e. MyCommand.chatInputRun`. And message is the normal log message.
I could just pass a formatting function to the current logger. But this is a very error-prone solution.
Thanks in advance.16 replies
SIASapphire - Imagine a framework
•Created by Je Suis Un Ami on 3/5/2024 in #sapphire-support
Failure Handling With Listeners.
This is more a design/architecture question. Just need more input.
So, a few of the commands I am writing for my bot are getting… complicated. By that I mean, database state is being updated, which leads to server categories being updated, and channels being updated, etc…So, instead of putting all the logic in my command class, I decided to go the event emitter route and
this.container.client.emit()
route and move the server channel/category/role updating to a listener that just responds to the custom event I emitted.
Something that crosses my mind though is how to handle if one of the listeners fail for whatever reason.
Does Sapphire provide an option to automatically retry listeners if they fail? Or am I gonna need to manually handle that somehow?15 replies
SIASapphire - Imagine a framework
•Created by Je Suis Un Ami on 3/3/2024 in #sapphire-support
Running Async Code in Precinditions
I didn’t see this in the docs. But, is it possible to run async code in preconditions?
TLDR: I need to load a “guild configuration” from my database, which contains the ID of a role that the caller must (in the server) to be allowed to execute some command.
Long question is, users (server owners) of my bot must run a “setup” command, which will create a “GuildProfile” for their server before they are allowed to use any of the other commands. In my precondition, what I want to do is get the “GuildProfile” for the server and see if the caller of the command has the role specified in that profile. If so,
return this.ok()
. Otherwise, return this.error()
.
However, it doesn’t look like I can run async code in preconditions. So, is there a way to do so? Or am I gonna need to perform this check in the *Run()
method of my command?4 replies
SIASapphire - Imagine a framework
•Created by Je Suis Un Ami on 3/2/2024 in #sapphire-support
Getting Data from .env File
I created a new Sapphire app using the CLI. I am using swc. And, I want to ask how do access env variables in my bot? The project contains a
src/.env
file . So, naturally, I thought to put my discord token there and access it in my src/index.ts
file as follows.
However, running this throws an error : Error [TokenInvalid]: An invalid token was provided.
. However, it runs if I pass the discord token as a string literal to the client.login()
method. So, this tells me the src/.env
file isn't getting included in the compiled source code. And, upon reviewing the outputted 'dist
folder, it appears this is the case.
How would I fix this? Or, is there a different way I am supposed to access environment variables?128 replies
Context Not Being Defined
I am running into a problem where the
AuthContext
isn't being defined in m app.
Here's my App component:
For convenience, here is my <mAIN />
component:
Essentially, the error being thrown is Uncaught TypeError: Cannot read properties of null
, referring to the auth check being done by the authenticated()
signal, defined in the AuthContext
context.
Here is the slimmed down version of my AuthContext
:
In my logs, I know that AuthContext
is not being defined because the print statement defined in it is never shown.
How would I fix this?2 replies
Saving Results of routeData() into a Context
What is the proper way to save the results of routeData() into a context? So, here's my setup.
Here's my root.
And, in my route component, I have it like this:
According to my logs, data is present in both the routeData() and when the component initially renders. However, it doesn't seem like it is ever put into the context (as in, it's saying it is undefined on my UI). How would I go about this?
2 replies
Creating an Observable from a store.
Is there a way I can create an observable from a store?
Essentially, what I want to do this:
Or, something like that at least.
This fails because the
observable()
function accepts an Accessor, which pretty much is only signals. Is there a way I can make it work with stores as well?4 replies