69 lines
2.5 KiB
TypeScript
69 lines
2.5 KiB
TypeScript
import { DmService } from "actions/dm/dm.service";
|
|
import config from "config";
|
|
import type { CacheType, ChatInputCommandInteraction, Guild, GuildMember, MessageReaction, PartialMessageReaction, PartialUser, User } from "discord.js";
|
|
|
|
export class ReactRolesService {
|
|
dmService: DmService;
|
|
|
|
constructor() {
|
|
this.dmService = new DmService();
|
|
}
|
|
|
|
async roleMention(reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser, add: boolean) {
|
|
if (!await this.validMsg(reaction.message.id)) return;
|
|
try {
|
|
await reaction.fetch();
|
|
const guild = reaction.message.guild;
|
|
if (!guild) return;
|
|
const member = await this.getUser(guild, user);
|
|
if (!member) return;
|
|
add ? await this.giveRoleMention(member, guild, user) : await this.removeRoleMention(member, guild, user);
|
|
await this.dmService.roleMentionDm(member, add);
|
|
} catch (error) {
|
|
console.error('smt went wring while roleMention():', error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
async giveRoleMention(member: GuildMember, guild: Guild, user: User | PartialUser) {
|
|
this.updateRoleMention(member, guild, user, true);
|
|
}
|
|
async removeRoleMention(member: GuildMember, guild: Guild, user: User | PartialUser) {
|
|
this.updateRoleMention(member, guild, user, false);
|
|
}
|
|
|
|
async updateRoleMention(member: GuildMember, guild: Guild, user: User | PartialUser, add: boolean) {
|
|
try {
|
|
|
|
const role = guild.roles.cache.get(config.discord.roleMention);
|
|
if (!role) {
|
|
console.error("role ot found.");
|
|
return;
|
|
}
|
|
|
|
if (add === member.roles.cache.has(role.id)) {
|
|
console.log(`${member.user.tag} hat die Rolle *streber* bereits.`);
|
|
return;
|
|
}
|
|
|
|
await (add ? member.roles.add(role) : member.roles.remove(role));
|
|
console.log(`role *streber* successfully ${add ? "added to" : "removed from"} ${member.user.tag}.`);
|
|
} catch (error) {
|
|
console.error(`error while ${add ? "added to" : "removed from"} RoleMention:`, error);
|
|
}
|
|
}
|
|
|
|
async validMsg(id: string) {
|
|
return id === config.discord.rolesMsg;
|
|
}
|
|
|
|
async getUser(guild: Guild, user: User | PartialUser) {
|
|
try {
|
|
return await guild.members.fetch(user.id);
|
|
} catch (error) {
|
|
console.error("error fetching user:", error);
|
|
return null;
|
|
}
|
|
}
|
|
} |