mirror of
https://github.com/thedrewen/protojx-manager.git
synced 2026-03-21 01:48:54 +01:00
feat(follow): add host option for notifications and update follow entity
This commit is contained in:
@@ -13,7 +13,7 @@ A status bot and other features for protojx.
|
||||
| Notification system in case of downtime. | 🌐 |
|
||||
| Ability to create persistent status messages that update automatically. (/live_status) | 🌐 |
|
||||
| Deployment workflow on Oracle VPS. | ➖ |
|
||||
| Filter for notifs. | ➖ |
|
||||
| Filter for notifs. | ✅ |
|
||||
|
||||
- 🌐 -> In production
|
||||
- ✅ -> Done
|
||||
|
||||
@@ -2,6 +2,7 @@ import { ApplicationIntegrationType, ChatInputCommandInteraction, InteractionCon
|
||||
import { CommandDefinition } from "../../type";
|
||||
import { AppDataSource } from "../../data-source";
|
||||
import { Follow } from "../../entity/follow.entity";
|
||||
import statusService from "../../services/status.service";
|
||||
|
||||
const cmd : CommandDefinition = {
|
||||
data: new SlashCommandBuilder()
|
||||
@@ -14,25 +15,39 @@ const cmd : CommandDefinition = {
|
||||
InteractionContextType.BotDM,
|
||||
InteractionContextType.Guild,
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
|
||||
follow.enable = !follow.enable;
|
||||
await interaction.reply({content: `✅ Notification successfully ${follow.enable ? 'enabled 🔔' : 'disabled 🔕'} for ${realHost[0]?.name}!`, flags: [MessageFlags.Ephemeral]});
|
||||
|
||||
await userRepo.save(follow);
|
||||
|
||||
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! To disable: /follow'})
|
||||
if(follow.enable) {
|
||||
await interaction.user.send({content: `🔔 Notifications have been successfully enabled for ${realHost[0]?.name} ! To disable: /follow host:${realHost[0]?.name}`})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,9 @@ export class Follow {
|
||||
@Column()
|
||||
user_discord: string;
|
||||
|
||||
@Column()
|
||||
host: string;
|
||||
|
||||
@Column({default: false})
|
||||
enable: boolean;
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import { Repository } from "typeorm";
|
||||
import { Follow } from "../entity/follow.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 {
|
||||
|
||||
@@ -204,7 +204,7 @@ export class StatusService {
|
||||
this.hostsLogRepo.save(log);
|
||||
|
||||
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});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,8 +239,9 @@ export class StatusService {
|
||||
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}});
|
||||
users.forEach(async (user) => {
|
||||
const users = await this.followRepo.find();
|
||||
const hosts = notifs.map((n) => n.host);
|
||||
users.filter(v => hosts.includes(v.host)).forEach(async (user) => {
|
||||
try {
|
||||
const userdc = await this.client?.users.fetch(user.user_discord);
|
||||
if(userdc) {
|
||||
|
||||
Reference in New Issue
Block a user