Reputation: 11
So... To give you a bit of context, I need to tell you what I have done. I have created a discord bot for moderation (mellobot.net) and I need help with a command. I would like to put an -uptime command for the actual bot itself to display the time in DD HH MM, but have no idea what the command line would look like. (I want something like NightBots !uptime that twitch users use for twitch streams) Are there any discord.py nerds that would help me with this predicament?
Upvotes: 1
Views: 2338
Reputation: 1
This is the latest workign command for discord.py re-write
@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)```
Upvotes: 0
Reputation: 2041
Store the time your program started, then, do some math. The on_ready()
event can and will fire multiple times during the uptime of a bot and bad things usually happen if you do anything more than print a message inside it.
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")
Upvotes: 1
Reputation: 4743
I'm not sure but maybe you can use datetime
module. When the bot is on_ready
, you can take the date, and make a !uptime
command like this:
@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}')
Upvotes: 1