How can I update a message only via its messageid and channelid?

CChillihero1/30/2023
I researched and found this solution that looks reasonable but sapphire container client does not like it
const message = await container.client.cache.get('xxxx').messages.fetch('yyyy');
2241/30/2023
Can you explain what you mean by "sapphire container client does not like it"?
CChillihero1/30/2023
ah yes ofc wait a sec
CChillihero1/30/2023
Image
BBen8551/30/2023
You need .channels before the .cache
CChillihero1/30/2023
Image
2241/30/2023
there's no global client cache, so as ben said, you need to specify which cache you want to use
2241/30/2023
this is a type script error
CChillihero1/30/2023
whats the best way to deal with this
2241/30/2023
what happens when the channel isn't in cache?
CChillihero1/30/2023
! ?
2241/30/2023
hint you get null
CChillihero1/30/2023
hm
2241/30/2023
so you either need to fetch the channel or you need to null check
CChillihero1/30/2023
so I need to validate it?
2241/30/2023
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
CChillihero1/30/2023
instead of get?
BBen8551/30/2023
It’s also kinda odd that you’re fetching a message and assigning it to a const called channel
CChillihero1/30/2023
is it slower? or just uses more resources
CChillihero1/30/2023
yeayea i tried splitting them befor
2241/30/2023
don't think in terms of slow or fast, or "resource usage"
CChillihero1/30/2023
what instead then?
2241/30/2023
does it work or not :P
2241/30/2023
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.
CChillihero1/30/2023
ImageImage
BBen8551/30/2023
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().
CChillihero1/30/2023
this makes sense
CChillihero1/30/2023
but how do I make sure its not null?
CChillihero1/30/2023
Image
CChillihero1/30/2023
I tried putting a ! but this helps not
2241/30/2023
I'd do a little digging into typescript
2241/30/2023
seems like you have a hard time understanding not typescript but where and when nulls occur and how to properly resolve them
BBen8551/30/2023
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
if(!channel || !channel.isTextBased()){
    throw new Error('invalid channel id')
}

// channel is not null and is text based here
2241/30/2023
im one of those guys and I do explicit if (channel == null)
2241/30/2023
though that works perfectly and you can adapt the thrown error to be whatever you like
KKrish1/30/2023
iirc all channels are already cached
CChillihero1/30/2023
I have to do this any Time I want to fetch a message? this sound so ineficient
BBen8551/30/2023
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
2241/30/2023
it's inefficient you say but you catch runtime errors this way
2241/30/2023
you can obviously ignore this all if you want and assume it's never null
2241/30/2023
with a constant too yeah maybe that's the way
BBen8551/30/2023
It’s much safer than trying to fetch messages that don’t exist from channels that don’t exist
2241/30/2023
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
CChillihero1/30/2023
if I throw an error, the program will exit correct? How exactly is this supposed to work with a discord bot?
2241/30/2023
no
2241/30/2023
errors in javascript (runtime you're in javascript land) do not terminate the process
2241/30/2023
plus that'd be a bad thing
CChillihero1/30/2023
thats good to know
2241/30/2023
if you're using sapphire then it goes to the respective command error listener
2241/30/2023
and depending on what's loaded it may or may not print to stderr
BBen8551/30/2023
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
2241/30/2023
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.
CChillihero1/30/2023
oh jesus there are command error listeners? like for slashcommands? why have I not seen that this is super dope
2241/30/2023
sapphire has a lot
2241/30/2023
lots of guides to help you get started with understanding sapphire as a framework
2241/30/2023
otherwise I'd suggest learning more about typescript specifically or using javascript
BBen8551/30/2023
Sapphire implements the stuff that I don’t want to deal with. It’s pretty nice
2241/30/2023
do you even want to deal with anything ben
😁
CChillihero1/30/2023
yea yea read through but its a lot to remember and Im sure I lost some knowledge already
BBen8551/30/2023
Fair point lol.
2241/30/2023
docs are there for you to refer to, shouldn't be ashamed to lose knowledge since no one can remember eveerything
2241/30/2023
good thing to just refer to docs when you are looking for something specific
otherwise djs docs is second in command
BBen8551/30/2023
I almost never code with less than three tabs dedicated to docs
BBen8551/30/2023
Both in my personal and professional projects
2241/30/2023
you have docs on your professional projects?
BBen8551/30/2023
Yeah. We use industry standard tools at my job and they have docs. If they didn’t I’d probably go mad.
2241/30/2023
wild concept
BBen8551/30/2023
We also write our own documentation on internal tools