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

73 lines
2.1 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',
priority: 3
});
this.hooks = [
['guildMemberAdd', this.welcome.bind(this)],
['guildMemberAdd', this.autorole.bind(this)]
2020-07-26 22:11:12 +02:00
];
2020-06-20 22:27:42 +02:00
}
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;
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(/\{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)
2020-06-20 22:27:42 +02:00
.trim();
}
}
module.exports = UtilityHook;