refactor: remove user bot methods (#2559)

* [WIP] Remove user bots

* more backend userbot removal

* Add mfaEnabled back

* revert client presences store removal

* partially revert getAuth changes

* remove more no longer used children of ClientUserGuildSettings

* fix a bug with this pr and TextBasedChannel.applyToClass

* remove a syncGuilds reference

* more user bot data handling

* various guildSync cleanup

* bots can't call logout

Had the user/bot portions of the code mixed up. Though, does this need to be a promise anymore?

* make ClientManager#destroy() sync

It nolonger needs to be a promise, and nothing depended on it being a promise that I can tell.

* requested change

* Fix massive error

* no longer used as it's userbot only
This commit is contained in:
bdistin
2018-07-25 21:14:23 -05:00
committed by Isabella
parent f963621ef1
commit 5afd77ab73
34 changed files with 15 additions and 1103 deletions

View File

@@ -97,8 +97,7 @@ class Client extends BaseClient {
this.channels = new ChannelStore(this);
/**
* Presences that have been received for the client user's friends, mapped by user IDs
* <warn>This is only filled when using a user account.</warn>
* Presences that have been received for the client user, mapped by user IDs
* @type {ClientPresenceStore<Snowflake, Presence>}
*/
this.presences = new ClientPresenceStore(this);
@@ -106,7 +105,7 @@ class Client extends BaseClient {
Object.defineProperty(this, 'token', { writable: true });
if (!browser && !this.token && 'CLIENT_TOKEN' in process.env) {
/**
* Authorization token for the logged in user/bot
* Authorization token for the logged in bot
* <warn>This should be kept private at all times.</warn>
* @type {?string}
*/
@@ -240,10 +239,6 @@ class Client extends BaseClient {
/**
* Logs the client in, establishing a websocket connection to Discord.
* <info>Both bot and regular user accounts are supported, but it is highly recommended to use a bot account whenever
* possible. User accounts are subject to harsher ratelimits and other restrictions that don't apply to bot accounts.
* Bot accounts also have access to many features that user accounts cannot utilise. User accounts that are found to
* be abusing/overusing the API will be banned, locking you out of Discord entirely.</info>
* @param {string} token Token of the account to log in with
* @returns {Promise<string>} Token of the account used
* @example
@@ -262,27 +257,13 @@ class Client extends BaseClient {
/**
* Logs out, terminates the connection to Discord, and destroys the client.
* @returns {Promise}
* @returns {void}
*/
destroy() {
super.destroy();
return this.manager.destroy();
}
/**
* Requests a sync of guild data with Discord.
* <info>This can be done automatically every 30 seconds by enabling {@link ClientOptions#sync}.</info>
* <warn>This is only available when using a user account.</warn>
* @param {Guild[]|Collection<Snowflake, Guild>} [guilds=this.guilds] An array or collection of guilds to sync
*/
syncGuilds(guilds = this.guilds) {
if (this.user.bot) return;
this.ws.send({
op: 12,
d: guilds instanceof Collection ? guilds.keyArray() : guilds.map(g => g.id),
});
}
/**
* Obtains an invite from Discord.
* @param {InviteResolvable} invite Invite code or URL
@@ -369,22 +350,16 @@ class Client extends BaseClient {
}
/**
* Obtains the OAuth Application of the bot from Discord.
* @param {Snowflake} [id='@me'] ID of application to fetch
* Obtains the OAuth Application of this bot from Discord.
* @returns {Promise<ClientApplication>}
* @example
* client.fetchApplication('id')
* .then(application => console.log(`Obtained application with name: ${application.name}`)
* .catch(console.error);
*/
fetchApplication(id = '@me') {
return this.api.oauth2.applications(id).get()
fetchApplication() {
return this.api.oauth2.applications('@me').get()
.then(app => new ClientApplication(this, app));
}
/**
* Generates a link that can be used to invite the bot to a guild.
* <warn>This is only available when using a bot account.</warn>
* @param {PermissionResolvable} [permissions] Permissions to request
* @returns {Promise<string>}
* @example

View File

@@ -63,15 +63,7 @@ class ClientManager {
destroy() {
this.client.ws.destroy();
if (!this.client.user) return Promise.resolve();
if (this.client.user.bot) {
this.client.token = null;
return Promise.resolve();
} else {
return this.client.api.logout.post().then(() => {
this.client.token = null;
});
}
if (this.client.user) this.client.token = null;
}
}

View File

@@ -20,8 +20,6 @@ class ActionsManager {
this.register(require('./GuildRoleDelete'));
this.register(require('./GuildRoleUpdate'));
this.register(require('./UserUpdate'));
this.register(require('./UserNoteUpdate'));
this.register(require('./GuildSync'));
this.register(require('./GuildEmojiCreate'));
this.register(require('./GuildEmojiDelete'));
this.register(require('./GuildEmojiUpdate'));

View File

@@ -1,29 +0,0 @@
const Action = require('./Action');
class GuildSync extends Action {
handle(data) {
const client = this.client;
const guild = client.guilds.get(data.id);
if (guild) {
if (data.presences) {
for (const presence of data.presences) guild.presences.add(presence);
}
if (data.members) {
for (const syncMember of data.members) {
const member = guild.members.get(syncMember.user.id);
if (member) {
member._patch(syncMember);
} else {
guild.members.add(syncMember, false);
}
}
}
if ('large' in data) guild.large = data.large;
}
}
}
module.exports = GuildSync;

View File

@@ -1,30 +0,0 @@
const Action = require('./Action');
const { Events } = require('../../util/Constants');
class UserNoteUpdateAction extends Action {
handle(data) {
const client = this.client;
const oldNote = client.user.notes.get(data.id);
const note = data.note.length ? data.note : null;
client.user.notes.set(data.id, note);
client.emit(Events.USER_NOTE_UPDATE, data.id, oldNote, note);
return {
old: oldNote,
updated: note,
};
}
}
/**
* Emitted whenever a note is updated.
* @event Client#userNoteUpdate
* @param {User} user The user the note belongs to
* @param {string} oldNote The note content before the update
* @param {string} newNote The note content after the update
*/
module.exports = UserNoteUpdateAction;

View File

@@ -37,9 +37,6 @@ class WebSocketPacketManager {
this.register(WSEvents.CHANNEL_PINS_UPDATE, require('./handlers/ChannelPinsUpdate'));
this.register(WSEvents.PRESENCE_UPDATE, require('./handlers/PresenceUpdate'));
this.register(WSEvents.USER_UPDATE, require('./handlers/UserUpdate'));
this.register(WSEvents.USER_NOTE_UPDATE, require('./handlers/UserNoteUpdate'));
this.register(WSEvents.USER_SETTINGS_UPDATE, require('./handlers/UserSettingsUpdate'));
this.register(WSEvents.USER_GUILD_SETTINGS_UPDATE, require('./handlers/UserGuildSettingsUpdate'));
this.register(WSEvents.VOICE_STATE_UPDATE, require('./handlers/VoiceStateUpdate'));
this.register(WSEvents.TYPING_START, require('./handlers/TypingStart'));
this.register(WSEvents.MESSAGE_CREATE, require('./handlers/MessageCreate'));
@@ -47,9 +44,6 @@ class WebSocketPacketManager {
this.register(WSEvents.MESSAGE_UPDATE, require('./handlers/MessageUpdate'));
this.register(WSEvents.MESSAGE_DELETE_BULK, require('./handlers/MessageDeleteBulk'));
this.register(WSEvents.VOICE_SERVER_UPDATE, require('./handlers/VoiceServerUpdate'));
this.register(WSEvents.GUILD_SYNC, require('./handlers/GuildSync'));
this.register(WSEvents.RELATIONSHIP_ADD, require('./handlers/RelationshipAdd'));
this.register(WSEvents.RELATIONSHIP_REMOVE, require('./handlers/RelationshipRemove'));
this.register(WSEvents.MESSAGE_REACTION_ADD, require('./handlers/MessageReactionAdd'));
this.register(WSEvents.MESSAGE_REACTION_REMOVE, require('./handlers/MessageReactionRemove'));
this.register(WSEvents.MESSAGE_REACTION_REMOVE_ALL, require('./handlers/MessageReactionRemoveAll'));

View File

@@ -1,11 +0,0 @@
const AbstractHandler = require('./AbstractHandler');
class GuildSyncHandler extends AbstractHandler {
handle(packet) {
const client = this.packetManager.client;
const data = packet.d;
client.actions.GuildSync.handle(data);
}
}
module.exports = GuildSyncHandler;

View File

@@ -9,9 +9,6 @@ class ReadyHandler extends AbstractHandler {
client.ws.heartbeat();
data.user.user_settings = data.user_settings;
data.user.user_guild_settings = data.user_guild_settings;
if (!ClientUser) ClientUser = require('../../../../structures/ClientUser');
const clientUser = new ClientUser(client, data.user);
client.user = clientUser;
@@ -20,27 +17,8 @@ class ReadyHandler extends AbstractHandler {
for (const guild of data.guilds) client.guilds.add(guild);
for (const privateDM of data.private_channels) client.channels.add(privateDM);
for (const relation of data.relationships) {
const user = client.users.add(relation.user);
if (relation.type === 1) {
client.user.friends.set(user.id, user);
} else if (relation.type === 2) {
client.user.blocked.set(user.id, user);
}
}
for (const presence of data.presences || []) client.presences.add(presence);
if (data.notes) {
for (const user in data.notes) {
let note = data.notes[user];
if (!note.length) note = null;
client.user.notes.set(user, note);
}
}
if (!client.users.has('1')) {
client.users.add({
id: '1',
@@ -61,7 +39,6 @@ class ReadyHandler extends AbstractHandler {
client.setMaxListeners(data.guilds.length + 10);
client.once('ready', () => {
client.syncGuilds();
client.setMaxListeners(10);
client.clearTimeout(t);
});

View File

@@ -1,19 +0,0 @@
const AbstractHandler = require('./AbstractHandler');
class RelationshipAddHandler extends AbstractHandler {
handle(packet) {
const client = this.packetManager.client;
const data = packet.d;
if (data.type === 1) {
client.users.fetch(data.id).then(user => {
client.user.friends.set(user.id, user);
});
} else if (data.type === 2) {
client.users.fetch(data.id).then(user => {
client.user.blocked.set(user.id, user);
});
}
}
}
module.exports = RelationshipAddHandler;

View File

@@ -1,19 +0,0 @@
const AbstractHandler = require('./AbstractHandler');
class RelationshipRemoveHandler extends AbstractHandler {
handle(packet) {
const client = this.packetManager.client;
const data = packet.d;
if (data.type === 2) {
if (client.user.blocked.has(data.id)) {
client.user.blocked.delete(data.id);
}
} else if (data.type === 1) {
if (client.user.friends.has(data.id)) {
client.user.friends.delete(data.id);
}
}
}
}
module.exports = RelationshipRemoveHandler;

View File

@@ -1,21 +0,0 @@
const AbstractHandler = require('./AbstractHandler');
const { Events } = require('../../../../util/Constants');
const ClientUserGuildSettings = require('../../../../structures/ClientUserGuildSettings');
class UserGuildSettingsUpdateHandler extends AbstractHandler {
handle(packet) {
const client = this.packetManager.client;
const settings = client.user.guildSettings.get(packet.d.guild_id);
if (settings) settings.patch(packet.d);
else client.user.guildSettings.set(packet.d.guild_id, new ClientUserGuildSettings(this.client, packet.d));
client.emit(Events.USER_GUILD_SETTINGS_UPDATE, client.user.guildSettings.get(packet.d.guild_id));
}
}
/**
* Emitted whenever the client user's settings update.
* @event Client#clientUserGuildSettingsUpdate
* @param {ClientUserGuildSettings} clientUserGuildSettings The new client user guild settings
*/
module.exports = UserGuildSettingsUpdateHandler;

View File

@@ -1,12 +0,0 @@
const AbstractHandler = require('./AbstractHandler');
class UserNoteUpdateHandler extends AbstractHandler {
handle(packet) {
const client = this.packetManager.client;
const data = packet.d;
client.actions.UserNoteUpdate.handle(data);
}
}
module.exports = UserNoteUpdateHandler;

View File

@@ -1,18 +0,0 @@
const AbstractHandler = require('./AbstractHandler');
const { Events } = require('../../../../util/Constants');
class UserSettingsUpdateHandler extends AbstractHandler {
handle(packet) {
const client = this.packetManager.client;
client.user.settings.patch(packet.d);
client.emit(Events.USER_SETTINGS_UPDATE, client.user.settings);
}
}
/**
* Emitted whenever the client user's settings update.
* @event Client#clientUserSettingsUpdate
* @param {ClientUserSettings} clientUserSettings The new client user settings
*/
module.exports = UserSettingsUpdateHandler;