mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 20:43:30 +01:00
Added Guild Deletion
This commit is contained in:
4
TODO.md
4
TODO.md
@@ -5,8 +5,8 @@
|
|||||||
- [ ] updating user details
|
- [ ] updating user details
|
||||||
- [x] deleting messages
|
- [x] deleting messages
|
||||||
- [x] updating messages
|
- [x] updating messages
|
||||||
- [ ] leaving guilds
|
- [x] leaving guilds
|
||||||
- [ ] deleting guilds
|
- [x] deleting guilds
|
||||||
- [ ] _joining guilds_
|
- [ ] _joining guilds_
|
||||||
- [x] creating channels
|
- [x] creating channels
|
||||||
- [x] updating channels
|
- [x] updating channels
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ class ActionsManager {
|
|||||||
this.register('ChannelCreate');
|
this.register('ChannelCreate');
|
||||||
this.register('ChannelDelete');
|
this.register('ChannelDelete');
|
||||||
this.register('ChannelUpdate');
|
this.register('ChannelUpdate');
|
||||||
|
this.register('GuildDelete');
|
||||||
}
|
}
|
||||||
|
|
||||||
register(name) {
|
register(name) {
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ class ChannelDeleteAction extends Action {
|
|||||||
|
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
super(client);
|
super(client);
|
||||||
|
this.timeouts = [];
|
||||||
|
this.deleted = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
handle(data) {
|
handle(data) {
|
||||||
@@ -15,13 +17,28 @@ class ChannelDeleteAction extends Action {
|
|||||||
let channel = client.store.get('channels', data.id);
|
let channel = client.store.get('channels', data.id);
|
||||||
|
|
||||||
if (channel) {
|
if (channel) {
|
||||||
|
|
||||||
client.store.KillChannel(channel);
|
client.store.KillChannel(channel);
|
||||||
|
this.deleted[channel.id] = channel;
|
||||||
|
this.scheduleForDeletion(channel.id);
|
||||||
|
|
||||||
|
} else if (this.deleted[data.id]) {
|
||||||
|
|
||||||
|
channel = this.deleted[data.id];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
channel,
|
channel,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
scheduleForDeletion(id) {
|
||||||
|
this.timeouts.push(
|
||||||
|
setTimeout(() => delete this.deleted[id],
|
||||||
|
this.client.options.rest_ws_bridge_timeout)
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = ChannelDeleteAction;
|
module.exports = ChannelDeleteAction;
|
||||||
|
|||||||
54
src/client/actions/GuildDelete.js
Normal file
54
src/client/actions/GuildDelete.js
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const Action = require('./Action');
|
||||||
|
const Constants = require('../../util/Constants');
|
||||||
|
const Message = require('../../structures/Message');
|
||||||
|
|
||||||
|
class GuildDeleteAction extends Action {
|
||||||
|
|
||||||
|
constructor(client) {
|
||||||
|
super(client);
|
||||||
|
this.deleted = {};
|
||||||
|
this.timeouts = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
handle(data) {
|
||||||
|
|
||||||
|
let client = this.client;
|
||||||
|
let guild = client.store.get('guilds', data.id);
|
||||||
|
|
||||||
|
if (guild) {
|
||||||
|
if (guild.available && data.unavailable) {
|
||||||
|
// guild is unavailable
|
||||||
|
guild.available = false;
|
||||||
|
client.emit(Constants.Events.GUILD_UNAVAILABLE, guild);
|
||||||
|
|
||||||
|
// stops the GuildDelete packet thinking a guild was actually deleted,
|
||||||
|
// handles emitting of event itself
|
||||||
|
return {
|
||||||
|
guild: null,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
// delete guild
|
||||||
|
client.store.remove('guilds', guild);
|
||||||
|
this.deleted[guild.id] = guild;
|
||||||
|
this.scheduleForDeletion(guild.id);
|
||||||
|
}
|
||||||
|
} else if (this.deleted[data.id]) {
|
||||||
|
guild = this.deleted[data.id];
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
guild,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
scheduleForDeletion(id) {
|
||||||
|
this.timeouts.push(
|
||||||
|
setTimeout(() => delete this.deleted[id],
|
||||||
|
this.client.options.rest_ws_bridge_timeout)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = GuildDeleteAction;
|
||||||
@@ -9,6 +9,7 @@ class MessageDeleteAction extends Action {
|
|||||||
constructor(client) {
|
constructor(client) {
|
||||||
super(client);
|
super(client);
|
||||||
this.timeouts = [];
|
this.timeouts = [];
|
||||||
|
this.deleted = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
handle(data) {
|
handle(data) {
|
||||||
@@ -16,9 +17,17 @@ class MessageDeleteAction extends Action {
|
|||||||
let channel = client.store.get('channels', data.channel_id);
|
let channel = client.store.get('channels', data.channel_id);
|
||||||
if (channel) {
|
if (channel) {
|
||||||
let message = channel.store.get('messages', data.id);
|
let message = channel.store.get('messages', data.id);
|
||||||
if (message && !message._deleted) {
|
|
||||||
message._deleted = true;
|
if (message) {
|
||||||
this.scheduleForDeletion(channel, message.id);
|
|
||||||
|
channel.store.remove('messages', message.id);
|
||||||
|
this.deleted[channel.id + message.id] = message;
|
||||||
|
this.scheduleForDeletion(channel.id, message.id);
|
||||||
|
|
||||||
|
} else if (this.deleted[channel.id + data.id]) {
|
||||||
|
|
||||||
|
message = this.deleted[channel.id + data.id];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -31,9 +40,9 @@ class MessageDeleteAction extends Action {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
scheduleForDeletion(channel, id) {
|
scheduleForDeletion(channelID, messageID) {
|
||||||
this.timeouts.push(
|
this.timeouts.push(
|
||||||
setTimeout(() => channel.store.remove('messages', id),
|
setTimeout(() => delete this.deleted[channelID + messageID],
|
||||||
this.client.options.rest_ws_bridge_timeout)
|
this.client.options.rest_ws_bridge_timeout)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -111,6 +111,27 @@ class RESTMethods{
|
|||||||
.catch(reject);
|
.catch(reject);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LeaveGuild(guild) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.rest.makeRequest('del', Constants.Endpoints.ME_GUILD(guild.id), true)
|
||||||
|
.then(() => {
|
||||||
|
resolve(this.rest.client.actions.GuildDelete.handle({ id:guild.id }).guild);
|
||||||
|
})
|
||||||
|
.catch(reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// untested but probably will work
|
||||||
|
DeleteGuild(guild) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.rest.makeRequest('del', Constants.Endpoints.GUILD(guild.id), true)
|
||||||
|
.then(() => {
|
||||||
|
resolve(this.rest.client.actions.GuildDelete.handle({ id:guild.id }).guild);
|
||||||
|
})
|
||||||
|
.catch(reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = RESTMethods;
|
module.exports = RESTMethods;
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class ChannelDeleteHandler extends AbstractHandler {
|
|||||||
let data = packet.d;
|
let data = packet.d;
|
||||||
let client = this.packetManager.client;
|
let client = this.packetManager.client;
|
||||||
|
|
||||||
let response = client.actions.ChannelCreate.handle(data);
|
let response = client.actions.ChannelDelete.handle(data);
|
||||||
|
|
||||||
if (response.channel) {
|
if (response.channel) {
|
||||||
client.emit(Constants.Events.CHANNEL_DELETE, response.channel);
|
client.emit(Constants.Events.CHANNEL_DELETE, response.channel);
|
||||||
|
|||||||
@@ -18,21 +18,10 @@ class GuildDeleteHandler extends AbstractHandler {
|
|||||||
let data = packet.d;
|
let data = packet.d;
|
||||||
let client = this.packetManager.client;
|
let client = this.packetManager.client;
|
||||||
|
|
||||||
let guild = client.store.get('guilds', data.id);
|
let response = client.actions.GuildDelete.handle(data);
|
||||||
|
|
||||||
if (guild) {
|
if (response.guild) {
|
||||||
if (guild.available && data.unavailable) {
|
client.emit(Constants.Events.GUILD_DELETE, response.guild);
|
||||||
// guild is unavailable
|
|
||||||
guild.available = false;
|
|
||||||
client.emit(Constants.Events.GUILD_UNAVAILABLE, guild);
|
|
||||||
} else {
|
|
||||||
// delete guild
|
|
||||||
client.store.KillGuild(guild);
|
|
||||||
this.packetManager.ws.checkIfReady();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// it's not there! :(
|
|
||||||
client.emit('warn', 'guild deleted but not cached in first place. missed packet?');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,6 +135,14 @@ class Guild {
|
|||||||
return this.client.rest.methods.CreateChannel(this, name, type);
|
return this.client.rest.methods.CreateChannel(this, name, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
leave() {
|
||||||
|
return this.client.rest.methods.LeaveGuild(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
delete() {
|
||||||
|
return this.client.rest.methods.DeleteGuild(this);
|
||||||
|
}
|
||||||
|
|
||||||
get channels() { return this.store.getAsArray('channels'); }
|
get channels() { return this.store.getAsArray('channels'); }
|
||||||
|
|
||||||
get $channels() { return this.store.data.channels; }
|
get $channels() { return this.store.data.channels; }
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ function arraysEqual(a, b) {
|
|||||||
if (a === b) return true;
|
if (a === b) return true;
|
||||||
|
|
||||||
for (let itemInd in a) {
|
for (let itemInd in a) {
|
||||||
|
let item = a[itemInd];
|
||||||
let ind = b.indexOf(item);
|
let ind = b.indexOf(item);
|
||||||
if (ind) {
|
if (ind) {
|
||||||
b.splice(ind, 1);
|
b.splice(ind, 1);
|
||||||
|
|||||||
@@ -82,7 +82,6 @@ class ClientDataStore extends AbstractDataStore{
|
|||||||
}
|
}
|
||||||
|
|
||||||
KillChannel(channel) {
|
KillChannel(channel) {
|
||||||
let already = this.get('channels', channel.id);
|
|
||||||
this.remove('channels', channel);
|
this.remove('channels', channel);
|
||||||
if (channel instanceof ServerChannel) {
|
if (channel instanceof ServerChannel) {
|
||||||
channel.guild.store.remove('channels', channel);
|
channel.guild.store.remove('channels', channel);
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ client.on('guildCreate', (guild) => {
|
|||||||
console.log(guild);
|
console.log(guild);
|
||||||
});
|
});
|
||||||
client.on('guildDelete', (guild) => {
|
client.on('guildDelete', (guild) => {
|
||||||
console.log(guild);
|
console.log('guilddel', guild.name);
|
||||||
});
|
});
|
||||||
client.on('guildUpdate', (old, guild) => {
|
client.on('guildUpdate', (old, guild) => {
|
||||||
console.log(old.name, guild.name);
|
console.log(old.name, guild.name);
|
||||||
@@ -23,7 +23,7 @@ client.on('channelCreate', channel => {
|
|||||||
// console.log(channel);
|
// console.log(channel);
|
||||||
});
|
});
|
||||||
client.on('channelDelete', channel => {
|
client.on('channelDelete', channel => {
|
||||||
console.log('channDel', channel);
|
console.log('channDel', channel.name);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('channelUpdate', (old, chan) => {
|
client.on('channelUpdate', (old, chan) => {
|
||||||
@@ -77,12 +77,16 @@ client.on('message', message => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (message.content === 'delchann') {
|
if (message.content === 'delchann') {
|
||||||
message.channel.delete();
|
message.channel.delete().then(chan => console.log('selfDelChann', chan.name));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message.content.startsWith('setname')) {
|
if (message.content.startsWith('setname')) {
|
||||||
message.channel.setName(message.content.substr(8)).then(chanLoop).catch(console.log);
|
message.channel.setName(message.content.substr(8)).then(chanLoop).catch(console.log);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (message.content === 'leave') {
|
||||||
|
message.guild.leave().then(guild => console.log('left guild', guild.name)).catch(console.log);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user