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
const message = await container.client.cache.get('xxxx').messages.fetch('yyyy');
const message = await container.client.cache.get('xxxx').messages.fetch('yyyy');
52 Replies
24
2417mo ago
Can you explain what you mean by "sapphire container client does not like it"?
chillihero
chillihero17mo ago
ah yes ofc wait a sec
chillihero
chillihero17mo ago
No description
Ben
Ben17mo ago
You need .channels before the .cache
chillihero
chillihero17mo ago
No description
24
2417mo ago
there'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
chillihero
chillihero17mo ago
whats the best way to deal with this
24
2417mo ago
what happens when the channel isn't in cache?
chillihero
chillihero17mo ago
! ?
24
2417mo ago
hint you get null
chillihero
chillihero17mo ago
hm
24
2417mo ago
so you either need to fetch the channel or you need to null check
chillihero
chillihero17mo ago
so I need to validate it?
24
2417mo ago
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
chillihero
chillihero17mo ago
instead of get?
Ben
Ben17mo ago
It’s also kinda odd that you’re fetching a message and assigning it to a const called channel
chillihero
chillihero17mo ago
is it slower? or just uses more resources yeayea i tried splitting them befor
24
2417mo ago
don't think in terms of slow or fast, or "resource usage"
chillihero
chillihero17mo ago
what instead then?
24
2417mo ago
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.
chillihero
chillihero17mo ago
No description
No description
Ben
Ben17mo ago
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().
chillihero
chillihero17mo ago
this makes sense but how do I make sure its not null?
chillihero
chillihero17mo ago
No description
chillihero
chillihero17mo ago
I tried putting a ! but this helps not
24
2417mo ago
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
Ben
Ben17mo ago
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
if(!channel || !channel.isTextBased()){
throw new Error('invalid channel id')
}

// channel is not null and is text based here
24
2417mo ago
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 like
Krish
Krish17mo ago
iirc all channels are already cached
chillihero
chillihero17mo ago
I have to do this any Time I want to fetch a message? this sound so ineficient
Ben
Ben17mo ago
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
24
2417mo ago
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
Ben
Ben17mo ago
It’s much safer than trying to fetch messages that don’t exist from channels that don’t exist
24
2417mo ago
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
chillihero
chillihero17mo ago
if I throw an error, the program will exit correct? How exactly is this supposed to work with a discord bot?
24
2417mo ago
no errors in javascript (runtime you're in javascript land) do not terminate the process plus that'd be a bad thing
chillihero
chillihero17mo ago
thats good to know
24
2417mo ago
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
Ben
Ben17mo ago
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
24
2417mo ago
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.
chillihero
chillihero17mo ago
oh jesus there are command error listeners? like for slashcommands? why have I not seen that this is super dope
24
2417mo ago
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
Ben
Ben17mo ago
Sapphire implements the stuff that I don’t want to deal with. It’s pretty nice
24
2417mo ago
do you even want to deal with anything ben 😁
chillihero
chillihero17mo ago
yea yea read through but its a lot to remember and Im sure I lost some knowledge already
Ben
Ben17mo ago
Fair point lol.
24
2417mo ago
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
Ben
Ben17mo ago
I almost never code with less than three tabs dedicated to docs Both in my personal and professional projects
24
2417mo ago
you have docs on your professional projects?
Ben
Ben17mo ago
Yeah. We use industry standard tools at my job and they have docs. If they didn’t I’d probably go mad.
24
2417mo ago
wild concept
Ben
Ben17mo ago
We also write our own documentation on internal tools