0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

discord.js 身内鯖用スパム対策したい

Posted at

事の経緯。

鯖内メンバーがパスワード流失(詳細は分かりません)しました。
鯖内すべてのチャンネルにリンクが張られるスパムが発生したので、今宵対策ですが、要件定義などがうまくいっていません。

投稿自体は初めてです。見にくいかと思いますがご了承お願いします。
更新の可能性があります。

const spamCount = 4 ;
const spamCheck = [];

client.on('message', async message => {
const spamInterval = 3000; //連投の間隔

if (message.mentions.everyone && message.author.bot == false ){ {
   spamCheck.push(message.author.id) 
   setTimeout(spamCheck.shift, spamInterval)
  

    if (spamCheck.filter(n => n === message.author.id).length >= spamCount) {
     
      const userToKick = message.author;
      client.users.get(userToKick.id).send('スパム検出されたのでキックの処理になりました。');
      

      // ユーザーをキックします
   try {
      await message.guild.member(userToKick).kick();
      message.channel.send(`スパムの疑いがあるため、${userToKick.username} をサーバーからキックしました。`);
    } catch (error) {
      console.error(error);
      message.channel.send('ユーザーをキックするのに失敗しました。');
    }
  

      spamCheck = [];

  }

  
   }
  
});

上から見ていきます。

スパムの簡単な要件定義。

  • 今回と類似のスパムを止めるだけ
  • 「3sの間に4回eveyoneメンションでスパム」とします
  • everyoneした&& !botで
    • 送信者のidを配列にpush
    • setTimeout(処理,開始時間)で
const spamCount = 4 ;
const spamCheck = [];

client.on('message', async message => {
const spamInterval = 3000;


if (message.mentions.everyone && message.author.bot == false ){ {
   spamCheck.push(message.author.id) 
   setTimeout(spamCheck.shift, spamInterval)
  
  • spamCheck配列をフィルタリング
  • message.author.idと一致する要素だけを取出
    • 特定のユーザーがこれまでに何回メッセージを送信したか確認
    • (.length >= spamCount)spamCount以上であるか。今回は(3sに)4回
  • メッセージ送信者のidを格納しDMにメッセージを送信

 if (spamcheck.filter(n => n === message.author.id).length >= spamCount) {
     
      const userToKick = message.author;
      client.users.get(userToKick.id).send('スパム検出されたのでキックの処理になりました。');

  • 送信者をkick
  • 鯖内でのメッセージ(ここは特定のチャンネルにsendでもいいと思う。応急処置で今はこの記述)
  • エラーの場合の処理。botの権限を確認してね
  • id持っておくのは嫌なので初期化

 // ユーザーをキックします
   try {
      await message.guild.member(userToKick).kick();
      message.channel.send(`スパムの疑いがあるため、${userToKick.username} をサーバーからキックしました。`);
    } catch (error) {
      console.error(error);
      message.channel.send('ユーザーをキックするのに失敗しました。');
    }
  
      spamCheck = [];

  }  
}
  
});
      

優先度高め課題

  • スパム投稿を消す。
  • 誤kickの場合のどうしよう
  • 鯖でkickの基準を説明していいのか。(投稿してるだろ)
  • everyoneがないと対策ができない
    • テスト環境の都合でイメージついていない
  • 休日もコードを書くとなぜか体調を崩す

優先度低すぎ課題

  • hereや個別の人へのメンションでの嫌がらせは対応しにくい。
    • ここに関しては招待側の自助努力と信頼でと考えている。

誰かの助けになれば幸いです。
またスパム要件定義等コメント待っています

◆参考
スパム対策をするサンプル

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?