1
私はPythonで不協和音を作っています。私はそれをウェブサイトにpingして、それが上か下かを伝えようとしています。それがループを実行していないいくつかの理由Pythonで不協和音のボットを使ってウェブサイトにpingをかける
import logging
logging.basicConfig(level=logging.INFO)
import discord
from discord.ext import commands
import os
website = "mywebsite.com"
des = "a website status bot"
prefix = "."
client = commands.Bot(description=des, command_prefix=prefix)
async def my_background_task():
await client.wait_until_ready()
channel = discord.Object(id='my server id went here')
response = 0
while not client.is_closed:
print("loop") #debugger
await asyncio.sleep(10)
global response
pr = response
response = os.system("ping -c 1 " + website)
if response != 0:
response = 1
if pr != response:
if response == 0:
await client.send_message(channel, 'mywebsite is up!')
else:
await client.send_message(channel, 'mywebsite is down!')
client.run("discord-bot-code-went-here")
: はここに私のコードです。何か案は?
ありがとうございました。
サイドノート:ボットは、それとping pongコマンドを実行しようとしたときにうまくいったので、プログラムを実行するときにエラーも出なかった。
おかげ@squaswin、それはまだループを実行するように見える...私はon_readyトリガーにそれを置くことによって自分のコードが動作して得ることができましたしませんが、私はまだそれが働いてみたいですバックグラウンドタスクとして。アイデア:コードはループを作成していますが、クライアントにそれを実行するように指示していますか? –
あなたの編集したコードをあなたの質問に表示できますか? – xNinjaKittyx
タスクを作成することで、可能であればコードを実行するようにasyncioに依頼します。コードがasyncioによってバックグラウンドタスクとして実行されているので、クライアントが接続されている間、whileループが捕捉されない例外をスローしない限り、クライアントはそれを実行します。 – squaswin