Git Product home page Git Product logo

discord.js-light's Introduction

discord.js-light v4

Discord.js v13 introduces several major changes including a completely new caching system which enables users to fully customize the library to match their caching preferences, however not all caching configurations are officially supported and some may introduce several side effects.

This library aims to improve support and usability when using discord.js with limited or disabled caches.

Branches

  • master - based on the discord.js master branch (not actively maintained, should not be used)
  • v4 - current npm version, based on discord.js v13
  • v3 - old npm version, based on discord.js v12
  • v2 - deprecated
  • v1 - deprecated

v4 Features

  • Fully supports the new discord.js caching system, including unsupported configurations, with little to no side effects
  • Discord.js partials system removed and replaced with an internal "always on" solution
  • Events always work, regardless of caching options (partial structures are given when missing), see djs-cache-test
  • Partials can be created on demand to interact with the Discord API without fetching first
  • Additional utilities to improve usability with caches disabled

Usage

const Discord = require("discord.js-light");
const client = new Discord.Client({
    // default caching options, feel free to copy and modify. more info on caching options below.
    makeCache: Discord.Options.cacheWithLimits({
        ApplicationCommandManager: 0, // guild.commands
        BaseGuildEmojiManager: 0, // guild.emojis
        ChannelManager: 0, // client.channels
        GuildChannelManager: 0, // guild.channels
        GuildBanManager: 0, // guild.bans
        GuildInviteManager: 0, // guild.invites
        GuildManager: Infinity, // client.guilds
        GuildMemberManager: 0, // guild.members
        GuildStickerManager: 0, // guild.stickers
        GuildScheduledEventManager: 0, // guild.scheduledEvents
        MessageManager: 0, // channel.messages
        PermissionOverwriteManager: 0, // channel.permissionOverwrites
        PresenceManager: 0, // guild.presences
        ReactionManager: 0, // message.reactions
        ReactionUserManager: 0, // reaction.users
        RoleManager: 0, // guild.roles
        StageInstanceManager: 0, // guild.stageInstances
        ThreadManager: 0, // channel.threads
        ThreadMemberManager: 0, // threadchannel.members
        UserManager: 0, // client.users
        VoiceStateManager: 0 // guild.voiceStates
    }),
    intents: [ /* your intents here */ ],
});

client.on("messageCreate", async message => {
    if(!message.guild) return;
    // if .partial is true, only .id is guaranteed to exist, all other properties will be either null or undefined, if you need them, you must fetch them from the api
    const guildNameOrId = message.guild.partial ? message.guild.id : message.guild.name;
    const channelNameOrId = message.channel.partial ? message.channel.id : message.channel.name;
    await message.channel.send(`hello from guild ${guildNameOrId} and channel ${channelNameOrId}`);
});

client.login("your token here");

Cache Configuration

Discord.js's new caching configuration is very powerful, here are a few examples:

{
    ChannelManager: 0, // cache disabled. nothing will ever be added, except when using .cache.forceSet(). items added with forceSet can be updated and deleted normally.
    GuildChannelManager: { maxSize: 0 }, // same as above
    RoleManager: 5, // only 5 items will be allowed, any new item will first remove the oldest by insertion order before being added.
    UserManager: {
        maxSize: 10,
        /**
         * if set, this function is called when a new item is added and the collection is already at the limit.
         * the collection starts looking for the oldest item to remove and tests each item with this function.
         * if the function returns true, the item is not removed, and the next is tested. The first item that returns false is removed, then the new item is inserted.
         * If maxSize is 0 or if updating an existing item, this function is not called.
         * This example will prevent the bot user from ever being removed due to the cache being full and some other user will be removed instead.
        */
        keepOverLimit: (value, key, collection) => value.id === value.client.user.id
    },
    GuildMemberManager: {
        maxSize: Infinity,
        /**
         * if set, automatic sweeping is enabled, and this function is called every sweepInterval seconds.
         * this function provides the collection being swept, and expects another function as a return value.
         * the returned function will be given to collection.sweep() internally, and will delete all items for which the function returns true.
         * this example will remove all members except the bot member, every 600 seconds.
        */
        /*
         * As of discord.js version 13.4.0+ (discord.js-light 4.5.0+) sweepFilter and sweepInterval are deprecated.
         * Instead use the new client sweeper options.
        */
        sweepFilter: collection => (value, key, collection) => value.id !== value.client.user.id,
        sweepInterval: 600 // autosweep interval in seconds
    }
}

New sweeper options added with discord.js v13.4.0 (discord.js-light 4.5.0):

new Discord.Client({
    makeCache: Discord.Options.cacheWithLimits({
        UserManager: {
            maxSize: 50,
            keepOverLimit: (value, key, collection) => value.id === value.client.user.id
        },
        MessageManager: {
            maxSize: 100
        }
    }),
    sweepers: {
        users: {
            interval: 600,
            filter: user => user.id !== user.client.user.id
        },
        messages: {
            interval: 600,
            filter: message => message.author?.id !== message.client.user.id
        }
    }
})

For further information refer to the official discord.js documentation.

Non-standard stuff

...Manager#forge

All managers implement this method to create partial versions of uncached objects on demand. This enables making API requests without fetching uncached objects first. This is only used to send requests to the API, if you need to access the object's properties, you need to fetch it. Some forge methods require additional parameters, check your intelisense.

await client.users.forge(id).send("hello");

Collection#forceSet

All caches implement this method, which is the same as .cache.set() but works even if the caches are completely disabled (set to 0). Use this to manually cache fetched items.

const user = await client.users.fetch(id);
client.users.cache.forceSet(id, user);

GuildChannel#fetchOverwrites

Method added to improve accessibility to permission checking when caches are disabled. This can be used to work with permissions directly but to use regular permission checking methods, they need to be manually added to the cache.

const overwrites = await channel.fetchOverwrites();
console.log(overwrites) // Collection<PermissionOverwrites>

// to enable permission checking in this channel if permissionOverwrites are not cached
overwrites.forEach(overwrite => {
    channel.permissionOverwrites.cache.forceSet(overwrite.id, overwrite);
})

shardConnect

Event fired when each individual internal shard connects.

client.on("shardConnect", (shardId, guilds) => {
    console.log(shardId) // shard ID
    console.log(guilds) // array of unavailable guilds as per the Discord API
});

guildEmojisUpdate

Event fired instead of the standard emoji events when the emoji cache is disabled. If the emojis cache is disabled, emojiCreate, emojiUpdate and emojiDelete will never be fired.

client.on("guildEmojisUpdate", emojis => {
    console.log(emojis) // Collection<GuildEmoji>
})

guildStickersUpdate

Event fired instead of the standard sticker events when the sticker cache is disabled. If the stickers cache is disabled, stickerCreate, stickerUpdate and stickerDelete will never be fired.

client.on("guildStickersUpdate", stickers => {
    console.log(stickers) // Collection<Sticker>
})

rest

Event fired when the library makes a request to the discord API. Use this to debug rate limit issues.

client.on("rest", request => {
    console.log(request); /*
    {
        path: string,
        method: string,
        responseHeaders: object,
        responseBody: string
    } */
});

Notes and Important Info

Fetching data does not automatically cache if cache limits are set to 0. Use the non-standard Collection#forceSet method instead to manually cache fetched items. Manually cached items can be accessed, updated, swept and removed normally.

The bot member is cached by default (unless removed by the user). GuildMemberManager auto-sweep will still remove it if not excluded in your sweepFilter.

The everyone role is cached by default (unless removed by the user). RoleManager auto-sweep will still remove it if not excluded in your sweepFilter.

ChannelManager and GuildChannelManager should be configured together, otherwise weird things can happen if they have different configurations, use at your own risk. If anything prioritize enabling ChannelManager over GuildChannelManager.

Note about continued development: v4 will likely be the last discord.js-light version. As of v13, discord.js is now much better than what it used to be in terms of configurability and resource management, and v14 will likely be even better, to the point where discord.js-light probably wont be useful anymore. For now v4 will still be maintained and will still keep up with discord.js's v13 development as needed, but this adventure might reach a conclusion soon. So long and thanks for all the fish!

Examples

An example for maximizing cache efficiency while keeping full support for permission checking. Using the new autosweep functionality and the non-standard forceSet methods to keep only active text channels cached.

const Discord = require("discord.js-light");

// remove non-text channels and remove text channels whose last message is older than 1 hour
function channelFilter(channel) {
    return !channel.lastMessageId || Discord.SnowflakeUtil.timestampFrom(channel.lastMessageId) < Date.now() - 3600000;
}

const makeCache = Discord.Options.cacheWithLimits({
    GuildManager: Infinity, // roles require guilds
    RoleManager: Infinity, // cache all roles
    PermissionOverwrites: Infinity, // cache all PermissionOverwrites. It only costs memory if the channel it belongs to is cached
    ChannelManager: {
        maxSize: 0, // prevent automatic caching
        sweepFilter: () => channelFilter, // remove manually cached channels according to the filter
        sweepInterval: 3600
    },
    GuildChannelManager: {
        maxSize: 0, // prevent automatic caching
        sweepFilter: () => channelFilter, // remove manually cached channels according to the filter
        sweepInterval: 3600
    },
    /* other caches */
});

const client = new Discord.Client({ makeCache, intents: [ /* your intents */ ] });

client.on("messageCreate", async message => {
    // if the channel is not cached, manually cache it while its active
    if(!client.channels.cache.has(message.channel.id)) {
        const channel = await client.channels.fetch(message.channel.id);
        client.channels.cache.forceSet(channel.id, channel); // manually cache it in client.channels
        message.guild?.channels.cache.forceSet(channel.id, channel); // manually cache it in guild.channels
    }
    console.log(message.channel.permissionsFor(message.member));
});

An example for caching roles only for the bot itself and ignoring all other roles. Use in combination with the previous example if you want to check permissions in channels as well.

const Discord = require("discord.js-light");

const makeCache = Discord.Options.cacheWithLimits({
    GuildManager: Infinity, // roles require guilds
    RoleManager: {
        maxSize: Infinity, // start with cache enabled to get initial roles, then disable it in the ready event
        sweepFilter: () => role => !role.guild.me.roles.has(role.id), // remove roles the bot doesnt have
        sweepInterval: 3600
    },
    /* other caches */
});

const client = new Discord.Client({ makeCache, intents: [ /* your intents */ ] });

client.on("ready", () => {
    client.guilds.forEach(guild => {
        // disable cache and remove roles we dont have
        guild.roles.cache.maxSize = 0;
        guild.roles.cache.sweep(role => !guild.me.roles.has(role.id))
    });
});

// this event is still sent for the bot user even if you dont have the GUILD_MEMBERS intent
client.on("guildMemberUpdate", async (oldMember, newMember) => {
    if(newMember.id === client.user.id) {
        // check for new roles and fetch them as needed
        // removed roles will be uncached later by the autosweeper
        for(const role of newMember.roles.cache.values()) {
            if(role.partial) {
                const fetched = await newMember.guild.roles.fetch(role.id);
                newMember.guild.roles.cache.forceSet(role.id, fetched);
            }
        }
    }
});

Bots using discord.js-light

Bot Servers
Birthday Bot
Dio
Truth or Dare
Nagito 2
Friend Time
Bump Reminder
D-Safe
QOTD Bot
Suggestions
Filo
Alita
Kable
Astrobot
Piรฑa Bot
Monika
Hydra bot
CalcBot
Helper
Utix
Custom Command
Scathach
Melody
Foxy ๐ŸฆŠ
Game Tracker
Anti NSFW
Denky
Gerald
Animal Bot
Slash
T_Moderator_Bot
CleverChat
Tamaki
Music Boat
Message Viewer
Art Prompts
T_Music_Bot
Kirby!
Droid
EcchiBot
Stalk.live
Multipurpose+
Corynth
Stereo
Coconut Mall'd
Discord.js Bot Template
Growtopian
MenheraBot
GameStatus

discord.js-light's People

Contributors

acollierr17 avatar almeidx avatar anna-rmrf avatar aykutsarac avatar douile avatar flav-code avatar googlesites avatar jacobk999 avatar jonnyapple445 avatar kevinnovak avatar marcrock22 avatar meister03 avatar ortovoxx avatar scottbucher avatar some1chan avatar timotejroiko avatar ysnoopydogy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

discord.js-light's Issues

Fetching members from a guild returns partial results

Hello,
When using <Guild>.members.fetch(), the Member and User structures returned are sometimes partial for everyone except the bot itself. Not sure if this is the intended behavior, since it is my understanding that fetching should return non-partial results.

Example:

guild.members.fetch({ withPresences: true }).then(members => {
    console.log(members.map(x => x.displayName)); // sometimes returns ["bot name", null, null, null, etc...]
    console.log(members.map(x => x.user)); // sometimes returns User structures with nothing except for the ID
});

discord.js-light version: 3.5.3
Client options:

const client = new Client({
    partials: ["MESSAGE", "CHANNEL", "REACTION", "USER", "GUILD_MEMBER"],
    ws: { intents: ["DIRECT_MESSAGES", "DIRECT_MESSAGE_REACTIONS", "GUILDS", "GUILD_BANS", "GUILD_EMOJIS", "GUILD_INVITES", "GUILD_MEMBERS", "GUILD_PRESENCES", "GUILD_MESSAGES", "GUILD_MESSAGE_REACTIONS", "GUILD_VOICE_STATES"] },
    cacheChannels: true,
    cacheEmojis: true,
    cacheOverwrites: true,
    cacheGuilds: true,
    cacheRoles: true,
    cachePresences: true,
});

Shard 0 keeps reconnecting

image

I'm guessing this isn't supposed to be happening? I haven't tested whether or not it can still receive messages while it's reconnecting but it keeps saying that in console

Privileged Intents Support

Private Intents does not work on commit "7c753b9d6a59ed149464d14415123772fa9e49ab".
I wrote the code like this.

const client = new Client({
  intents: [Intents.FLAGS.GUILD_MESSAGES, Intents.PRIVILEGED],
});

client.on("message", (msg) => {
  if (msg.author.bot) return;
  console.log(msg.author.presence.status);
});

However, only offline appears in the console log.

Also, it doesn't seem to work in the official release of v3.5.11.
This code was created when using the above version, and will only output offline to the console log.

const client = new Client({
  ws: {
    intents: [Intents.FLAGS.GUILD_MESSAGES, Intents.PRIVILEGED],
  },
});

client.on("message", (msg) => {
  if (msg.author.bot) return;
  console.log(msg.author.presence.status);
});

GuildChannel.permissionsFor(user) returns null with all caches enabled

When using the GuildChannel.permissionsFor(), null is returned.

Example:

const Discord = require('discord.js-light');
const client = new Discord.Client({
    cacheGuilds: true,
    cacheChannels: true,
    cacheOverwrites: true,
    cacheRoles: true,
    cacheEmojis: true,
    cachePresences: true,
});

const MY_TOKEN = '{INSERT_TOKEN_HERE}';

(() => {
    client.on('message', msg => {
        if (msg.content.toLowerCase() !== 'test') {
            return;
        }

        // This comes back null
        let authorPerms = msg.channel.permissionsFor(msg.author);

        // This will fail because the above is null
        let authorIsAdmin = authorPerms.has('ADMINISTRATOR');

        msg.channel.send(authorIsAdmin ? 'You are an admin!' : 'You are not an admin!');
    });

    client.login(MY_TOKEN);
})();

TypeError: Cannot read property 'slice' of null

Hey, I use the below code and got error, how can i fix it?

`TypeError: Cannot read property 'slice' of null

let high = parseInt(num.slice(0, -10)) || 0;
^

function channelFilter(channel) {
return !channel.messages || Discord.SnowflakeUtil.deconstruct(channel.lastMessageId).timestamp < Date.now() - 3600000;
}

ChannelManager: {
		maxSize: Infinity, // 
		sweepFilter: () => channelFilter, // remove manually cached channels according to the filter
		sweepInterval: 3600
	},
	GuildChannelManager: {
		maxSize: Infinity, // 
		sweepFilter: () => channelFilter, // remove manually cached channels according to the filter
		sweepInterval: 3600
	},

MessageFlags is not defined

if(this.channel.type !== 'news' || this.type !== 'DEFAULT' || this.flags.has(MessageFlags.FLAGS.CROSSPOSTED)) { return false; }

Got error "ReferenceError: MessageFlags is not defined" while trying to check property "crosspostable" of message.

discord-api-types conflicts

Hello, i'm facing an issue with the api types with this latest update of v4

Here are my version installed:

discord.js: 13.7.0 (uses api types 0.30.0)
discord.js-light: 4.7.0 (uses api types 0.33.0)

The error

Type 'MessageActionRow<MessageActionRowComponent | TextInputComponent, MessageActionRowComponentResolvable, { ...; }>' is not assignable to type 'MessageActionRow<MessageActionRowComponent, MessageActionRowComponentResolvable, APIActionRowComponent<APIMessageActionRowComponent>>'.
  Types of property 'components' are incompatible.
    Type '(MessageActionRowComponent | TextInputComponent)[]' is not assignable to type 'MessageActionRowComponent[]'.
      Type 'MessageActionRowComponent | TextInputComponent' is not assignable to type 'MessageActionRowComponent'.

46 const actionRow = (components: MessageActionRowComponentResolvable[]): MessageActionRow => new MessageActionRow({ components });
The inferred type of 'default' cannot be named without a reference to 'discord.js/node_modules/discord-api-types/v9'. This is likely not portable. A type annotation is necessary.

The code

const actionRow = (components: MessageActionRowComponentResolvable[]): MessageActionRow => new MessageActionRow({ components });

export default {
    actionRow: actionRow,
}

How i actually import from api types

import { APIMessage } from 'discord.js/node_modules/discord-api-types/v9 (or v10)';

I don't exactly understand what the error is here. If you have any idea that would be great ! :)

Getting an error

Hey, I'm getting this error:

TypeError: Cannot read properties of null (reading 'members')
    at Message._patch (./node_modules/discord.js-light/extensions.js:80:78)
    at Message._update (./node_modules/discord.js/src/structures/Base.js:30:10)
    at MessageUpdateAction.MessageUpdate [as handle] (./node_modules/discord.js-light/actions.js:402:18)
    at Object.MESSAGE_UPDATE (./node_modules/discord.js-light/handlers.js:268:57)
    at WebSocketManager.Client.ws.handlePacket (./node_modules/discord.js-light/client.js:47:29)
    at WebSocketShard.onPacket (./node_modules/discord.js/src/client/websocket/WebSocketShard.js:443:22)
    at WebSocketShard.onMessage (./node_modules/discord.js/src/client/websocket/WebSocketShard.js:300:10)
    at WebSocket.onMessage (./node_modules/ws/lib/event-target.js:199:18)
    at WebSocket.emit (node:events:390:28)
    at Receiver.receiverOnMessage (./node_modules/ws/lib/websocket.js:1022:20)

GuildManager cache is on Infinity.

Buttons Support

Are you able to add buttons support to djs light as per the djs update?

Question

Hi there! I really love this wrapper.

Could you possibly explain why I'm unable to read usernames from this code? When console logging, it shows the user info but when putting it into code it does not.

Test:

const test = await req.app.get("client").users.fetch("359498825150365699");
console.log(test);

Code: (EJS)

<%= await discord_data.users.fetch(i.userID).username ? discord_data.users.fetch(i.userID).username : "Not In Guild" %> 

Notes: discord_data is the discord.js light client. i.userID is a user ID string from my database.

RangeError: "StageChannel" is not a valid extensible structure.

Hi, I'm using the discord.js-light master build, and whenever I start it up using the master build, I get this error:
image
and I'm not sure if the error is my fault or if it's to do with the library. Is there anything I can do to fix it, or is there any fix on the way?
Thanks!

how do i get all channels in a guild?

so If I pass in the options for no channelCaching, how can I get all channels in a guild? Would It be like:

<Guild>.channels.fetch();
//do something
<Guild>.channels.cache.deleteAll();

or does it automatically remove it from the cache?

TextChannel can never be an instance of User

discord.js-light/classes.js

Lines 283 to 285 in c0abe51

Discord.Structures.extend("TextChannel", T => class TextChannel extends T {
async send(content, options) {
if (this instanceof Discord.User || this instanceof Discord.GuildMember) {

if (this instanceof Discord.User)

but as you can see

class TextChannel extends T

where T is TextChannel from discord.js

`guildCreate` doesn't get triggered on init

Currently there's no way to get guilds on launch with ChannelManager and GuildManager being LimitedCollection with size = 0 without fetching them with client.guilds.forge(exampleGuildId).channels.fetch() (ChannelManager) and client.guilds.forge(exampleGuildId).fetch() (GuildManager) as example.

client.on("raw",console.log) shows, that we receive GUILD_CREATE events (with channels and threads) without getting them on guildCreate or guildUpdate (shardConnect works for this option, but will do it only on sharding).

I was collecting parentId's of guild channels and thread parentIds, so there's no way to get them at launch without fetching.

Code:

...

const categories = new Collection<Snowflake, Snowflake>() // { channel => category }
const threads = new Collection<Snowflake, Snowflake>() // { thread => channel }

// categories and threads are always synced:
client.on("channelCreate",this._onChannelCreate);
client.on("channelUpdate",this._onChannelUpdate);
client.on("channelDelete",this._onChannelDelete);
client.on("threadCreate",this._onThreadCreate);
client.on("threadDelete",this._onThreadDelete);
client.on("threadUpdate",this._onThreadUpdate);
client.on("threadListSync",this._onThreadSync);

const channels = await this.client.guilds.forge(config().constants.trustedGuild).channels.fetch(); // can't receive on guildUpdate or guildCreate
// same with threads

channels.forEach(channel => {
   if (!channel.parentId || !channel.isText()) return;
   this.categories.set(channel.id, channel.parentId)
})

...

messageCreate: (leveling.channelMultipliers contains { Snowflake: number } with channel/category ids and multiplier values)

const channelId = message.channel.isThread() ? this.threads.get(message.channelId) : message.channelId;
const categoryId = channelId ? this.categories.get(channelId) : null;

const channelMultiplier = leveling.channelMultipliers[channelId!] ?? leveling.channelMultipliers[categoryId!] ?? 0;

console.log(`${channelId}'s multiplier is ${channelMultiplier}`);

Cannot read properties of undefined (reading '_add')

Hi
I don't know if this is an issue with discord.js light, but this is coming from there.

TypeError: Cannot read properties of undefined (reading '_add')
    at MessageCreateAction.MessageCreate [as handle] (/app/node_modules/discord.js-light/actions.js:440:36)
    at Object.MESSAGE_CREATE (/app/node_modules/discord.js-light/handlers.js:281:52)
    at WebSocketManager.Client.ws.handlePacket (/app/node_modules/discord.js-light/client.js:47:29)
    at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/app/node_modules/ws/lib/event-target.js:199:18)
    at WebSocket.emit (node:events:394:28)
    at Receiver.receiverOnMessage (/app/node_modules/ws/lib/websocket.js:1137:20)
    at Receiver.emit (node:events:394:28)
    at Receiver.dataMessage (/app/node_modules/ws/lib/receiver.js:528:14)

versions:
discord.js : 13.6.0
discord.js light: 4.6.1

Disconnecting and not reconnecting

I'm getting that error in all of sudden while it's working fine.

   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR Caught exception: TypeError: Cannot read property 'guild' of undefined
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR TypeError: Cannot read property 'guild' of undefined
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at ChannelManager.remove (/home/vcap/deps/0/node_modules/discord.js/src/managers/ChannelManager.js:44:17)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at GuildDeleteAction.client.actions.GuildDelete.handle (/home/vcap/deps/0/node_modules/discord.js-light/actions.js:126:15)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at Object.PacketHandlers.GUILD_DELETE (/home/vcap/deps/0/node_modules/discord.js-light/handlers.js:106:47)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at WebSocketManager.client.ws.handlePacket (/home/vcap/deps/0/node_modules/discord.js-light/actions.js:38:28)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at WebSocketShard.onPacket (/home/vcap/deps/0/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at WebSocketShard.onMessage (/home/vcap/deps/0/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at WebSocket.onMessage (/home/vcap/deps/0/node_modules/ws/lib/event-target.js:132:16)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at WebSocket.emit (node:events:378:20)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at Receiver.receiverOnMessage (/home/vcap/deps/0/node_modules/ws/lib/websocket.js:825:20)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at Receiver.emit (node:events:378:20)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at Receiver.dataMessage (/home/vcap/deps/0/node_modules/ws/lib/receiver.js:428:14)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at Receiver.getData (/home/vcap/deps/0/node_modules/ws/lib/receiver.js:367:17)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at Receiver.startLoop (/home/vcap/deps/0/node_modules/ws/lib/receiver.js:143:22)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at Receiver._write (/home/vcap/deps/0/node_modules/ws/lib/receiver.js:78:10)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at writeOrBuffer (node:internal/streams/writable:400:12)
   2021-02-09T16:45:54.69+0300 [APP/PROC/WEB/0] ERR     at _write (node:internal/streams/writable:341:10)
   2021-02-09T16:46:53.80+0300 [APP/PROC/WEB/0] OUT Debug log: [WS => Shard 0] [HeartbeatTimer] Didn't receive a heartbeat ack last time, assuming
zombie connection. Destroying and reconnecting.
   2021-02-09T16:46:53.80+0300 [APP/PROC/WEB/0] OUT     Status          : READY
   2021-02-09T16:46:53.80+0300 [APP/PROC/WEB/0] OUT     Sequence        : 66582
   2021-02-09T16:46:53.80+0300 [APP/PROC/WEB/0] OUT     Connection State: OPEN
   2021-02-09T16:46:53.80+0300 [APP/PROC/WEB/0] OUT Debug log: [WS => Shard 0] [DESTROY]
   2021-02-09T16:46:53.80+0300 [APP/PROC/WEB/0] OUT     Close Code    : 4009
   2021-02-09T16:46:53.80+0300 [APP/PROC/WEB/0] OUT     Reset         : true
   2021-02-09T16:46:53.80+0300 [APP/PROC/WEB/0] OUT     Emit DESTROYED: true
   2021-02-09T16:46:53.80+0300 [APP/PROC/WEB/0] OUT Debug log: [WS => Shard 0] Clearing the heartbeat interval.

Error in makePartial and in actions

Hello.

Most of the shards on my bot crashed early today due to this error:

TypeError: Object.defineProperty called on non-object
    at Function.defineProperty (<anonymous>)
    at makePartial (./node_modules/discord.js-light/functions.js:26:9)
    at ChannelUpdateAction.ChannelUpdate [as handle] (./node_modules/discord.js-light/actions.js:64:4)
    at Object.CHANNEL_UPDATE (./node_modules/discord.js-light/handlers.js:99:57)
    at WebSocketManager.Client.ws.handlePacket (./node_modules/discord.js-light/client.js:47:29)
    at WebSocketShard.onPacket (./node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (./node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (./node_modules/ws/lib/event-target.js:199:18)
    at WebSocket.emit (node:events:390:28)
    at Receiver.receiverOnMessage (./node_modules/ws/lib/websocket.js:1098:20)

I also got a notification about this one a few days ago:

TypeError: Cannot convert undefined or null to object
    at Function.values (<anonymous>)
    at ThreadListSyncAction.ThreadListSync [as handle] (./node_modules/discord.js-light/actions.js:667:33)
    at Object.THREAD_LIST_SYNC (./node_modules/discord.js-light/handlers.js:376:33)
    at WebSocketManager.Client.ws.handlePacket (./node_modules/discord.js-light/client.js:47:29)
    at WebSocketShard.onPacket (./node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (./node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (./node_modules/ws/lib/event-target.js:199:18)
    at WebSocket.emit (node:events:390:28)
    at Receiver.receiverOnMessage (./node_modules/ws/lib/websocket.js:1098:20)
    at Receiver.emit (node:events:390:28)

Also do you have a Discord server where I could ask if I set up the caching properly? I've got some caching issues with channels and channel permissions.

MessageManager#forge doesn't set channel or guild data

When creating a message with MessageManager#forge the created message doesn't have a guild or channel set.

Example code:

const Discord = require('discord.js-light');
const client = new Discord.Client({ intents: [] });

const guild = client.guilds.forge('765694954416373771');
const channel = guild.channels.forge('765694954416373771', 'GUILD_TEXT');
const message = channel.messages.forge('960328996095148082');
console.log(message);
/*
Message {
  channelId: undefined,                 <------ These are unset
  guildId: null,
  id: '960328996095148082',
  createdTimestamp: 1649030674719,
  system: null,
  type: null,
  content: null,
  author: null,
  pinned: null,
  tts: null,
  nonce: null,
  embeds: [],
  components: [],
  attachments: Collection(0) [Map] {},
  stickers: Collection(0) [Map] {},
  editedTimestamp: null,
  reactions: ReactionManager { message: [Circular *1] },
  mentions: MessageMentions {
    everyone: false,
    users: Collection(0) [Map] {},
    roles: Collection(0) [Map] {},
    _members: null,
    _channels: null,
    crosspostedChannels: Collection(0) [Map] {},
    repliedUser: null
  },
  webhookId: null,
  groupActivityApplication: null,
  applicationId: null,
  activity: null,
  flags: MessageFlags { bitfield: 0 },
  reference: null,
  interaction: null
}
*/

Discord.MessageManager.prototype.forge = function(id) {
return this._add({ id }, false);
};

Permission issues

we were going to switch our bot to discord.js light but one issue stopped us.
when looking for permissions on a channel for our bot the bitfield that returns is always 0.

Shards: "auto" not working

I have an application that delivery (now) 230 bots with same source code, the idea is to make code as generic as possible and keep it health to host. Your lib is helping me a lot, but I'm having problem to make shard works in automatic mode. Maybe there is a way to use ShardManager, but I would have to check which bot needs Shards or not, and rewrite a big part of the struct code (which isn't a problem, but maybe it doesn't scale so good).

In summary, shards: "auto" already solved my situation using discord.js main lib, BUT, costing me 2000% more RAM than your library.

Question: I'm doing something wrong or shards auto option (in ClientOptions params) isn't working?
PS: In rare cases it works, but in most part of time not.

Error [SHARDING_REQUIRED]: This session would have handled too many guilds - Sharding is required.
801|vps | at WebSocketManager.createShards (/root/fork-heroku-bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:249:15)
801|vps | at async Client.login (/root/fork-heroku-bot/node_modules/discord.js/src/client/Client.js:245:7)
801|vps | at async upsetEvents (/root/fork-heroku-bot/dist/bot.js:336:21) {
801|vps | [Symbol(code)]: 'SHARDING_REQUIRED'
801|vps | }

const config = {

  intents: ["GUILD_MESSAGES"],

  makeCache: Discord.Options.cacheWithLimits({
    ApplicationCommandManager: 0, 
    BaseGuildEmojiManager: 0, 
    ChannelManager: 0, 
    GuildChannelManager: 0, 
    GuildBanManager: 0, 
    GuildInviteManager: 0, 
    GuildManager: Infinity, 
    GuildMemberManager: 0,
    GuildStickerManager: 0, 
    MessageManager: 0, 
    PermissionOverwriteManager: 0,
    PresenceManager: 0, 
    ReactionManager: 0,
    ReactionUserManager: 0, 
    RoleManager: 0, 
    StageInstanceManager: 0, 
    ThreadManager: 0, 
    ThreadMemberManager: 0, 
    UserManager: 0, 
    VoiceStateManager: 0 
  })
}
const Discord = require("discord.js-light")

...
some code here
...

discordApiKey.forEach((_) => this.client.push(new Discord.Client({
      ...config,
      shards: "auto"
})))

v13

When will discord.js-light support v13?

Channel NSFW status

The library seems to return an incorrect NSFW status for text channels in message objects when channel caching is disabled.
Despite the channel being NSFW, the property always is false.

Migration from discord.js

Hey!

I'm the owner of a fairly large Discord bot, and I'm considering migrating my code from discord.js to discord.js-light, because their memory usage is getting absolutely stupid at 7k+ servers.

I have a few questions:

  • Are there ANY API differences and gotchas I should be looking out for while I switch to this library, or is it literally just a drop-in replacement without caching? I assume I do have to make sure data is in cache now before pulling it, which i could basically avoid before since discord.js caches everything, but I mean anything other than that.
  • Would you be interested in migrating discord.js-light's codebase to Typescript? If i ever find the courage I might be interested in doing that, doesn't seem like a huge amount of work from what I've seen.
  • Are github issues the best way to contact you? In case I have problems & bugs as I migrate my project, I wanna make sure I ask in the right place.

TypeScript support?

When using TypeScript, the editor is unable to find the type declarations file (I'm guessing since it is included in discord.js but not in discord.js-light?):

Missing TypeScript

Is there a way to include or redirect typescript definitions to make discord.js-light usable with typescript?

Owner Name and NickName

I'm trying to load the information from the server and save it to a database. I'm getting information like name, id, urlImage, owner id ... But I want to get the owner's name, and I'm getting a null result. I know that DiscordLight does not store cache. Would there be any other way of obtaining this information?

const Discord = require('discord.js-light');
const client = new Discord.Client({
  cacheGuilds: true,
  cacheChannels: true,
  cacheOverwrites: false,
  cacheRoles: true,
  cacheEmojis: false,
  cachePresences: false
});

client.on('ready', async () => {
  console.log(`Logged in as ${client.user.tag}!`);

  client.guilds.cache.forEach((value) => {
    console.log(value.name);  //return Guild Name
    console.log(value.id); //return Guild ID
    console.log(value.iconURL()); //return urlImage
    console.log(value.owner); //return null
    console.log(value.ownerID); //return Owner Id
  });    
});

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

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

Can't set maxSize on cache.

Hey, according to your example about caching roles only for the bot itself and ignoring all other roles you set the maxSize to 0.
In your types however there's only forceSet function defined and maxSize is not.
Because of that, TypeScript throws me an error.
image

LimitedCollection Typescript Error

Hello, I'm having a typescript error.
It's warning me that 'All declarations of "LimitedCollection' must have identical type parameters" in node_modules/discord.js-light/client.d.ts:35:12 and node_modules/discord.js-light/node_modules/discord.js/typings/index.d.ts:1084:14

node: v16.6.0
discord.js: v13.1.0
discord.js-light: v4.1.4
image_2021-09-12_183650

How to send webhook in the thread?

const channel = await client.channels.fetch(discord.channelId);
let webhooks = await channel.fetchWebhooks();
webhook = webhooks.find(v => {
return v.name == 'XOXO' && v.type == "Incoming";
})
await webhook.send({ content: text, username: message.myName.username, avatarURL: message.myName.avatarURL });

It is work in channel, but get error in thread

error TypeError: channel.fetchWebhooks is not a function

Discord.js V14

Do you have an ETA on when you will release a version for Discord.js V14 maybe on a V5 branch ?

<VoiceChannel>.join() don't work

Hello, i use <VoiceChannel>.join() and the bot say:

Error [VOICE_JOIN_CHANNEL]: You do not have permission to join this voice channel.

i view the code so <VoiceChannel>.joinable and <VoiceChannel>.viewable always is false.

Fetching multiple members a 2nd time returns an array instead of a map

the first guild.members.fetch returns a collection, the second call returns an array. This should only return a collection
Unit test

test('members fetch issue', async () => {
    const client = await login(); // login using discord.js-light
    const guild = await client.guilds.fetch("YOUR_GUILD_ID");
    const mapOrArray1 = await guild.members.fetch({user: ["MEMBER_1", "MEMBER_2"]});
    console.log(Array.isArray(mapOrArray1)); // false
    const mapOrArray2 = await guild.members.fetch({user: ["MEMBER_1", "MEMBER_2"]});
    console.log(Array.isArray(mapOrArray2)); // true ???
},999999);

I'm using this code to handle it for now

module.exports = async ({guild, playerIds}) => {
    const members = await guild.members.fetch({user: playerIds});
    if (Array.isArray(members)) {
        return new Map(members.map(member => [member.id, member]));
    } else {
        return members;
    }
};

Here's my client config

new Discord.Client({
    cacheGuilds: true,
    cacheChannels: false,
    cacheOverwrites: false,
    cacheRoles: true,
    cacheEmojis: false,
    cachePresences: false
})

I've tested this same code with discord.js and it always returns a collection

TypeError: Cannot read property 'id' of undefined

Hi, just here to report an issue I've just noticed within Discord JS light. Somehow, it seems possible that a new guild member can come through with an undefined ID, crashing the app. Please let me know whether this is an issue you can resolve or which I should take up to Discord JS.

Sadly, as this is an automated logging which happened about 16 minutes ago at the time of writing, I don't know much more than this as it was not in a development situation but in a live bot. It's worth noting that this bot has been fine for multiple weeks now, and this is the first issue I seemingly am not able to resolve myself.

Thank you for the library and I hope I'm both at the right place and that I can get this issue resolved with you.

Cheers!

Aug 06 09:20:34.447am info app TypeError: Cannot read property 'id' of undefined
Aug 06 09:20:34.447am info app at GuildMemberManager.Discord.GuildMemberManager.add (/app/node_modules/discord.js-light/classes.js:460:103)
Aug 06 09:20:34.447am info app at Message._patch (/app/node_modules/discord.js-light/classes.js:31:28)
Aug 06 09:20:34.447am info app at new Message (/app/node_modules/discord.js-light/node_modules/discord.js/src/structures/Message.js:43:20)
Aug 06 09:20:34.447am info app at new Message (/app/node_modules/discord.js-light/classes.js:6:9)
Aug 06 09:20:34.447am info app at Immediate.<anonymous> (/app/node_modules/discord.js-light/actions.js:29:29)
Aug 06 09:20:34.447am info app at processImmediate (internal/timers.js:458:21)
Aug 06 09:20:34.447am info app at MessageManager.add (/app/node_modules/discord.js-light/node_modules/discord.js/src/managers/BaseManager.js:49:32)
Aug 06 09:20:34.447am info app at MessageManager.add (/app/node_modules/discord.js-light/node_modules/discord.js/src/managers/MessageManager.js:29:18)
Aug 06 09:20:34.447am info app at MessageCreateAction.client.actions.MessageCreate.handle (/app/node_modules/discord.js-light/actions.js:221:34)
Aug 06 09:20:34.447am info app at Object.PacketHandlers.MESSAGE_CREATE (/app/node_modules/discord.js-light/handlers.js:170:49)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.