LoginSignup
4
1

More than 1 year has passed since last update.

discord.jsでURLの安全性チェックコマンドを作る

Last updated at Posted at 2021-01-08

今回作るもの

1.discord 上で URL 解析を行うコマンドを実行
2.メッセージから URL を取り出す
3.node-fetch を利用して NortonSafeWeb でサイトの安全性をチェック
4.結果をサイトの HTML から取得
5.1が送信されたチャンネルに結果を送信

必要なもの

・node.js => node.js
・discord.js => npm i discord.js
・@replit/node-fetch => npm i @replit/node-fetch

コード

const discord = require("discord.js");
const fetch = require("@replit/node-fetch");
const client = new discord.Client({
  intents: [
    discord.GatewayIntentBits.Guilds,
    discord.GatewayIntentBits.GuildMessages,
    discord.GatewayIntentBits.MessageContent,
  ],
});
const prefix = "!";

client.on(discord.Events.MessageCreate, async (message) => {
  if (message.author.bot) return;
  if (!message.content.startsWith(prefix)) return; //ボットのプレフィックスからメッセージが始まっているか確認
  const args = message.content.slice(prefix.length).trim().split(/ +/g);
  const command = args.shift().toLowerCase();
  if (command === "url") {
    //コマンド名
    try {
      const url = args[0]; //URLを取得
      if (!url) return message.channel.send("エラー : URLを指定して下さい。");
      fetch(
        `https://safeweb.norton.com/report/show?url=${encodeURI(url)}&ulang=jpn`
      )
        .then((res) => res.text())
        .then((norton) => {
          //NortonSafeWebにアクセス
          if (norton.indexOf("安全性") != -1) {
            message.channel.send({
              embeds: [
                {
                  title: "結果は安全です。",
                  description: `ノートン セーフウェブが ${url} を分析して安全性とセキュリティの問題を調べました。`,
                  footer: {
                    text: "Powered by Norton Safeweb",
                  },
                  color: 0xffd700,
                },
              ],
            });
          } else if (norton.indexOf("[注意]") != -1) {
            message.channel.send({
              embeds: [
                {
                  title: "結果は注意です。",
                  description: `[注意]の評価を受けた Web サイトは少数の脅威または迷惑を伴いますが、赤色の[警告]に相当するほど危険とは見なされません。サイトにアクセスする場合には注意が必要です。`,
                  footer: {
                    text: "Powered by Norton Safeweb",
                  },
                  color: 0xffd700,
                },
              ],
            });
          } else if (norton.indexOf("警告") != -1) {
            message.channel.send({
              embeds: [
                {
                  title: "結果は警告です。",
                  description: `これは既知の危険な Web ページです。このページを表示**しない**ことを推奨します。`,
                  footer: {
                    text: "Powered by Norton Safeweb",
                  },
                  color: 0xffd700,
                },
              ],
            });
          } else {
            message.channel.send({
              embeds: [
                {
                  title: "結果は未評価です。",
                  description: `このサイトはまだ評価されていません。`,
                  footer: {
                    text: "Powered by Norton Safeweb",
                  },
                  color: 0xffd700,
                },
              ],
            });
          }
        });
    } catch (err) {
      message.channel.send("エラー : 解析中にエラーが発生しました。");
    }
  }
});

使い方

!url <解析したいURL>
で利用することができます!

最後に

初めての投稿なのでおかしいところや、わかりにくいところがあればコメントで教えて頂きたいです!

4
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1