How can I update a message only via its messageid and channelid?
I researched and found this solution that looks reasonable but sapphire container client does not like it
52 Replies
Can you explain what you mean by "sapphire container client does not like it"?
ah yes ofc wait a sec
You need
.channels
before the .cachethere's no global client cache, so as ben said, you need to specify which cache you want to use
this is a type script error
whats the best way to deal with this
what happens when the channel isn't in cache?
! ?
hint you get null
hm
so you either need to fetch the channel or you need to null check
so I need to validate it?
you cannot assume the channel will be in cache 100%, but .fetch will check cache and fetch if not present so it's usually safe to use fetch all the time
instead of get?
It’s also kinda odd that you’re fetching a message and assigning it to a const called channel
is it slower? or just uses more resources
yeayea i tried splitting them befor
don't think in terms of slow or fast, or "resource usage"
what instead then?
does it work or not :P
replace your cache call with a fetch to the channel or nullcheck the cache entry, then fetch the message. That's the solution you'll need.
The messages property doesn’t exist on all channels because not all channels have messages (voice, stage, and categories come to mind). You can make sure it’s a text based channel with
channel.isTextBased()
.this makes sense
but how do I make sure its not null?
I tried putting a ! but this helps not
I'd do a little digging into typescript
seems like you have a hard time understanding not typescript but where and when nulls occur and how to properly resolve them
First, don’t cast it to a TextBaseChannel. Doing that will cause issues if it’s not actually text based. Like I mentioned earlier use .isTextBased to check.
Second, checking if it’s null or not is really easy and can actually be combined with the text based check. You’d do something like this
im one of those guys and I do explicit
if (channel == null)
though that works perfectly and you can adapt the thrown error to be whatever you likeiirc all channels are already cached
I have to do this any Time I want to fetch a message? this sound so ineficient
I mean if I we’re doing it myself I’d probably use optional chaining but that’s less explicit about what’s actually happening in the code
it's inefficient you say but you catch runtime errors this way
you can obviously ignore this all if you want and assume it's never null
with a constant too yeah maybe that's the way
It’s much safer than trying to fetch messages that don’t exist from channels that don’t exist
typescript cannot guarantee that variable can never be null, so it forces you to either lie and say it will never be or it wants you to type check
this is the foundation of typescript and the values it brings you
if I throw an error, the program will exit correct? How exactly is this supposed to work with a discord bot?
no
errors in javascript (runtime you're in javascript land) do not terminate the process
plus that'd be a bad thing
thats good to know
if you're using sapphire then it goes to the respective command error listener
and depending on what's loaded it may or may not print to stderr
You also don’t have to throw an error there. You could try and handle invalid channels some other way. I just put it there to demonstrate that was where the channel would be invalid
code is fluent- morph it to suit your needs. Ben's examples was more focused on what to do and not exactly how you need to do it.
oh jesus there are command error listeners? like for slashcommands? why have I not seen that this is super dope
sapphire has a lot
https://www.sapphirejs.dev/docs/Guide/getting-started/getting-started-with-sapphire
lots of guides to help you get started with understanding sapphire as a framework
otherwise I'd suggest learning more about typescript specifically or using javascript
Sapphire implements the stuff that I don’t want to deal with. It’s pretty nice
do you even want to deal with anything ben
😁
yea yea read through but its a lot to remember and Im sure I lost some knowledge already
Fair point lol.
docs are there for you to refer to, shouldn't be ashamed to lose knowledge since no one can remember eveerything
good thing to just refer to docs when you are looking for something specific
otherwise djs docs is second in command
I almost never code with less than three tabs dedicated to docs
Both in my personal and professional projects
you have docs on your professional projects?
Yeah. We use industry standard tools at my job and they have docs. If they didn’t I’d probably go mad.
wild concept
We also write our own documentation on internal tools