galactic-bot/structure/client/components/observers/UtilityHook.js

163 lines
5.5 KiB
JavaScript
Raw Normal View History

2020-06-20 22:27:42 +02:00
const { Observer } = require('../../../interfaces/');
2020-07-26 22:11:12 +02:00
const { Util } = require('../../../../util');
2020-06-20 22:27:42 +02:00
const CONSTANTS = {};
class UtilityHook extends Observer {
constructor(client) {
super(client, {
name: 'utility',
2021-06-09 01:45:34 +02:00
priority: 3,
disabled: false
2020-06-20 22:27:42 +02:00
});
this.hooks = [
['guildMemberAdd', this.welcome.bind(this)],
2020-08-09 01:22:23 +02:00
['guildMemberAdd', this.autorole.bind(this)],
['guildMemberAdd', this.automute.bind(this)],
['guildMemberAdd', this.stickyRole.bind(this)],
['guildMemberRemove', this.storeRoles.bind(this)],
['inviteCreate', this.inviteCreate.bind(this)],
['inviteDelete', this.inviteDelete.bind(this)]
2020-07-26 22:11:12 +02:00
];
2020-06-20 22:27:42 +02:00
}
2020-08-09 01:22:23 +02:00
async storeRoles(member) {
const { guild } = member;
const settings = await guild.settings();
const setting = settings.stickyrole;
if (!setting.roles.length || guild.premium < 1) return;
const { roles } = member;
const storeThese = roles.cache.filter((role) => setting.roles.includes(role.id) && !role.equals(guild.roles.everyone)).map((role) => role.id);
if (!storeThese.length) return;
await this.client.storageManager.mongodb.role_cache.updateOne({ member: member.id, guild: guild.id }, { roles: storeThese, timestamp: Math.floor(Date.now()/1000) });
}
async automute(member) {
const { guild } = member;
const settings = await guild.settings();
const setting = settings.mute;
if (!guild.me.hasPermission('MANAGE_ROLES')) return;
const infraction = await this.client.storageManager.mongodb.infractions.findOne({
duration: { $gt: 0 },
guild: guild.id,
target: member.id,
type: 'MUTE',
_callbacked: false
2020-08-09 01:22:23 +02:00
});
if (!infraction || infraction.resolved) return;
if (infraction.data.muteType === 0) await member.roles.add(setting.role, 'automute upon rejoin, type 0');
else if (infraction.data.muteType === 1) {
2020-11-12 00:00:07 +01:00
await Util.wait(5 * 1000); //wait 5 seconds in case other bots add roles that would break the mute
2020-08-09 01:22:23 +02:00
const roles = member.roles.cache;
const managed = roles.find((r) => r.managed);
const remove = roles.filter((r) => !r.managed); //Have to do this bs for managed roles -.-
if (managed) {
await member.roles.add(setting.role, 'automute upon rejoin, type 1');
await member.roles.remove(remove, 'removing excess roles for type 1 mute');
} else await member.roles.set(setting.role, 'automute upon join, type 1');
} else if (infraction.data.muteType === 2) {
2020-11-12 00:00:07 +01:00
await Util.wait(5 * 1000); //wait 5 seconds in case other bots add roles that would break the mute
2020-08-09 01:22:23 +02:00
const roles = member.roles.cache;
const remove = roles.filter((r) => !r.managed); //Have to do this bs for managed roles -.-
await member.roles.remove(remove, 'automute upon join, type 1');
}
}
2020-06-20 22:27:42 +02:00
async stickyRole(member) {
const { guild } = member;
const settings = await guild.settings();
2020-08-09 01:22:23 +02:00
const setting = settings.stickyrole;
if (!setting.roles.length || guild.premium < 1) return;
if (!guild.me.hasPermission('MANAGE_ROLES')) return;
const data = await this.client.storageManager.mongodb.role_cache.findOne({ guild: guild.id, member: member.id });
if (!data) return;
await member.roles.add(data.roles, 'Adding stickyroles');
2020-06-20 22:27:42 +02:00
}
async autorole(member) {
const { guild } = member;
const settings = await guild.settings();
const setting = settings.autorole;
2020-07-26 22:11:12 +02:00
if (!setting.enabled) return;
if (!guild.me.hasPermission('MANAGE_ROLES')) return;
const _roles = await guild.resolveRoles(setting.roles);
const roles = _roles.map((r) => r.id);
await member.roles.add(roles, 'Adding autoroles').catch(this.client.logger.error);
2020-06-20 22:27:42 +02:00
}
async welcome(member) {
const { guild, user } = member;
const settings = await guild.settings();
const setting = settings.welcomer;
2020-07-26 22:11:12 +02:00
if (!setting.enabled) return;
2020-06-20 22:27:42 +02:00
const channel = await user.createDM();
const message = this._replaceTags(setting.message, member);
await channel.send(message).catch();
}
_replaceTags(text, member) {
const { user, guild } = member;
return text
2020-07-26 22:11:12 +02:00
.replace(/\{mention\}/gu, `<@${member.id}>`)
.replace(/\{tag\}/gu, Util.escapeMarkdown(user.tag))
.replace(/\{user\}/gu, Util.escapeMarkdown(user.username))
.replace(/\{serversize\}/gu, guild.memberCount)
.replace(/\{servername\}/gu, guild.name)
2020-07-26 22:11:12 +02:00
.replace(/\{accage\}/gu, this.client.resolver.timeAgo(Date.now()/1000 - user.createdTimestamp/1000)) //.replace(/a/, '1')
.replace(/\{id\}/gu, user.id)
2020-06-20 22:27:42 +02:00
.trim();
}
async inviteCreate(invite) {
const { guild } = invite;
if (!guild) return;
if (!guild.me.hasPermission('MANAGE_GUILD')) return;
if (!guild.invites) guild.invites = await guild.fetchInvites();
guild.invites.set(invite.code, invite);
}
async inviteDelete(invite) {
const { guild } = invite;
if (!guild) return;
if (!guild.me.hasPermission('MANAGE_GUILD')) return;
if (!guild.invites) guild.invites = await guild.fetchInvites();
guild.invites.delete(invite.code);
}
2020-06-20 22:27:42 +02:00
}
module.exports = UtilityHook;