mirror of
https://github.com/kirameki-cafe/Yumi.git
synced 2026-09-13 10:49:20 +00:00
Changed to use init function instead
This commit is contained in:
+33
-43
@@ -14,31 +14,7 @@ enum MeasureType {
|
||||
DiscordWebsocket = "discordwebsocket"
|
||||
}
|
||||
|
||||
let measureList = [
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
let measureList: any = [];
|
||||
|
||||
const EMBEDS = {
|
||||
PING_INFO: (data: Message | Interaction, description: string) => {
|
||||
@@ -53,15 +29,29 @@ const EMBEDS = {
|
||||
|
||||
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) {
|
||||
if(command.toLowerCase() !== 'ping') return;
|
||||
if (command.toLowerCase() !== 'ping') return;
|
||||
await this.process(message, args);
|
||||
}
|
||||
|
||||
async interactionCreate(interaction: CommandInteraction) {
|
||||
if(interaction.isCommand()) {
|
||||
if(typeof interaction.commandName === 'undefined') return;
|
||||
if((interaction.commandName).toLowerCase() !== 'ping') return;
|
||||
async interactionCreate(interaction: CommandInteraction) {
|
||||
if (interaction.isCommand()) {
|
||||
if (typeof interaction.commandName === 'undefined') return;
|
||||
if ((interaction.commandName).toLowerCase() !== 'ping') return;
|
||||
await this.process(interaction, interaction.options);
|
||||
}
|
||||
}
|
||||
@@ -70,7 +60,7 @@ export default class Ping {
|
||||
const isSlashCommand = data instanceof CommandInteraction && data.isCommand();
|
||||
const isMessage = data instanceof Message;
|
||||
|
||||
if(!isSlashCommand && !isMessage) return;
|
||||
if (!isSlashCommand && !isMessage) return;
|
||||
|
||||
let placeholder;
|
||||
|
||||
@@ -81,7 +71,7 @@ export default class Ping {
|
||||
});
|
||||
|
||||
let beforeEditDate = Date.now();
|
||||
placeholder = await sendMessageOrInteractionResponse(data, { embeds:[loadingEmbed]} );
|
||||
placeholder = await sendMessageOrInteractionResponse(data, { embeds: [loadingEmbed] });
|
||||
let afterEditDate = Date.now();
|
||||
|
||||
|
||||
@@ -89,23 +79,23 @@ export default class Ping {
|
||||
|
||||
// TODO: Optimize speed of this
|
||||
|
||||
for(let entry of measureList) {
|
||||
for (let entry of measureList) {
|
||||
|
||||
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 });
|
||||
|
||||
if(!res.alive) {
|
||||
|
||||
if (!res.alive) {
|
||||
stringCurrent += "Failed";
|
||||
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";
|
||||
return desString.push(stringCurrent);
|
||||
}
|
||||
|
||||
|
||||
stringCurrent += `${parseFloat(res.avg).toFixed(1)}ms (${parseFloat(res.min).toFixed(1)}ms - ${parseFloat(res.max).toFixed(1)}ms)`;
|
||||
desString.push(stringCurrent);
|
||||
} else if (entry.type === MeasureType.DiscordWebsocket) {
|
||||
@@ -114,15 +104,15 @@ export default class Ping {
|
||||
} else if (entry.type === MeasureType.DiscordHTTPPing) {
|
||||
stringCurrent += `${Math.abs(afterEditDate - beforeEditDate).toFixed(1)}ms`;
|
||||
desString.push(stringCurrent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
desString.push("");
|
||||
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'))] });
|
||||
else if(typeof placeholder !== "undefined" && isMessage && placeholder instanceof Message) return await placeholder.edit({ 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'))] });
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import { registerAllGuildsCommands, unregisterAllGuildsCommands } from "../../ut
|
||||
import Prisma from "../../providers/Prisma";
|
||||
import Users from "../../services/Users";
|
||||
|
||||
import {Promise, reject} from "bluebird";
|
||||
import { Promise, reject } from "bluebird";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
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 {
|
||||
|
||||
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) {
|
||||
if (command.toLowerCase() !== 'serviceannouncement') return;
|
||||
await this.process(message, args);
|
||||
@@ -215,7 +216,7 @@ export default class InteractionManager {
|
||||
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 => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
@@ -223,14 +224,14 @@ export default class InteractionManager {
|
||||
let Channel: TextChannel = element[1];
|
||||
try {
|
||||
const guildObject = DiscordProvider.client.guilds.cache.get(Guild.id);
|
||||
if(!guildObject)
|
||||
if (!guildObject)
|
||||
throw new Error('Guild not found');
|
||||
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) {
|
||||
withError = true;
|
||||
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})`);
|
||||
|
||||
reject(err);
|
||||
@@ -241,16 +242,16 @@ export default class InteractionManager {
|
||||
});
|
||||
}, { concurrency: 2 });
|
||||
|
||||
if(withError) {
|
||||
if(isMessage)
|
||||
if (withError) {
|
||||
if (isMessage)
|
||||
(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);
|
||||
}
|
||||
else {
|
||||
if(isMessage)
|
||||
if (isMessage)
|
||||
(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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user