Skip to content

Commit

Permalink
feat(join-roles): Add join roles config
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco (Valandur) committed Dec 1, 2019
1 parent 36ecbd0 commit 1fb4b5f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/framework/commands/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export default class extends Command {
}

// Validate a new config value to see if it's ok (no parsing, already done beforehand)
private validate(key: GuildSettingsKey, value: any, { t, me }: Context): string | null {
private validate(key: GuildSettingsKey, value: any, { t, isPremium, me }: Context): string | null {
if (value === null || value === undefined) {
return null;
}
Expand All @@ -172,6 +172,10 @@ export default class extends Command {
return t('cmd.config.invalid.canNotSendEmbeds');
}
}
} else if (key === GuildSettingsKey.joinRoles) {
if (!isPremium && value && value.length > 1) {
return t('cmd.config.invalid.multipleJoinRolesIsPremium');
}
}

return null;
Expand Down
6 changes: 5 additions & 1 deletion src/framework/commands/config/interactiveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ export default class extends Command {
});
}

private validate(key: GuildSettingsKey, value: any, { t, me }: Context): string | null {
private validate(key: GuildSettingsKey, value: any, { t, isPremium, me }: Context): string | null {
if (value === null || value === undefined) {
return null;
}
Expand All @@ -508,6 +508,10 @@ export default class extends Command {
return t('cmd.config.invalid.canNotSendEmbeds');
}
}
} else if (key === GuildSettingsKey.joinRoles) {
if (!isPremium && value && value.length > 1) {
return t('cmd.config.invalid.multipleJoinRolesIsPremium');
}
}

return null;
Expand Down
3 changes: 2 additions & 1 deletion src/framework/models/GuildSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export enum GuildSettingsKey {
channels = 'channels',
ignoredChannels = 'ignoredChannels',

// Join and leave message
// Join and leave
joinRoles = 'joinRoles',
joinMessage = 'joinMessage',
joinMessageChannel = 'joinMessageChannel',
leaveMessage = 'leaveMessage',
Expand Down
17 changes: 15 additions & 2 deletions src/invites/services/Tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,24 @@ export class TrackingService {
console.log(`DISABLING BOT FOR ${guild.id} BECAUSE PRO VERSION IS ACTIVE`);
this.client.disabledGuilds.add(guild.id);
}

// Exit either way
return;
}

// Join roles
const sets = await this.client.cache.guilds.get(guild.id);
if (sets.joinRoles && sets.joinRoles.length > 0) {
if (!guild.members.get(this.client.user.id).permission.has(GuildPermission.MANAGE_ROLES)) {
console.log(`TRYING TO SET JOIN ROLES IN ${guild.id} WITHOUT MANAGE_ROLES PERMISSION`);
} else {
const premium = await this.client.cache.premium.get(guild.id);
const roles = premium ? sets.joinRoles : sets.joinRoles.slice(0, 1);

roles.forEach(role => guild.addMemberRole(member.id, role, 'Join role'));
}
}

// If we don't have manage server then what are we even doing here and why did you invite our bot
if (!guild.members.get(this.client.user.id).permission.has(GuildPermission.MANAGE_GUILD)) {
console.error(`BOT DOESN'T HAVE MANAGE SERVER PERMISSIONS FOR ${guild.id} ON MEMBERADD`);
Expand Down Expand Up @@ -331,8 +346,6 @@ export class TrackingService {
});
}

// Get settings
const sets = await this.client.cache.guilds.get(guild.id);
const lang = sets.lang;
const joinChannelId = sets.joinMessageChannel;

Expand Down
6 changes: 6 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface GuildSettingsObject {
channels: string[];
ignoredChannels: string[];

joinRoles: string[];
joinMessage: string;
joinMessageChannel: string;
leaveMessage: string;
Expand Down Expand Up @@ -204,6 +205,11 @@ export const guildSettingsInfo: {
defaultValue: []
},

joinRoles: {
type: 'Role[]',
grouping: [SettingsGroup.invites, SettingsGroup.general],
defaultValue: []
},
joinMessage: {
type: 'String',
grouping: [SettingsGroup.invites, SettingsGroup.joins],
Expand Down

0 comments on commit 1fb4b5f

Please sign in to comment.