timotejroiko / discord.js-light

All the power of discord.js, zero caching. This library modifies discord.js's internal classes and functions in order to give you full control over its caching behaviour.
Apache License 2.0
292 stars 29 forks source link

Property 'forceSet' does not exist on type 'Collection<string, Channel>' #75

Closed ySnoopyDogy closed 3 years ago

ySnoopyDogy commented 3 years ago

When using typescipt, then forceSet function from cache manager doesn't exists

Property 'forceSet' does not exist on type 'Collection<string, Channel>'

This error is from

this.client.channels.cache.forceSet(interaction.channelId, channel);

I am trying to migrate from djs to djs-light, and for permissions i am using the first example in README.md from here, and that error appears inside of that if

ySnoopyDogy commented 3 years ago

https://github.com/MenheraBot/MenheraBot/runs/3638596126

More info here

timotejroiko commented 3 years ago

Strange, client.channels.cache should be a LimitedCollection, not a Collection. But anyway i added a typing for it too, can you test if it works? npm i timotejroiko/discord.js-light#v4

ySnoopyDogy commented 3 years ago

Hey, apologies for the delay. It works for the forceSet, but with that i cannot use Collection for value, like create a collection. For me its not a problem, i can use a map instead, but i don't know for others. But yeah, the main problem its fixed. Thanks

timotejroiko commented 3 years ago

why cant you use it? what happens if you try to use it?

ySnoopyDogy commented 3 years ago

If i import both, LimitedCollection and Collection from discord.js-light it says 'Collection' only refers to a type, but is being used as a value here.. The code is

import { Collection, LimitedCollection } from 'discord.js-light';
const collection = new Collection(); //'Collection' only refers to a type, but is being used as a value here 
const limited = new LimitedCollection(); //'LimitedCollection' only refers to a type, but is being used as a value here
timotejroiko commented 3 years ago

tried something new, can you test the v4 branch again?

ySnoopyDogy commented 3 years ago

Yes, it works! forceSet exists, and limitedCollection and Collection are now classes too. Thanks.

By the way, i saw that the readme shows people that uses discord.js-light, i don't know what i need to do to add it here, but if its available, i would like to add my bot in here. Thanks again for the attention

timotejroiko commented 3 years ago

cool, i'll update it on npm soon, and sure, just make a PR for it

ySnoopyDogy commented 3 years ago

Oky Doki, it just to create a PR to change the readme with my information, right?

ySnoopyDogy commented 3 years ago

Hey, sorry for it, but i just found out that the forceSet problem still exists. I can use forceSet in custom Collection and LimitedCollections, but in collections from discord.js (client.channels in my case) the forceSet function does not exists

EDIT

Actually, i updated to the timotejroiko/discord.js-light#v4 again, and the problem is back for creating Collections, it shows that its a type. forceSet exists in client.channels

timotejroiko commented 3 years ago

made another update, try again

ySnoopyDogy commented 3 years ago

I can create collections, and they have forceSet property, but the collection from djs (client.channels) does not have forceSet

timotejroiko commented 3 years ago

sigh... i hate typescript....

what about now?

ySnoopyDogy commented 3 years ago

I think we'll still have to fight for it

image

timotejroiko commented 3 years ago

hmm, i dont believe its gonna be possible without modifying discordjs/collection...

timotejroiko commented 3 years ago

i've reverted the typings a few steps back, and i believe the solution is to do this instead:

(client.channels.cache as Discord.Collection<Discord.Snowflake, Discord.Channel>).forceSet()

The issue is that internally discord.js does not use Discord.Collection but they directly import discordjs/collection, so i dont think we can override that from this lib.

ySnoopyDogy commented 3 years ago

No problem, i will update my code now. Thank you for all the support

ySnoopyDogy commented 3 years ago
 if (!this.client.channels.cache.has(interaction.channelId)) {
      const channel = await this.client.channels.fetch(interaction.channelId).catch(() => null);
      if (channel) {
        (this.client.channels.cache as Collection<string, ThreadChannel | GuildChannel>).forceSet(
          interaction.channelId,
          channel,
        );
        (
          interaction.guild?.channels.cache as Collection<string, ThreadChannel | GuildChannel>
        ).forceSet(interaction.channelId, channel);
      }
    }

This is my final code taking from reference the first example in your readme, it works pretty well