RolesService wip

This commit is contained in:
mo
2026-03-06 23:36:35 +01:00
parent 73b1d82f43
commit dd7553f294
8 changed files with 168 additions and 15 deletions

View File

@@ -1,5 +1,4 @@
import { createLogger } from "@avocadi/bot-core/lib/logger";
import type { User } from "@fluxerjs/core";
import { config } from "config";
import { RolesService } from "entities/roles/roles.service";
import { acceptUserService } from "features/accept-user/accept-user.service";
@@ -8,15 +7,6 @@ import client from "lib/client";
const logger = createLogger("ReactionsListener");
// client.on("messageReactionRemove", async (reaction, user) => {
// reactionRolesService.handleReaction(
// reaction,
// user,
// Roles.enum.people,
// "remove",
// );
// });
client.on("messageReactionAdd", async (reaction, user) => {
const guild = reaction.message.guild;
if (!guild) {

View File

@@ -1 +0,0 @@
console.log("Hello via Bun!");

View File

@@ -17,7 +17,7 @@
"dependencies": {
"@avocadi/bot-core": "workspace:*",
"@discordjs/rest": "^2.6.0",
"@fluxerjs/core": "^1.1.7",
"@fluxerjs/core": "^1.2.3",
"cron": "^4.4.0",
"dotenv": "^17.3.1",
"dotenv-expand": "^12.0.3",

View File

@@ -0,0 +1,39 @@
import type { RolesServiceInterface } from "@avocadi/bot-core/entities/roles/roles.service";
import { createLogger } from "@avocadi/bot-core/lib/logger";
import type { GuildMember } from "@fluxerjs/core";
export class RolesService implements RolesServiceInterface<GuildMember> {
private logger = createLogger("RolesService");
async assignRole(user: GuildMember, roleId: string) {
const roleToAssign = user.guild.roles.has(roleId);
if (!roleToAssign) {
this.logger.error(
`Role ${roleId} not found in guild ${user.guild.name}.`,
);
return;
}
this.logger.info(`Role ${roleId} found in guild ${user.guild.name}.`);
await user.guild.addRoleToMember(user.id, roleId);
}
async removeRole(user: GuildMember, role: string) {
const roleToRemove = user.guild.roles.has(role);
if (!roleToRemove) {
this.logger.error(`Role ${role} not found in guild ${user.guild.name}.`);
return;
}
await user.guild.removeRoleFromMember(user.id, role);
}
/*** get all roles of GuildMember */
async getRoles(user: GuildMember) {
return user.roles.map((role) => role);
}
/*** check if GuildMember has a specific role */
async hasRole(user: GuildMember, role: string) {
return user.roles.includes(role);
}
}

View File

@@ -0,0 +1,10 @@
import { AcceptUserService } from "@avocadi/bot-core/features/accept-user/accept-user.service";
import { i18nService } from "@avocadi/bot-core/lib/i18n";
import type { User } from "@fluxerjs/core";
import { messagesService } from "entities/messages/messages.service";
export const acceptUserService = new AcceptUserService<User>(
messagesService,
i18nService,
"en",
);

View File

@@ -0,0 +1,60 @@
import { createLogger } from "@avocadi/bot-core/lib/logger";
import { Events, type MessageReaction, type User } from "@fluxerjs/core";
import { config } from "config";
import { RolesService } from "entities/roles/roles.service";
import { acceptUserService } from "features/accept-user/accept-user.service";
import { logChannelService } from "features/log-channel/log-channel.service";
import client from "lib/client";
const logger = createLogger("ReactionsListener");
client.on(
Events.MessageReactionAdd,
async (reaction: MessageReaction, user: User) => {
const guild = reaction.guild;
if (!guild) {
logger.error(`Guild not found for message ${reaction.messageId}.`);
return;
}
logger.info(`Guild found for message ${reaction.messageId}.`);
const channelIntroduction = await client.channels.fetch(
config.channelMapping.text.introduction,
);
const message = await reaction.fetchMessage();
// check if valid channel
if (message.guild?.id && message.channel?.id === channelIntroduction?.id) {
logger.info("valid channel");
const member = guild?.members.get(user.id);
// check if member who reacted has role MOD
if (member?.roles.includes(config.roleMapping.mod)) {
const messageAuthor_User = message.author;
if (messageAuthor_User) {
const roleMemberId = config.roleMapping.people;
const messageAuthor_GuildMember = await guild?.fetchMember(
messageAuthor_User.id,
);
if (
!messageAuthor_GuildMember?.roles.find((r) => r === roleMemberId)
) {
logger.info(`accepting ${messageAuthor_User?.username}...`);
const rolesService = new RolesService();
rolesService.assignRole(messageAuthor_GuildMember, roleMemberId);
await acceptUserService.sendDmAcceptUser(messageAuthor_User);
await logChannelService.sendLogMessage(
`introduction of <@${messageAuthor_User.id}> (${messageAuthor_User.username}) got accepted`,
);
}
}
}
}
},
);

View File

@@ -0,0 +1,58 @@
import { createLogger } from "@avocadi/bot-core/lib/logger";
import { Events } from "@fluxerjs/core";
import { config } from "config";
import { acceptUserService } from "features/accept-user/accept-user.service";
import { logChannelService } from "features/log-channel/log-channel.service";
import client from "lib/client";
const logger = createLogger("ReactionsListener");
client.on(Events.MessageReactionAdd, async (reaction, user) => {
const guild = reaction.message.guild;
if (!guild) {
logger.error(`Guild not found for message ${reaction.message.id}.`);
return;
}
logger.info(`Guild found for message ${reaction.message.id}.`);
const channelIntroduction = await client.channels.fetch(
config.channelMapping.text.introduction,
);
const message = await reaction.message.fetch();
// check if valid channel
if (message.guild?.id && message.channel?.id === channelIntroduction?.id) {
logger.info("valid channel");
const member = guild?.members.cache.get(user.id);
// check if member who reacted has role MOD
if (member?.roles.cache.get(config.roleMapping.mod)) {
const messageAuthor_User = await message.author.fetch();
if (messageAuthor_User) {
const roleMemberId = config.roleMapping.people;
const messageAuthor_GuildMember = await guild?.members.fetch(
messageAuthor_User.id,
);
if (
!messageAuthor_GuildMember?.roles.cache.find(
(r) => r.id === roleMemberId,
)
) {
logger.info(`accepting ${messageAuthor_User?.username}...`);
const rolesService = new RolesService();
rolesService.assignRole(messageAuthor_GuildMember, roleMemberId);
await acceptUserService.sendDmAcceptUser(messageAuthor_User);
await logChannelService.sendLogMessage(
`introduction of <@${messageAuthor_User.id}> (${messageAuthor_User.username}) got accepted`,
);
}
}
}
}
});