mirror of
https://github.com/kirameki-cafe/Yumi.git
synced 2026-09-13 18:59:19 +00:00
Changed to use init function instead
This commit is contained in:
+27
-37
@@ -14,31 +14,7 @@ enum MeasureType {
|
|||||||
DiscordWebsocket = "discordwebsocket"
|
DiscordWebsocket = "discordwebsocket"
|
||||||
}
|
}
|
||||||
|
|
||||||
let measureList = [
|
let measureList: any = [];
|
||||||
{
|
|
||||||
type: MeasureType.Ping,
|
|
||||||
title: "☁️ Internet | ",
|
|
||||||
host: "1.1.1.1"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: MeasureType.DiscordWebsocket,
|
|
||||||
title: "🚀 Discord Websocket | "
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: MeasureType.DiscordHTTPPing,
|
|
||||||
title: "🏓 Discord HTTP | "
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
if(fs.existsSync(path.join(process.cwd(), 'configs/Ping.json'))) {
|
|
||||||
try {
|
|
||||||
const rawData = fs.readFileSync(path.join(process.cwd(), 'configs/Ping.json'));
|
|
||||||
const jsonData = JSON.parse(rawData.toString());
|
|
||||||
measureList = jsonData;
|
|
||||||
} catch (err) {
|
|
||||||
Logger.error("Unable to load custom Ping config: " + err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const EMBEDS = {
|
const EMBEDS = {
|
||||||
PING_INFO: (data: Message | Interaction, description: string) => {
|
PING_INFO: (data: Message | Interaction, description: string) => {
|
||||||
@@ -53,15 +29,29 @@ const EMBEDS = {
|
|||||||
|
|
||||||
export default class Ping {
|
export default class Ping {
|
||||||
|
|
||||||
|
async init() {
|
||||||
|
|
||||||
|
if (fs.existsSync(path.join(process.cwd(), 'configs/Ping.json'))) {
|
||||||
|
try {
|
||||||
|
const rawData = fs.readFileSync(path.join(process.cwd(), 'configs/Ping.json'));
|
||||||
|
const jsonData = JSON.parse(rawData.toString());
|
||||||
|
measureList = jsonData;
|
||||||
|
} catch (err) {
|
||||||
|
Logger.error("Unable to load custom Ping config: " + err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
async onCommand(command: string, args: any, message: Message) {
|
async onCommand(command: string, args: any, message: Message) {
|
||||||
if(command.toLowerCase() !== 'ping') return;
|
if (command.toLowerCase() !== 'ping') return;
|
||||||
await this.process(message, args);
|
await this.process(message, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
async interactionCreate(interaction: CommandInteraction) {
|
async interactionCreate(interaction: CommandInteraction) {
|
||||||
if(interaction.isCommand()) {
|
if (interaction.isCommand()) {
|
||||||
if(typeof interaction.commandName === 'undefined') return;
|
if (typeof interaction.commandName === 'undefined') return;
|
||||||
if((interaction.commandName).toLowerCase() !== 'ping') return;
|
if ((interaction.commandName).toLowerCase() !== 'ping') return;
|
||||||
await this.process(interaction, interaction.options);
|
await this.process(interaction, interaction.options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -70,7 +60,7 @@ export default class Ping {
|
|||||||
const isSlashCommand = data instanceof CommandInteraction && data.isCommand();
|
const isSlashCommand = data instanceof CommandInteraction && data.isCommand();
|
||||||
const isMessage = data instanceof Message;
|
const isMessage = data instanceof Message;
|
||||||
|
|
||||||
if(!isSlashCommand && !isMessage) return;
|
if (!isSlashCommand && !isMessage) return;
|
||||||
|
|
||||||
let placeholder;
|
let placeholder;
|
||||||
|
|
||||||
@@ -81,7 +71,7 @@ export default class Ping {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let beforeEditDate = Date.now();
|
let beforeEditDate = Date.now();
|
||||||
placeholder = await sendMessageOrInteractionResponse(data, { embeds:[loadingEmbed]} );
|
placeholder = await sendMessageOrInteractionResponse(data, { embeds: [loadingEmbed] });
|
||||||
let afterEditDate = Date.now();
|
let afterEditDate = Date.now();
|
||||||
|
|
||||||
|
|
||||||
@@ -89,19 +79,19 @@ export default class Ping {
|
|||||||
|
|
||||||
// TODO: Optimize speed of this
|
// TODO: Optimize speed of this
|
||||||
|
|
||||||
for(let entry of measureList) {
|
for (let entry of measureList) {
|
||||||
|
|
||||||
let stringCurrent = `${entry.title}`;
|
let stringCurrent = `${entry.title}`;
|
||||||
|
|
||||||
if(entry.type === MeasureType.Ping) {
|
if (entry.type === MeasureType.Ping) {
|
||||||
const res = await NodePing.promise.probe(entry.host!, { min_reply: 4 });
|
const res = await NodePing.promise.probe(entry.host!, { min_reply: 4 });
|
||||||
|
|
||||||
if(!res.alive) {
|
if (!res.alive) {
|
||||||
stringCurrent += "Failed";
|
stringCurrent += "Failed";
|
||||||
return desString.push(stringCurrent);
|
return desString.push(stringCurrent);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(res.avg === 'unknown' || res.min === 'unknown' || res.max === 'unknown') {
|
if (res.avg === 'unknown' || res.min === 'unknown' || res.max === 'unknown') {
|
||||||
stringCurrent += "Failed";
|
stringCurrent += "Failed";
|
||||||
return desString.push(stringCurrent);
|
return desString.push(stringCurrent);
|
||||||
}
|
}
|
||||||
@@ -121,8 +111,8 @@ export default class Ping {
|
|||||||
desString.push("");
|
desString.push("");
|
||||||
desString.push("💻 Running on " + `${os.hostname()}${Environment.get().NODE_ENV === "development" ? ' / Development Environment' : ''}`);
|
desString.push("💻 Running on " + `${os.hostname()}${Environment.get().NODE_ENV === "development" ? ' / Development Environment' : ''}`);
|
||||||
|
|
||||||
if(isSlashCommand) return await data.editReply({ embeds: [EMBEDS.PING_INFO(data, desString.join('\n'))] });
|
if (isSlashCommand) return await data.editReply({ embeds: [EMBEDS.PING_INFO(data, desString.join('\n'))] });
|
||||||
else if(typeof placeholder !== "undefined" && isMessage && placeholder instanceof Message) return await placeholder.edit({ embeds: [EMBEDS.PING_INFO(data, desString.join('\n'))] });
|
else if (typeof placeholder !== "undefined" && isMessage && placeholder instanceof Message) return await placeholder.edit({ embeds: [EMBEDS.PING_INFO(data, desString.join('\n'))] });
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ import { registerAllGuildsCommands, unregisterAllGuildsCommands } from "../../ut
|
|||||||
import Prisma from "../../providers/Prisma";
|
import Prisma from "../../providers/Prisma";
|
||||||
import Users from "../../services/Users";
|
import Users from "../../services/Users";
|
||||||
|
|
||||||
import {Promise, reject} from "bluebird";
|
import { Promise, reject } from "bluebird";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import Logger from "../../libs/Logger";
|
import Logger from "../../libs/Logger";
|
||||||
@@ -124,20 +124,21 @@ let Announcements = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fs.existsSync(path.join(process.cwd(), 'configs/ServiceAnnouncement.json'))) {
|
|
||||||
try {
|
|
||||||
const rawData = fs.readFileSync(path.join(process.cwd(), 'configs/ServiceAnnouncement.json'));
|
|
||||||
const jsonData = JSON.parse(rawData.toString());
|
|
||||||
Announcements = jsonData;
|
|
||||||
} catch (err) {
|
|
||||||
Logger.error("Unable to load custom ServiceAnnouncement config: " + err);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fs.writeFileSync(path.join(process.cwd(), 'configs/ServiceAnnouncement.json'), JSON.stringify(Announcements, null, 4), 'utf8');
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class InteractionManager {
|
export default class InteractionManager {
|
||||||
|
|
||||||
|
async init() {
|
||||||
|
|
||||||
|
if (fs.existsSync(path.join(process.cwd(), 'configs/ServiceAnnouncement.json'))) {
|
||||||
|
try {
|
||||||
|
const rawData = fs.readFileSync(path.join(process.cwd(), 'configs/ServiceAnnouncement.json'));
|
||||||
|
const jsonData = JSON.parse(rawData.toString());
|
||||||
|
Announcements = jsonData;
|
||||||
|
} catch (err) {
|
||||||
|
Logger.error("Unable to load custom ServiceAnnouncement config: " + err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async onCommand(command: string, args: any, message: Message) {
|
async onCommand(command: string, args: any, message: Message) {
|
||||||
if (command.toLowerCase() !== 'serviceannouncement') return;
|
if (command.toLowerCase() !== 'serviceannouncement') return;
|
||||||
await this.process(message, args);
|
await this.process(message, args);
|
||||||
@@ -215,7 +216,7 @@ export default class InteractionManager {
|
|||||||
toSend.set(Guild, channel);
|
toSend.set(Guild, channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
let placeholder = await sendMessage(data.channel!, undefined, { embeds:[EMBEDS.SENDING_SERVICE_ANNOUNCEMENT(data)] });
|
let placeholder = await sendMessage(data.channel!, undefined, { embeds: [EMBEDS.SENDING_SERVICE_ANNOUNCEMENT(data)] });
|
||||||
|
|
||||||
await Promise.map(toSend, element => {
|
await Promise.map(toSend, element => {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
@@ -223,14 +224,14 @@ export default class InteractionManager {
|
|||||||
let Channel: TextChannel = element[1];
|
let Channel: TextChannel = element[1];
|
||||||
try {
|
try {
|
||||||
const guildObject = DiscordProvider.client.guilds.cache.get(Guild.id);
|
const guildObject = DiscordProvider.client.guilds.cache.get(Guild.id);
|
||||||
if(!guildObject)
|
if (!guildObject)
|
||||||
throw new Error('Guild not found');
|
throw new Error('Guild not found');
|
||||||
Logger.info(`Sending Service Announcement to ${guildObject?.name} (${guildObject.id}) #${Channel.name} (${Channel.id})`);
|
Logger.info(`Sending Service Announcement to ${guildObject?.name} (${guildObject.id}) #${Channel.name} (${Channel.id})`);
|
||||||
await sendMessage(Channel, undefined, { embeds:[EMBEDS.MAKE_PAYLOAD(data, embed)] })
|
await sendMessage(Channel, undefined, { embeds: [EMBEDS.MAKE_PAYLOAD(data, embed)] })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
withError = true;
|
withError = true;
|
||||||
const guildObject = DiscordProvider.client.guilds.cache.get(Guild.id);
|
const guildObject = DiscordProvider.client.guilds.cache.get(Guild.id);
|
||||||
if(guildObject)
|
if (guildObject)
|
||||||
Logger.info(`Unable to send Service Announcement to ${guildObject?.name} (${guildObject.id}) #${Channel.name} (${Channel.id})`);
|
Logger.info(`Unable to send Service Announcement to ${guildObject?.name} (${guildObject.id}) #${Channel.name} (${Channel.id})`);
|
||||||
|
|
||||||
reject(err);
|
reject(err);
|
||||||
@@ -241,16 +242,16 @@ export default class InteractionManager {
|
|||||||
});
|
});
|
||||||
}, { concurrency: 2 });
|
}, { concurrency: 2 });
|
||||||
|
|
||||||
if(withError) {
|
if (withError) {
|
||||||
if(isMessage)
|
if (isMessage)
|
||||||
(placeholder as Message).edit({ embeds: [EMBEDS.SERVICE_ANNOUNCEMENT_SENT_WITH_ERRORS(data)] });
|
(placeholder as Message).edit({ embeds: [EMBEDS.SERVICE_ANNOUNCEMENT_SENT_WITH_ERRORS(data)] });
|
||||||
else if(isSlashCommand)
|
else if (isSlashCommand)
|
||||||
return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.SERVICE_ANNOUNCEMENT_SENT_WITH_ERRORS(data)] }, true);
|
return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.SERVICE_ANNOUNCEMENT_SENT_WITH_ERRORS(data)] }, true);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if(isMessage)
|
if (isMessage)
|
||||||
(placeholder as Message).edit({ embeds: [EMBEDS.SERVICE_ANNOUNCEMENT_SENT(data)] });
|
(placeholder as Message).edit({ embeds: [EMBEDS.SERVICE_ANNOUNCEMENT_SENT(data)] });
|
||||||
else if(isSlashCommand)
|
else if (isSlashCommand)
|
||||||
return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.SERVICE_ANNOUNCEMENT_SENT(data)] }, true);
|
return await sendMessageOrInteractionResponse(data, { embeds: [EMBEDS.SERVICE_ANNOUNCEMENT_SENT(data)] }, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user