それで...少し文脈を説明するために、私が何をしたかを説明する必要があります。モデレーション用の discord ボット (mellobot.net) を作成しましたが、コマンドについてサポートが必要です。実際のボット自体に -uptime コマンドを追加して、時間を DD HH MM で表示したいのですが、コマンド ラインがどのようなものになるかわかりません。 (Twitch ユーザーが Twitch ストリームに使用する NightBots !uptime のようなものが欲しいです) この苦境を助けてくれる discord.py オタクはいますか?
------------------------
よくわかりませんが、datetime モジュールを使用できるかもしれません。ボットが準備完了になったら、日付を取得して !upti を作成できます。次のようなコマンドを実行します:
@client.event
async def on_ready():
global startdate
startdate = datetime.now()
@client.command()
async def uptime(ctx):
now = datetime.now()
uptime = startdate - now
uptime = uptime.strftime('%d/%h/%M')# I don't remember the date's well, might be the lowercases are false.
await ctx.send(f'Uptime: {uptime}')
------------------------
プログラムの開始時間を保存し、計算してください。 on_ready() イベントは、ボットの稼働中に複数回発生する可能性があり、また発生します。イベント内でメッセージを出力する以上のことを行うと、通常、悪いことが起こります。
from datetime import datetime
bot = commands.Bot(command_prefix='>')
bot.launch_time = datetime.utcnow()
@bot.command()
async def uptime(ctx):
delta_uptime = datetime.utcnow() - bot.launch_time
hours, remainder = divmod(int(delta_uptime.total_seconds()), 3600)
minutes, seconds = divmod(remainder, 60)
days, hours = divmod(hours, 24)
await ctx.send(f"{days}d, {hours}h, {minutes}m")
------------------------
これは discord.py 書き換え用の最新の作業コマンドです
@commands.command()
async def uptime(self, ctx):
delta_uptime = datetime.datetime.utcnow() - self.bot.launch_time
hours, remainder = divmod(int(delta_uptime.total_seconds()), 3600)
minutes, seconds = divmod(remainder, 60)
days, hours = divmod(hours, 24)
e = discord.Embed(title=f"I've been up for {days}d, {hours}h, {minutes}m, {seconds}s,", color=discord.Color.green())
await ctx.send(embed=e)```
共有
この回答を改善します
フォローする
2021 年 3 月 18 日 11:25 に回答
シタンシュ シュレスタ
シタンシュ シュレスタ
1