Skip to content

Commit

Permalink
✨ Handle partial types
Browse files Browse the repository at this point in the history
  • Loading branch information
Androz2091 committed Dec 23, 2020
1 parent 876c3ed commit cd44f57
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 202 deletions.
87 changes: 46 additions & 41 deletions handlers/guildChannel.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,56 @@
import { Client, GuildChannel, TextChannel } from 'discord.js';
import { Channel, Client, TextChannel, GuildChannel } from 'discord.js';

/**
* @handler Channel Events
* @related channelUpdate
*/
export async function handleChannelUpdateEvent(client: Client, oldChannel: GuildChannel, newChannel: GuildChannel) {
export async function handleChannelUpdateEvent(client: Client, oldChannel: Channel, newChannel: Channel) {
let emitted = false;
/**
* @event guildChannelPermissionsUpdate
* @description Emitted when channel permissions are updated.
* @param {DJS:GuildChannel} channel The channel whose permissions have been updated.
* @param {DJS:PermissionOverwrites} oldPermissions Collection of old PermissionOverwrites.
* @param {DJS:PermissionOverwrites} newPermissions Collection of new PermissionOverwrites.
* @example
* client.on("guildChannelPermissionsUpdate", (channel, oldPermissions, newPermissions) => {
* console.log(channel.name+"'s permissions updated!");
* });
*/
if (oldChannel.permissionOverwrites !== newChannel.permissionOverwrites) {
client.emit(
'guildChannelPermissionsUpdate',
newChannel,
oldChannel.permissionOverwrites,
newChannel.permissionOverwrites,
);
emitted = true;
}

if (Object.prototype.hasOwnProperty.call(oldChannel, 'guild')) {

/**
* @event guildChannelPermissionsUpdate
* @description Emitted when channel permissions are updated.
* @param {DJS:GuildChannel} channel The channel whose permissions have been updated.
* @param {DJS:PermissionOverwrites} oldPermissions Collection of old PermissionOverwrites.
* @param {DJS:PermissionOverwrites} newPermissions Collection of new PermissionOverwrites.
* @example
* client.on("guildChannelPermissionsUpdate", (channel, oldPermissions, newPermissions) => {
* console.log(channel.name+"'s permissions updated!");
* });
*/
if ((oldChannel as GuildChannel).permissionOverwrites !== (newChannel as GuildChannel).permissionOverwrites) {
client.emit(
'guildChannelPermissionsUpdate',
newChannel,
(oldChannel as GuildChannel).permissionOverwrites,
(newChannel as GuildChannel).permissionOverwrites,
);
emitted = true;
}

/**
* @event guildChannelTopicUpdate
* @description Emitted when a channel topic changes.
* @param {DJS:GuildChannel} channel The channel whose topic have been updated.
* @param {string} oldTopic The old channel topic.
* @param {string} newTopic The new channel topic.
* @example
* client.on("guildChannelTopicUpdate", (channel, oldTopic, newTopic) => {
* console.log(channel.name+"'s topic changed to " + newTopic +"!");
* });
*/
if (oldChannel.type === 'text' && (oldChannel as TextChannel).topic !== (newChannel as TextChannel).topic) {
client.emit(
'guildChannelTopicUpdate',
newChannel,
(oldChannel as TextChannel).topic,
(newChannel as TextChannel).topic,
);
emitted = true;
}

/**
* @event guildChannelTopicUpdate
* @description Emitted when a channel topic changes.
* @param {DJS:GuildChannel} channel The channel whose topic have been updated.
* @param {string} oldTopic The old channel topic.
* @param {string} newTopic The new channel topic.
* @example
* client.on("guildChannelTopicUpdate", (channel, oldTopic, newTopic) => {
* console.log(channel.name+"'s topic changed to " + newTopic +"!");
* });
*/
if (oldChannel.type === 'text' && (oldChannel as TextChannel).topic !== (newChannel as TextChannel).topic) {
client.emit(
'guildChannelTopicUpdate',
newChannel,
(oldChannel as TextChannel).topic,
(newChannel as TextChannel).topic,
);
emitted = true;
}

/**
Expand Down
162 changes: 84 additions & 78 deletions handlers/guildMemberUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,94 @@
import { Client, GuildMember, Role } from 'discord.js';
import { Client, GuildMember, PartialGuildMember, Role } from 'discord.js';

/**
* @handler Guild Member Events
* @related guildMemberUpdate
*/
export async function handleGuildMemberUpdateEvent(client: Client, oldMember: GuildMember, newMember: GuildMember) {
export async function handleGuildMemberUpdateEvent(client: Client, oldMember: GuildMember | PartialGuildMember, newMember: GuildMember) {
let emitted = false;
/**
* @event guildMemberBoost
* @description Emitted when a member starts boosting.
* @param {DJS:GuildMember} member The member who started boosting.
* @example
* client.on("guildMemberBoost", (member) => {
* console.log(member.user.tag+" has started boosting "+member.guild.name+"!");
* });
*/
if (!oldMember.premiumSince && newMember.premiumSince) {
client.emit('guildMemberBoost', newMember);
emitted = true;
}
/**
* @event guildMemberUnboost
* @description Emitted when a member stops boosting.
* @param {DJS:GuildMember} member The member who stopped boosting.
* @example
* client.on("guildMemberUnboost", (member) => {
* console.log(member.user.tag+" has stopped boosting "+member.guild.name+"...");
* });
*/
if (oldMember.premiumSince && !newMember.premiumSince) {
client.emit('guildMemberUnboost', newMember);
emitted = true;
}
const addedRoles: Role[] = [];
newMember.roles.cache.forEach((role) => {
if (!oldMember.roles.cache.has(role.id)) addedRoles.push(role);
});
/**
* @event guildMemberRoleAdd
* @description Emitted when a member acquires a new role.
* @param {DJS:GuildMember} member The member who acquired the role.
* @param {DJS:Role} role The role the member has acquired.
* @example
* client.on("guildMemberRoleAdd", (member, role) => {
* console.log(member.user.tag+" acquired the role: "+role.name);
* });
*/
addedRoles.forEach((role) => {
client.emit('guildMemberRoleAdd', oldMember, role);
emitted = true;
});
const removedRoles: Role[] = [];
oldMember.roles.cache.forEach((role) => {
if (!newMember.roles.cache.has(role.id)) removedRoles.push(role);
});
/**
* @event guildMemberRoleRemove
* @description Emitted when a member looses a new role.
* @param {DJS:GuildMember} member The member who lost the role.
* @param {DJS:Role} role The role the member has lost.
* @example
* client.on("guildMemberRoleRemove", (member, role) => {
* console.log(member.user.tag+" lost the role: "+role.name);
* });
*/
removedRoles.forEach((role) => {
client.emit('guildMemberRoleRemove', oldMember, role);
emitted = true;
});
/**
* @event guildMemberNicknameUpdate
* @description Emitted when a member's nickname changes.
* @param {DJS:GuildMember} member The member whose nickname has changed.
* @param {string} oldNickname The member's old nickname.
* @param {string} newNickname The member's new nickname.
* @example
* client.on("guildMemberNicknameUpdate", (member, oldNickname, newNickname) => {
* console.log(member.user.tag+"'s nickname is now "+newNickname);
* });
*/
if (oldMember.nickname !== newMember.nickname) {
client.emit('guildMemberNicknameUpdate', newMember, oldMember.nickname, newMember.nickname);
emitted = true;

if (!oldMember.partial) {

/**
* @event guildMemberBoost
* @description Emitted when a member starts boosting.
* @param {DJS:GuildMember} member The member who started boosting.
* @example
* client.on("guildMemberBoost", (member) => {
* console.log(member.user.tag+" has started boosting "+member.guild.name+"!");
* });
*/
if (!oldMember.premiumSince && newMember.premiumSince) {
client.emit('guildMemberBoost', newMember);
emitted = true;
}
/**
* @event guildMemberUnboost
* @description Emitted when a member stops boosting.
* @param {DJS:GuildMember} member The member who stopped boosting.
* @example
* client.on("guildMemberUnboost", (member) => {
* console.log(member.user.tag+" has stopped boosting "+member.guild.name+"...");
* });
*/
if (oldMember.premiumSince && !newMember.premiumSince) {
client.emit('guildMemberUnboost', newMember);
emitted = true;
}
const addedRoles: Role[] = [];
newMember.roles.cache.forEach((role) => {
if (!oldMember.roles.cache.has(role.id)) addedRoles.push(role);
});
/**
* @event guildMemberRoleAdd
* @description Emitted when a member acquires a new role.
* @param {DJS:GuildMember} member The member who acquired the role.
* @param {DJS:Role} role The role the member has acquired.
* @example
* client.on("guildMemberRoleAdd", (member, role) => {
* console.log(member.user.tag+" acquired the role: "+role.name);
* });
*/
addedRoles.forEach((role) => {
client.emit('guildMemberRoleAdd', oldMember, role);
emitted = true;
});
const removedRoles: Role[] = [];
oldMember.roles.cache.forEach((role) => {
if (!newMember.roles.cache.has(role.id)) removedRoles.push(role);
});
/**
* @event guildMemberRoleRemove
* @description Emitted when a member looses a new role.
* @param {DJS:GuildMember} member The member who lost the role.
* @param {DJS:Role} role The role the member has lost.
* @example
* client.on("guildMemberRoleRemove", (member, role) => {
* console.log(member.user.tag+" lost the role: "+role.name);
* });
*/
removedRoles.forEach((role) => {
client.emit('guildMemberRoleRemove', oldMember, role);
emitted = true;
});
/**
* @event guildMemberNicknameUpdate
* @description Emitted when a member's nickname changes.
* @param {DJS:GuildMember} member The member whose nickname has changed.
* @param {string} oldNickname The member's old nickname.
* @param {string} newNickname The member's new nickname.
* @example
* client.on("guildMemberNicknameUpdate", (member, oldNickname, newNickname) => {
* console.log(member.user.tag+"'s nickname is now "+newNickname);
* });
*/
if (oldMember.nickname !== newMember.nickname) {
client.emit('guildMemberNicknameUpdate', newMember, oldMember.nickname, newMember.nickname);
emitted = true;
}

}

/**
* @event unhandledGuildMemberUpdate
* @description Emitted when the guildMemberUpdate event is triggered but discord-logs didn't trigger any custom event.
Expand Down
64 changes: 35 additions & 29 deletions handlers/messageUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
import { Client, Message } from 'discord.js';
import { Client, Message, PartialMessage } from 'discord.js';

/**
* @handler Message Events
* @related messageUpdate
*/
export async function handleMessageUpdateEvent(client: Client, oldMessage: Message, newMessage: Message) {
export async function handleMessageUpdateEvent(client: Client, oldMessage: Message | PartialMessage, newMessage: Message | PartialMessage) {

let emitted = false;
/**
* @event messagePinned
* @description Emitted when a message has been pinned.
* @param {DJS:Message} message The message that was pinned.
* @example
* client.on("messagePinned", (message) => {
* console.log("This message has been pinned : "+message);
* });
*/
if (!oldMessage.pinned && newMessage.pinned) {
client.emit('messagePinned', newMessage);
emitted = true;
}
/**
* @event messageContentEdited
* @description Emitted when a message content has been edited.
* @param {DJS:Message} message The old message.
* @param {string} oldContent The message content before it was edited.
* @param {string} newContent The message content after it was edited.
* @example
* client.on("messageContentEdited", (message, oldContent, newContent) => {
* console.log("Message '"+message.id+"' has been edited to "+newContent);
* });
*/
if (oldMessage.content !== newMessage.content) {
client.emit('messageContentEdited', newMessage, oldMessage.content, newMessage.content);
emitted = true;
if (!oldMessage.partial && !newMessage.partial) {

/**
* @event messagePinned
* @description Emitted when a message has been pinned.
* @param {DJS:Message} message The message that was pinned.
* @example
* client.on("messagePinned", (message) => {
* console.log("This message has been pinned : "+message);
* });
*/
if (!oldMessage.pinned && newMessage.pinned) {
client.emit('messagePinned', newMessage);
emitted = true;
}
/**
* @event messageContentEdited
* @description Emitted when a message content has been edited.
* @param {DJS:Message} message The old message.
* @param {string} oldContent The message content before it was edited.
* @param {string} newContent The message content after it was edited.
* @example
* client.on("messageContentEdited", (message, oldContent, newContent) => {
* console.log("Message '"+message.id+"' has been edited to "+newContent);
* });
*/
if (oldMessage.content !== newMessage.content) {
client.emit('messageContentEdited', newMessage, oldMessage.content, newMessage.content);
emitted = true;
}

}

/**
* @event unhandledMessageUpdate
* @description Emitted when the messageUpdate event is triggered but discord-logs didn't trigger any custom event.
Expand Down
Loading

0 comments on commit cd44f57

Please sign in to comment.