Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor: bump the minimum node version to 18.x.x #46

Merged
merged 16 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
---
env:
es6: true
es2022: true
node: true
plugins:
- "sort-class-members"
extends: "eslint:recommended"
globals:
BigInt: true
window: true
parserOptions:
ecmaVersion: 2020
rules:
array-bracket-spacing:
- 2
Expand Down Expand Up @@ -65,12 +62,14 @@ rules:
sort-class-members/sort-class-members:
- 2
- order:
- "[alphabetical-properties]"
- constructor
- update
- "[alphabetical-getters]"
- "[alphabetical-methods]"
- "[alphabetical-conventional-private-methods]"
- "[everything-else]"
- "[alphabetical-private-methods]"
- "[custom-inspect-method]"
- toString
- toJSON
Expand All @@ -81,10 +80,16 @@ rules:
alphabetical-methods:
- type: method
sort: alphabetical
alphabetical-properties:
- type: property
sort: alphabetical
alphabetical-conventional-private-methods:
- name: "/_.+/"
type: method
sort: alphabetical
alphabetical-private-methods:
- type: method
sort: alphabetical
custom-inspect-method:
- name: "[util.inspect.custom]"
type: method
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A fork of [Eris](https://github.com/abalabahaha/eris), a Node.js wrapper for int
Installing
----------

You will need NodeJS 10.4+. Voice support requires [additional software](https://github.com/nodejs/node-gyp#installation).
You will need Node.js 18+. Voice support requires [additional software](https://github.com/nodejs/node-gyp#installation).

```
npm install --omit=optional @projectdysnomia/dysnomia
Expand Down
81 changes: 36 additions & 45 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ const AutoModerationRule = require("./structures/AutoModerationRule");
let EventEmitter;
try {
EventEmitter = require("eventemitter3");
} catch(err) {
EventEmitter = require("events");
} catch{
EventEmitter = require("node:events");
}
let Erlpack;
try {
Erlpack = require("erlpack");
} catch(err) { // eslint-disable no-empty
} catch{ // eslint-disable no-empty
}

const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
Expand Down Expand Up @@ -65,6 +65,27 @@ const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
* @prop {Collection<VoiceConnection>} voiceConnections Extended collection of active VoiceConnections the bot has
*/
class Client extends EventEmitter {
channelGuildMap = {};
guilds = new Collection(Guild);
guildShardMap = {};
lastConnect = 0;
lastReconnectDelay = 0;
presence = {
activities: null,
afk: false,
since: null,
status: "offline"
};
privateChannelMap = {};
privateChannels = new Collection(PrivateChannel);
ready = false;
reconnectAttempts = 0;
startTime = 0;
threadGuildMap = {};
unavailableGuilds = new Collection(UnavailableGuild);
users = new Collection(User);
voiceConnections = new VoiceConnectionManager();

/**
* Create a Client
* @arg {String} token The auth token to use. Bot tokens should be prefixed with `Bot` (e.g. `Bot MTExIHlvdSAgdHJpZWQgMTEx.O5rKAA.dQw4w9WgXcQ_wpV-gGA4PSk_bm8`).
Expand Down Expand Up @@ -134,8 +155,8 @@ class Client extends EventEmitter {
throw new TypeError(`Invalid default image size: ${defaultImageSize}`);
}
// Set HTTP Agent on Websockets if not already set
if(this.options.rest.agent && !(this.options.ws && this.options.ws.agent)) {
this.options.ws = this.options.ws || {};
if(this.options.rest.agent && !this.options.ws?.agent) {
this.options.ws ??= {};
this.options.ws.agent = this.options.rest.agent;
}

Expand All @@ -152,29 +173,9 @@ class Client extends EventEmitter {
this.shards = new ShardManager(this, this.options.gateway);
delete this.options.gateway;

this.ready = false;
this.bot = this._token.startsWith("Bot ");
this.startTime = 0;
this.lastConnect = 0;
this.channelGuildMap = {};
this.threadGuildMap = {};
this.guilds = new Collection(Guild);
this.privateChannelMap = {};
this.privateChannels = new Collection(PrivateChannel);
this.guildShardMap = {};
this.unavailableGuilds = new Collection(UnavailableGuild);
this.users = new Collection(User);
this.presence = {
activities: null,
afk: false,
since: null,
status: "offline"
};
this.voiceConnections = new VoiceConnectionManager();

this.connect = this.connect.bind(this);
this.lastReconnectDelay = 0;
this.reconnectAttempts = 0;
}

get uptime() {
Expand Down Expand Up @@ -343,12 +344,10 @@ class Client extends EventEmitter {
throw new Error("Failed to autoshard due to lack of data from Discord.");
}
this.shards.options.maxShards = data.shards;
if(this.shards.options.lastShardID === undefined) {
this.shards.options.lastShardID = data.shards - 1;
}
this.shards.options.lastShardID ??= data.shards - 1;
}

if(this.shards.options.shardConcurrency === "auto" && data.session_start_limit && typeof data.session_start_limit.max_concurrency === "number") {
if(this.shards.options.shardConcurrency === "auto" && typeof data.session_start_limit?.max_concurrency === "number") {
this.shards.options.maxConcurrency = data.session_start_limit.max_concurrency;
}

Expand Down Expand Up @@ -423,7 +422,7 @@ class Client extends EventEmitter {
return this.requestHandler.request("POST", Endpoints.GUILD_CHANNELS(guildID), true, {
name: name,
type: type,
available_tags: options.availableTags && options.availableTags.map((tag) => ({
available_tags: options.availableTags?.map((tag) => ({
id: tag.id,
name: tag.name,
moderated: tag.moderated,
Expand Down Expand Up @@ -1211,7 +1210,7 @@ class Client extends EventEmitter {
return this.requestHandler.request("PATCH", Endpoints.CHANNEL(channelID), true, {
archived: options.archived,
auto_archive_duration: options.autoArchiveDuration,
available_tags: options.availableTags && options.availableTags.map((tag) => ({
available_tags: options.availableTags?.map((tag) => ({
id: tag.id,
name: tag.name,
moderated: tag.moderated,
Expand Down Expand Up @@ -1477,7 +1476,7 @@ class Client extends EventEmitter {
*/
editGuildMember(guildID, memberID, options, reason) {
return this.requestHandler.request("PATCH", Endpoints.GUILD_MEMBER(guildID, memberID), true, {
roles: options.roles && options.roles.filter((roleID, index) => options.roles.indexOf(roleID) === index),
roles: options.roles?.filter((roleID, index) => options.roles.indexOf(roleID) === index),
nick: options.nick,
mute: options.mute,
deaf: options.deaf,
Expand Down Expand Up @@ -2099,9 +2098,7 @@ class Client extends EventEmitter {
* @returns {Promise<{autoModerationRules: Array<AutoModerationRule>, commands: Array<ApplicationCommand>, entries: Array<GuildAuditLogEntry>, events: Array<GuildScheduledEvent>, integrations: Array<PartialIntegration>, threads: Array<NewsThreadChannel | PrivateThreadChannel | PublicThreadChannel>, users: Array<User>, webhooks: Array<Webhook>}>}
*/
getGuildAuditLog(guildID, options = {}) {
if(options.limit === undefined) { // Legacy behavior
options.limit = 50;
}
options.limit ??= 50; // Legacy behavior
if(options.actionType !== undefined) {
options.action_type = options.actionType;
}
Expand Down Expand Up @@ -2405,9 +2402,7 @@ class Client extends EventEmitter {
if(reaction === decodeURI(reaction)) {
reaction = encodeURIComponent(reaction);
}
if(options.limit === undefined) { // Legacy behavior
options.limit = 100;
}
options.limit ??= 100; // Legacy behavior
return this.requestHandler.request("GET", Endpoints.CHANNEL_MESSAGE_REACTION(channelID, messageID, reaction), true, options).then((users) => users.map((user) => new User(user, this)));
}

Expand All @@ -2422,9 +2417,7 @@ class Client extends EventEmitter {
* @returns {Promise<Array<Message>>}
*/
async getMessages(channelID, options = {}) {
if(options.limit === undefined) { // Legacy behavior
options.limit = 50;
}
options.limit ??= 50; // Legacy behavior
let limit = options.limit;
if(limit && limit > 100) {
let logs = [];
Expand Down Expand Up @@ -2797,7 +2790,7 @@ class Client extends EventEmitter {
if(!channel) {
return Promise.reject(new Error("Channel not found"));
}
if(channel.guild && channel.guild.members.has(this.user.id) && !(channel.permissionsOf(this.user.id).allow & Constants.Permissions.voiceConnect)) {
if(channel.guild?.members.has(this.user.id) && !(channel.permissionsOf(this.user.id).allow & Constants.Permissions.voiceConnect)) {
return Promise.reject(new Error("Insufficient permission to connect to voice channel"));
}
this.shards.get(this.guildShardMap[this.channelGuildMap[channelID]] || 0).sendWS(Constants.GatewayOPCodes.VOICE_STATE_UPDATE, {
Expand All @@ -2806,9 +2799,7 @@ class Client extends EventEmitter {
self_mute: options.selfMute || false,
self_deaf: options.selfDeaf || false
});
if(options.opusOnly === undefined) {
options.opusOnly = this.options.opusOnly;
}
options.opusOnly ??= this.options.opusOnly;
return this.voiceConnections.join(this.channelGuildMap[channelID], channelID, options);
}

Expand Down
Loading