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
242y ago
Can you explain what you mean by "sapphire container client does not like it"?
chillihero
chilliheroOP2y ago
ah yes ofc wait a sec
chillihero
chilliheroOP2y ago
No description
Ben
Ben2y ago
You need .channels before the .cache
chillihero
chilliheroOP2y ago
No description
24
242y 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
chilliheroOP2y ago
whats the best way to deal with this
24
242y ago
what happens when the channel isn't in cache?
chillihero
chilliheroOP2y ago
! ?
24
242y ago
hint you get null
chillihero
chilliheroOP2y ago
hm
24
242y ago
so you either need to fetch the channel or you need to null check
chillihero
chilliheroOP2y ago
so I need to validate it?
24
242y 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
chilliheroOP2y ago
instead of get?
Ben
Ben2y ago
It’s also kinda odd that you’re fetching a message and assigning it to a const called channel
chillihero
chilliheroOP2y ago
is it slower? or just uses more resources yeayea i tried splitting them befor
24
242y ago
don't think in terms of slow or fast, or "resource usage"
chillihero
chilliheroOP2y ago
what instead then?
24
242y 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
chilliheroOP2y ago
No description
No description
Ben
Ben2y 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
chilliheroOP2y ago
this makes sense but how do I make sure its not null?
chillihero
chilliheroOP2y ago
No description
chillihero
chilliheroOP2y ago
I tried putting a ! but this helps not
24
242y 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
Ben2y 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
242y 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
Krish2y ago
iirc all channels are already cached
chillihero
chilliheroOP2y ago
I have to do this any Time I want to fetch a message? this sound so ineficient
Ben
Ben2y 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
242y 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
Ben2y ago
It’s much safer than trying to fetch messages that don’t exist from channels that don’t exist
24
242y 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
chilliheroOP2y ago
if I throw an error, the program will exit correct? How exactly is this supposed to work with a discord bot?
24
242y ago
no errors in javascript (runtime you're in javascript land) do not terminate the process plus that'd be a bad thing
chillihero
chilliheroOP2y ago
thats good to know
24
242y 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
Ben2y 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
242y 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
chilliheroOP2y ago
oh jesus there are command error listeners? like for slashcommands? why have I not seen that this is super dope
24
242y 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
Ben2y ago
Sapphire implements the stuff that I don’t want to deal with. It’s pretty nice
24
242y ago
do you even want to deal with anything ben 😁
chillihero
chilliheroOP2y ago
yea yea read through but its a lot to remember and Im sure I lost some knowledge already
Ben
Ben2y ago
Fair point lol.
24
242y 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
Ben2y ago
I almost never code with less than three tabs dedicated to docs Both in my personal and professional projects
24
242y ago
you have docs on your professional projects?
Ben
Ben2y ago
Yeah. We use industry standard tools at my job and they have docs. If they didn’t I’d probably go mad.
24
242y ago
wild concept
Ben
Ben2y ago
We also write our own documentation on internal tools
Want results from more Discord servers?
Add your server