Skip to content

Commit

Permalink
feat(tracking): Track updates to channels & roles
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco (Valandur) committed Dec 4, 2019
1 parent 7faeb03 commit 68c564b
Showing 1 changed file with 86 additions and 10 deletions.
96 changes: 86 additions & 10 deletions src/invites/services/Tracking.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Guild, GuildAuditLog, Invite, Member, Role, TextChannel } from 'eris';
import { AnyChannel, Guild, GuildAuditLog, GuildChannel, Invite, Member, Role, TextChannel } from 'eris';
import i18n from 'i18n';
import moment from 'moment';

Expand Down Expand Up @@ -26,7 +26,11 @@ export class TrackingService {
this.client = client;

client.on('ready', this.onClientReady.bind(this));
client.on('channelCreate', this.onChannelCreate.bind(this));
client.on('channelUpdate', this.onChannelUpdate.bind(this));
client.on('channelDelete', this.onChannelDelete.bind(this));
client.on('guildRoleCreate', this.onGuildRoleCreate.bind(this));
client.on('guildRoleUpdate', this.onGuildRoleUpdate.bind(this));
client.on('guildRoleDelete', this.onGuildRoleDelete.bind(this));
client.on('guildMemberAdd', this.onGuildMemberAdd.bind(this));
client.on('guildMemberRemove', this.onGuildMemberRemove.bind(this));
Expand Down Expand Up @@ -92,6 +96,69 @@ export class TrackingService {
}
}

private async onChannelCreate(channel: AnyChannel) {
if (!(channel instanceof GuildChannel)) {
return;
}

// Ignore disabled guilds
if (this.client.disabledGuilds.has(channel.guild.id)) {
return;
}

await this.client.db.saveChannels([
{
id: channel.id,
name: channel.name,
guildId: channel.guild.id,
createdAt: new Date(channel.createdAt)
}
]);
}

private async onChannelUpdate(channel: AnyChannel, oldChannel: AnyChannel) {
if (!(channel instanceof GuildChannel) || !(oldChannel instanceof GuildChannel)) {
return;
}

// Ignore disabled guilds
if (this.client.disabledGuilds.has(channel.guild.id)) {
return;
}

await this.client.db.saveChannels([
{
id: channel.id,
name: channel.name,
guildId: channel.guild.id,
createdAt: new Date(channel.createdAt)
}
]);
}

private async onChannelDelete(channel: AnyChannel) {
if (!(channel instanceof GuildChannel)) {
return;
}

// Ignore disabled guilds
if (this.client.disabledGuilds.has(channel.guild.id)) {
return;
}

// Remove the channel from the filtered list if it is there
const settings = await this.client.cache.guilds.get(channel.guild.id);
if (settings.channels && settings.channels.some(c => c === channel.id)) {
await this.client.cache.guilds.setOne(
channel.guild.id,
GuildSettingsKey.channels,
settings.channels.filter(c => c !== channel.id)
);
}

// TODO: Delete channel
}

private async onGuildRoleCreate(guild: Guild, role: Role) {
// Ignore disabled guilds
if (this.client.disabledGuilds.has(guild.id)) {
Expand All @@ -103,18 +170,27 @@ export class TrackingService {
color = '0'.repeat(6 - color.length) + color;
}

// Create the guild first, because this event sometimes
// gets triggered before 'guildCreate' for new guilds
await this.client.db.saveGuilds([
await this.client.db.saveRoles([
{
id: guild.id,
name: guild.name,
icon: guild.iconURL,
memberCount: guild.memberCount,
deletedAt: null,
banReason: null
id: role.id,
name: role.name,
color: color,
guildId: role.guild.id,
createdAt: new Date(role.createdAt)
}
]);
}

private async onGuildRoleUpdate(guild: Guild, role: Role, oldRole: Role) {
// Ignore disabled guilds
if (this.client.disabledGuilds.has(guild.id)) {
return;
}

let color = role.color.toString(16);
if (color.length < 6) {
color = '0'.repeat(6 - color.length) + color;
}

await this.client.db.saveRoles([
{
Expand Down

0 comments on commit 68c564b

Please sign in to comment.