Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #53 from takejohn/dev/refactoring
Browse files Browse the repository at this point in the history
自動整形ツール・リファクタリング
  • Loading branch information
ringo360 authored Mar 2, 2024
2 parents 9899408 + e95e70c commit 5548457
Show file tree
Hide file tree
Showing 61 changed files with 12,149 additions and 11,162 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.md
*.yml
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"useTabs": true
}
28 changes: 13 additions & 15 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
{
// IntelliSense を使用して利用可能な属性を学べます。
// 既存の属性の説明をホバーして表示します。
// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "プログラムの起動",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\discordbot.js"
}
]
// IntelliSense を使用して利用可能な属性を学べます。
// 既存の属性の説明をホバーして表示します。
// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "プログラムの起動",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}\\discordbot.js"
}
]
}
112 changes: 64 additions & 48 deletions commands/checkping.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,66 @@
const { SlashCommandBuilder, CommandInteraction } = require('discord.js');
const { LANG, strFormat } = require('../util/languages');
const { CheckHostRequest, CHECK_PING, CheckPingOk, isValidHostname } = require('../util/check-host');
const { formatTable } = require('../util/strings');
const { SlashCommandBuilder } = require("discord.js");
const { LANG, strFormat } = require("../util/languages");
const {
CheckHostRequest,
CHECK_PING,
CheckPingOk,
isValidHostname,
} = require("../util/check-host");
const { formatTable } = require("../util/strings");

module.exports = {
data: new SlashCommandBuilder()
.setName(LANG.commands.checkping.name)
.setDescription(LANG.commands.checkping.description)
.addStringOption(option => (
option
.setName(LANG.commands.checkping.options.ip.name)
.setDescription(LANG.common.optionDescription.ipAddress)
.setRequired(true)
)),
execute: async function (/** @type {CommandInteraction} */ interaction) {
let url = interaction.options.getString(LANG.commands.checkping.options.ip.name);
if (!isValidHostname(url)) {
// IPアドレスが間違っています。(IPv4、またはドメインのみ対応しています。
return await interaction.reply(LANG.commands.checkping.invalidIpError);
}
const request = await CheckHostRequest.get(CHECK_PING, url, 40);
const msg = await interaction.reply(LANG.common.message.checking);
const resultMap = await request.checkResult(1.0, 7);
const table = [...resultMap.entries()].map(([node, result]) => {
const nodeName = node.name.replace(".node.check-host.net", "");
const prefix = `[${nodeName}]`;
console.log(strFormat(LANG.common.message.dataFor, [nodeName]), result);
if (result instanceof CheckPingOk) {
const values = result.values;
const average = values.reduce((a, { ping: b }) => a + b, 0) / values.length;
return [
prefix, values[3].reply + ',', values[3].ping, '/', values[2].reply + ',', values[2].ping,
'| Ping:', `${Math.floor(average * 1000)} ms`
];
}
return [prefix, result.state];
});
const str = formatTable(table, {
align: ['left', 'left', 'left', 'left', 'left', 'left', 'left', 'right']
});
msg.edit({
content: LANG.common.message.result,
files: [{
attachment: Buffer.from(str),
name: "result.txt"
}]
})
}
};
data: new SlashCommandBuilder()
.setName(LANG.commands.checkping.name)
.setDescription(LANG.commands.checkping.description)
.addStringOption((option) =>
option
.setName(LANG.commands.checkping.options.ip.name)
.setDescription(LANG.common.optionDescription.ipAddress)
.setRequired(true),
),
execute: async function (/** @type {CommandInteraction} */ interaction) {
const url = interaction.options.getString(
LANG.commands.checkping.options.ip.name,
);
if (!isValidHostname(url)) {
// IPアドレスが間違っています。(IPv4、またはドメインのみ対応しています。
return await interaction.reply(LANG.commands.checkping.invalidIpError);
}
const request = await CheckHostRequest.get(CHECK_PING, url, 40);
const msg = await interaction.reply(LANG.common.message.checking);
const resultMap = await request.checkResult(1.0, 7);
const table = [...resultMap.entries()].map(([node, result]) => {
const nodeName = node.name.replace(".node.check-host.net", "");
const prefix = `[${nodeName}]`;
console.log(strFormat(LANG.common.message.dataFor, [nodeName]), result);
if (result instanceof CheckPingOk) {
const values = result.values;
const average =
values.reduce((a, { ping: b }) => a + b, 0) / values.length;
return [
prefix,
values[3].reply + ",",
values[3].ping,
"/",
values[2].reply + ",",
values[2].ping,
"| Ping:",
`${Math.floor(average * 1000)} ms`,
];
}
return [prefix, result.state];
});
const str = formatTable(table, {
align: ["left", "left", "left", "left", "left", "left", "left", "right"],
});
msg.edit({
content: LANG.common.message.result,
files: [
{
attachment: Buffer.from(str),
name: "result.txt",
},
],
});
},
};
106 changes: 61 additions & 45 deletions commands/checktcp.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,62 @@
const { SlashCommandBuilder } = require('discord.js');
const { LANG, strFormat } = require('../util/languages');
const { CheckHostRequest, CHECK_TCP, CheckTcpOk, CheckTcpError, isValidHostname } = require('../util/check-host');
const { formatTable } = require('../util/strings');
const { SlashCommandBuilder } = require("discord.js");
const { LANG, strFormat } = require("../util/languages");
const {
CheckHostRequest,
CHECK_TCP,
CheckTcpOk,
CheckTcpError,
isValidHostname,
} = require("../util/check-host");
const { formatTable } = require("../util/strings");
module.exports = {
data: new SlashCommandBuilder()
.setName(LANG.commands.checktcp.name)
.setDescription(LANG.commands.checktcp.description)
.addStringOption(option => (
option
.setName(LANG.commands.checktcp.options.ip.name)
.setDescription(LANG.common.optionDescription.ipAddress)
.setRequired(true)
)),
execute: async function (interaction) {
const url = interaction.options.getString(LANG.commands.checktcp.options.ip.name);
if (!isValidHostname(url)) {
return await interaction.reply(LANG.commands.checktcp.invalidUrlError);
}
const request = await CheckHostRequest.get(CHECK_TCP, url, 40);
const msg = await interaction.reply(LANG.common.message.checking);
const resultMap = await request.checkResult(1.0, 7);
const table = [...resultMap.entries()].map(([node, result]) => {
const nodeName = node.name.replace(".node.check-host.net", "");
const prefix = `[${nodeName}]`;
console.log(strFormat(LANG.common.message.dataFor, [nodeName]), result);
if (result instanceof CheckTcpOk) {
return [prefix, 'OK,', result.time, '| Ping: ', `${Math.floor(result.time * 1000)} ms`];
}
if (result instanceof CheckTcpError) {
return [prefix, 'ERROR', result.description];
}
return [prefix, result.state];
});
const str = formatTable(table, {
align: ['left', 'left', 'left', 'left', 'right']
});
msg.edit({
content: LANG.common.message.result,
files: [{
attachment: Buffer.from(str),
name: "result.txt"
}]
});
}
};
data: new SlashCommandBuilder()
.setName(LANG.commands.checktcp.name)
.setDescription(LANG.commands.checktcp.description)
.addStringOption((option) =>
option
.setName(LANG.commands.checktcp.options.ip.name)
.setDescription(LANG.common.optionDescription.ipAddress)
.setRequired(true),
),
execute: async function (interaction) {
const url = interaction.options.getString(
LANG.commands.checktcp.options.ip.name,
);
if (!isValidHostname(url)) {
return await interaction.reply(LANG.commands.checktcp.invalidUrlError);
}
const request = await CheckHostRequest.get(CHECK_TCP, url, 40);
const msg = await interaction.reply(LANG.common.message.checking);
const resultMap = await request.checkResult(1.0, 7);
const table = [...resultMap.entries()].map(([node, result]) => {
const nodeName = node.name.replace(".node.check-host.net", "");
const prefix = `[${nodeName}]`;
console.log(strFormat(LANG.common.message.dataFor, [nodeName]), result);
if (result instanceof CheckTcpOk) {
return [
prefix,
"OK,",
result.time,
"| Ping: ",
`${Math.floor(result.time * 1000)} ms`,
];
}
if (result instanceof CheckTcpError) {
return [prefix, "ERROR", result.description];
}
return [prefix, result.state];
});
const str = formatTable(table, {
align: ["left", "left", "left", "left", "right"],
});
msg.edit({
content: LANG.common.message.result,
files: [
{
attachment: Buffer.from(str),
name: "result.txt",
},
],
});
},
};
Loading

0 comments on commit 5548457

Please sign in to comment.