1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

24時間動くdiscord BOT を作ろう!

Last updated at Posted at 2025-04-01

概要

本記事は、webアプリをデプロイし、かつ、discordのBOTを24時間動作させる内容となっています。

この記事は、前回の続きとなっています。
前回記事「100MBまでの動画をdiscordに送信できるBOTを作る」も是非読んでください!!
https://qiita.com/yonuma28/items/3a49659774c408367e58

webアプリのデプロイとdiscord BOTの起動はrender上で行っています。renderの詳細は下記記事をご覧ください。
https://qiita.com/tks_00/items/73cf04c225e5f6113cd8

render と UptimeRobot

今回使用しているrenderですが、フリープランですと画面を閉じると一定時間でサービスがスリープ状態に入ってしまいます。そのため、定期的にrenderにリクエストを送ることでスリープ状態を回避します。
定期的なリクエストは、UptimeRobotというサービスを使用します。
https://dashboard.uptimerobot.com

やり方

では、どのようにして24時間BOTを動作させるのかを見ていきましょう。
まずは、エンドポイントを作成します。

@app.route('/keep_alive', methods=['GET'])
def keep_alive():
    return jsonify({'message': 'Alive'}), 200

これにより、URL/keep_aliveにてHTTP/GET リクエストを受け取ることが出来ます。
次に、このエンドポイントを利用して、外部から定期的にリクエストを送信する仕組みを作ります。今回は、UptimeRobotを使用して指定した間隔でこのエンドポイントにリクエストを送信します。これにより、サーバーがアイドル状態にならず、常に稼働し続けることが可能になります。

UptimeRobot

image.png

ログインしたら、New monitorをクリックします。すると、以下の画面が開きますので、作成したエンドポイントを入力したら完了です。これで、5分おきに /keep_alive にリクエストが入り、BOTを24時間起動することが出来ます。
image.png

入力するURLは、
image.png
ここで表示されているURLの後ろに、/keep_alive を付けたものになります。

終わりに

簡単にdiscord BOTを24時間動作させる方法をご紹介しました。良き開発ライフを。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?