accept user fluxer implementation

This commit is contained in:
mo
2026-03-08 22:45:56 +01:00
parent dd7553f294
commit 5271665963
8 changed files with 156 additions and 169 deletions

View File

@@ -20,11 +20,9 @@ export class MessagesService
async sendToUser(userInput: User, message: MessagePayload): Promise<void> { async sendToUser(userInput: User, message: MessagePayload): Promise<void> {
const user = await client.users.fetch(userInput.id); const user = await client.users.fetch(userInput.id);
if (user) { if (!user) this.logger.error(`User with ID ${userInput.id} not found.`);
await user.send(message); this.logger.info(`sending message to user with ID ${userInput.id}.`);
} else { await user.send(message);
this.logger.error(`User with ID ${userInput.id} not found.`);
}
} }
async logMessage(message: Message): Promise<void> { async logMessage(message: Message): Promise<void> {
@@ -38,7 +36,6 @@ export class MessagesService
recipient = channel.recipient; recipient = channel.recipient;
} }
let logMessage: string; let logMessage: string;
if (recipient) { if (recipient) {
@@ -57,7 +54,16 @@ export class MessagesService
const fetchedChannel = await client.channels.fetch(channel.id); const fetchedChannel = await client.channels.fetch(channel.id);
if (fetchedChannel?.isTextBased() && fetchedChannel.isSendable()) { if (fetchedChannel?.isTextBased() && fetchedChannel.isSendable()) {
await fetchedChannel.send(createMessageInput); this.logger.info(`sending message to channel with ID ${channel.id}.`);
const success = await fetchedChannel.send(createMessageInput);
if (success)
this.logger.info(
`sending message to channel with ID ${channel.id} SUCCESS.`,
);
else
this.logger.error(
`sending message to channel with ID ${channel.id} FAILED.`,
);
} else { } else {
this.logger.error( this.logger.error(
`Channel with ID ${channel.id} not found or is not text-based.`, `Channel with ID ${channel.id} not found or is not text-based.`,

View File

@@ -1,16 +1,24 @@
import { i18nService } from "@avocadi/bot-core/lib/i18n";
import { config } from "config";
import { greetingService } from "features/greeting/greeting.service"; import { greetingService } from "features/greeting/greeting.service";
import { logChannelService } from "features/log-channel/log-channel.service"; import { logChannelService } from "features/log-channel/log-channel.service";
import client from "lib/client"; import client from "lib/client";
import { logger } from "lib/common-logger";
client.on("guildMemberAdd", async (member) => { client.on("guildMemberAdd", async (member) => {
if (member.user.bot) { if (member.user.bot) {
// Don't send a welcome message for bots (sorry tom) // Don't send a welcome message for bots (sorry tom)
return; return;
} }
greetingService.sendGreeting(member.user, member.user.username); const channel = await member.guild.channels.fetch(
logChannelService.sendLogMessage( config.channelMapping.text.welcome,
`Neues Mitglied: <@${member.user.id}> (${member.user.tag}) ist dem Server beigetreten.`,
); );
if (!channel) return;
greetingService.sendGreeting(member.user.id, channel);
greetingService.sendDmGreeting(member.user);
// logChannelService.sendLogMessage(await i18nService.t("greeting1", "de", "greeting"), TODO
}); });

View File

@@ -49,7 +49,7 @@ client.on("messageReactionAdd", async (reaction, user) => {
await acceptUserService.sendDmAcceptUser(messageAuthor_User); await acceptUserService.sendDmAcceptUser(messageAuthor_User);
await logChannelService.sendLogMessage( await logChannelService.sendLogMessage(
`introduction of <@${messageAuthor_User.id}> (${messageAuthor_User.username}) got accepted`, `introduction of <@${messageAuthor_User.id}> got accepted`,
); );
} }
} }

View File

@@ -10,11 +10,11 @@ const configInput: z.input<typeof ConfigSchema> = {
feedback: "1475069278198489089", feedback: "1475069278198489089",
help: "", help: "",
introduction: "1473060169972367394", introduction: "1473060169972367394",
news: "", news: "1474477662908567694",
log: "1473380467480031548", log: "1473380467480031548",
general: "1473029758951358491", general: "1473029758951358491",
rules: "1473070476174811195", rules: "1473070476174811195",
testing: "", testing: "1479976182517571597",
welcome: "1473270114214928440", welcome: "1473270114214928440",
pomodoro: "", pomodoro: "",
}, },

View File

@@ -1,60 +0,0 @@
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

@@ -21,6 +21,6 @@ client.on(Events.GuildMemberAdd, async (member: GuildMember) => {
await logChannelService.sendLogMessage( await logChannelService.sendLogMessage(
`new member: <@${member.user.id}> (${member.user.username}) joined the server.`, `new member: <@${member.user.id}> (${member.user.username}) joined the server.`,
); );
await greetingService.sendGreeting(member.user.username, channel); await greetingService.sendGreeting(member.user.id, channel);
await greetingService.sendDmGreeting(member.user, member.user.username); await greetingService.sendDmGreeting(member.user);
}); });

View File

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

View File

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