feat(follow): add host option for notifications and update follow entity

This commit is contained in:
2025-11-03 11:45:42 +01:00
parent d417e7334b
commit a593e05f5c
4 changed files with 37 additions and 18 deletions

View File

@@ -13,7 +13,7 @@ A status bot and other features for protojx.
| Notification system in case of downtime. | 🌐 | | Notification system in case of downtime. | 🌐 |
| Ability to create persistent status messages that update automatically. (/live_status) | 🌐 | | Ability to create persistent status messages that update automatically. (/live_status) | 🌐 |
| Deployment workflow on Oracle VPS. | | | Deployment workflow on Oracle VPS. | |
| Filter for notifs. | | | Filter for notifs. | |
- 🌐 -> In production - 🌐 -> In production
- ✅ -> Done - ✅ -> Done

View File

@@ -2,6 +2,7 @@ import { ApplicationIntegrationType, ChatInputCommandInteraction, InteractionCon
import { CommandDefinition } from "../../type"; import { CommandDefinition } from "../../type";
import { AppDataSource } from "../../data-source"; import { AppDataSource } from "../../data-source";
import { Follow } from "../../entity/follow.entity"; import { Follow } from "../../entity/follow.entity";
import statusService from "../../services/status.service";
const cmd : CommandDefinition = { const cmd : CommandDefinition = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
@@ -14,25 +15,39 @@ const cmd : CommandDefinition = {
InteractionContextType.BotDM, InteractionContextType.BotDM,
InteractionContextType.Guild, InteractionContextType.Guild,
InteractionContextType.PrivateChannel InteractionContextType.PrivateChannel
)
.addStringOption((option) =>
option
.setRequired(true)
.addChoices(...statusService.hosts.filter((v) => v.notify).map((s) => ({name: s.name, value: s.host})))
.setName('host')
.setDescription('Host enable/disable.')
), ),
async execute(interaction : ChatInputCommandInteraction) { async execute(interaction : ChatInputCommandInteraction) {
const userRepo = AppDataSource.getRepository(Follow); const userRepo = AppDataSource.getRepository(Follow);
const hostvalue = interaction.options.getString('host');
const realHost = statusService.hosts.filter((v) => v.host == hostvalue);
if(!hostvalue || realHost.length == 0) {
await interaction.reply({content: '⚠️ Host not found !', flags: [MessageFlags.Ephemeral]});
}else{
let follow = await userRepo.findOne({where: {user_discord: interaction.user.id, host: hostvalue}});
if(!follow) {
follow = new Follow();
follow.user_discord = interaction.user.id;
follow.host = hostvalue;
await userRepo.save(follow);
}
follow.enable = !follow.enable;
let follow = await userRepo.findOne({where: {user_discord: interaction.user.id}});
if(!follow) {
follow = new Follow();
follow.user_discord = interaction.user.id;
await userRepo.save(follow); await userRepo.save(follow);
}
follow.enable = !follow.enable;
await userRepo.save(follow); await interaction.reply({content: `✅ Notification successfully ${follow.enable ? 'enabled 🔔' : 'disabled 🔕'} for ${realHost[0]?.name}!`, flags: [MessageFlags.Ephemeral]});
await interaction.reply({content: `✅ Notification successfully ${follow.enable ? 'enabled 🔔' : 'disabled 🔕'}!`, flags: [MessageFlags.Ephemeral]}); if(follow.enable) {
await interaction.user.send({content: `🔔 Notifications have been successfully enabled for ${realHost[0]?.name} ! To disable: /follow host:${realHost[0]?.name}`})
if(follow.enable) { }
await interaction.user.send({content: '🔔 Notifications have been successfully enabled! To disable: /follow'})
} }
} }
} }

View File

@@ -8,6 +8,9 @@ export class Follow {
@Column() @Column()
user_discord: string; user_discord: string;
@Column()
host: string;
@Column({default: false}) @Column({default: false})
enable: boolean; enable: boolean;
} }

View File

@@ -8,7 +8,7 @@ import { Repository } from "typeorm";
import { Follow } from "../entity/follow.entity"; import { Follow } from "../entity/follow.entity";
import { Guild } from "../entity/guild.entity"; import { Guild } from "../entity/guild.entity";
type Nofity = {time: Date, name : string, alive : boolean, type : InfraType}; type Nofity = {time: Date, name : string, alive : boolean, type : InfraType, host: string};
export class StatusService { export class StatusService {
@@ -204,7 +204,7 @@ export class StatusService {
this.hostsLogRepo.save(log); this.hostsLogRepo.save(log);
if(latestLog && host.notify) { if(latestLog && host.notify) {
notifs.push({alive: host.alive, name: host.name, time: new Date(), type: host.type}); notifs.push({alive: host.alive, name: host.name, time: new Date(), type: host.type, host: host.host});
} }
} }
@@ -233,14 +233,15 @@ export class StatusService {
// ? Notification System (part 2 !): // ? Notification System (part 2 !):
const container = new ContainerBuilder() const container = new ContainerBuilder()
.addTextDisplayComponents((t) => t.setContent(`### 🔔 Status change alert`)); .addTextDisplayComponents((t) => t.setContent(`### 🔔 Status change alert`));
notifs.map(async (n) => { notifs.map(async (n) => {
container.addSeparatorComponents((s)=>s); container.addSeparatorComponents((s)=>s);
container.addTextDisplayComponents((text) => text.setContent(`${n.alive ? process.env.EMOJI_STATUS_ONLINE : process.env.EMOJI_STATUS_OFFLINE} **${n.name}** is now **${n.alive ? 'online' : 'offline'}**\n🏷 Type : ${n.type}\n🕒 Time : <t:${Math.round(new Date().getTime()/1000)}:R>`)); container.addTextDisplayComponents((text) => text.setContent(`${n.alive ? process.env.EMOJI_STATUS_ONLINE : process.env.EMOJI_STATUS_OFFLINE} **${n.name}** is now **${n.alive ? 'online' : 'offline'}**\n🏷 Type : ${n.type}\n🕒 Time : <t:${Math.round(new Date().getTime()/1000)}:R>`));
}); });
const users = await this.followRepo.find({where: {enable: true}}); const users = await this.followRepo.find();
users.forEach(async (user) => { const hosts = notifs.map((n) => n.host);
users.filter(v => hosts.includes(v.host)).forEach(async (user) => {
try { try {
const userdc = await this.client?.users.fetch(user.user_discord); const userdc = await this.client?.users.fetch(user.user_discord);
if(userdc) { if(userdc) {