galactic-bot/structure/client/components/observers/UtilityHook.js
2020-07-26 23:11:12 +03:00

73 lines
2.1 KiB
JavaScript

const { Observer } = require('../../../interfaces/');
const { Util } = require('../../../../util');
const CONSTANTS = {};
class UtilityHook extends Observer {
constructor(client) {
super(client, {
name: 'utility',
priority: 3
});
this.hooks = [
['guildMemberAdd', this.welcome.bind(this)],
['guildMemberAdd', this.autorole.bind(this)]
];
}
async stickyRole(member) {
const { guild } = member;
const settings = await guild.settings();
const setting = settings.stickyRole;
if(!setting.enabled) return;
}
async autorole(member) {
const { guild } = member;
const settings = await guild.settings();
const setting = settings.autorole;
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);
}
async welcome(member) {
const { guild, user } = member;
const settings = await guild.settings();
const setting = settings.welcomer;
if (!setting.enabled) return;
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
.replace(/\{mention\}/gu, `<@${member.id}>`)
.replace(/\{tag\}/gu, Util.escapeMarkdown(user.tag))
.replace(/\{user\}/gu, Util.escapeMarkdown(user.username))
.replace(/\{guildsize\}/gu, guild.memberCount)
.replace(/\{guildname\}/gu, guild.name)
.replace(/\{accage\}/gu, this.client.resolver.timeAgo(Date.now()/1000 - user.createdTimestamp/1000)) //.replace(/a/, '1')
.replace(/\{id\}/gu, user.id)
.trim();
}
}
module.exports = UtilityHook;