mirror of
https://github.com/neoarz/Syntrel.git
synced 2025-12-25 03:40:11 +01:00
feat(uptime): add uptime monitor with refreshes
Introduces an uptime command and view to show how long the bot has been running. Adds a method to calculate uptime in bot.py, registers the command in help.py, and implements the command logic in a new uptime.py cog with a refresh button.
This commit is contained in:
18
bot.py
18
bot.py
@@ -4,6 +4,7 @@ import os
|
|||||||
import platform
|
import platform
|
||||||
import random
|
import random
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
import aiosqlite
|
import aiosqlite
|
||||||
import discord
|
import discord
|
||||||
@@ -125,6 +126,7 @@ class DiscordBot(commands.Bot):
|
|||||||
self.database = None
|
self.database = None
|
||||||
self.bot_prefix = os.getenv("PREFIX")
|
self.bot_prefix = os.getenv("PREFIX")
|
||||||
self.invite_link = os.getenv("INVITE_LINK")
|
self.invite_link = os.getenv("INVITE_LINK")
|
||||||
|
self.start_time = time.time()
|
||||||
|
|
||||||
async def init_db(self) -> None:
|
async def init_db(self) -> None:
|
||||||
async with aiosqlite.connect(
|
async with aiosqlite.connect(
|
||||||
@@ -225,6 +227,22 @@ class DiscordBot(commands.Bot):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_uptime(self) -> str:
|
||||||
|
uptime_seconds = int(time.time() - self.start_time)
|
||||||
|
days = uptime_seconds // 86400
|
||||||
|
hours = (uptime_seconds % 86400) // 3600
|
||||||
|
minutes = (uptime_seconds % 3600) // 60
|
||||||
|
seconds = uptime_seconds % 60
|
||||||
|
|
||||||
|
if days > 0:
|
||||||
|
return f"{days}d {hours}h {minutes}m {seconds}s"
|
||||||
|
elif hours > 0:
|
||||||
|
return f"{hours}h {minutes}m {seconds}s"
|
||||||
|
elif minutes > 0:
|
||||||
|
return f"{minutes}m {seconds}s"
|
||||||
|
else:
|
||||||
|
return f"{seconds}s"
|
||||||
|
|
||||||
async def on_message(self, message: discord.Message) -> None:
|
async def on_message(self, message: discord.Message) -> None:
|
||||||
"""
|
"""
|
||||||
The code in this event is executed every time someone sends a message, with or without the prefix
|
The code in this event is executed every time someone sends a message, with or without the prefix
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ class Help(commands.Cog, name="help"):
|
|||||||
"serverinfo": "general",
|
"serverinfo": "general",
|
||||||
"ping": "general",
|
"ping": "general",
|
||||||
"feedback": "general",
|
"feedback": "general",
|
||||||
|
"uptime": "general",
|
||||||
# "context_menus": "general",
|
# "context_menus": "general",
|
||||||
|
|
||||||
# Fun Commands
|
# Fun Commands
|
||||||
|
|||||||
54
cogs/general/uptime.py
Normal file
54
cogs/general/uptime.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import discord
|
||||||
|
from discord.ext import commands
|
||||||
|
from discord.ext.commands import Context
|
||||||
|
|
||||||
|
|
||||||
|
class UptimeView(discord.ui.View):
|
||||||
|
def __init__(self, bot):
|
||||||
|
super().__init__(timeout=300)
|
||||||
|
self.bot = bot
|
||||||
|
|
||||||
|
@discord.ui.button(emoji="<:RefreshEmoji:1418934990770802891>", style=discord.ButtonStyle.primary)
|
||||||
|
async def refresh_button(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||||
|
embed = discord.Embed(
|
||||||
|
title="Bot Uptime",
|
||||||
|
description=f"The bot has been running for {self.bot.get_uptime()}",
|
||||||
|
color=0x7289DA,
|
||||||
|
)
|
||||||
|
embed.set_author(name="Uptime", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
|
||||||
|
await interaction.response.edit_message(embed=embed, view=self)
|
||||||
|
|
||||||
|
|
||||||
|
class Uptime(commands.Cog, name="uptime"):
|
||||||
|
def __init__(self, bot) -> None:
|
||||||
|
self.bot = bot
|
||||||
|
|
||||||
|
@commands.hybrid_command(
|
||||||
|
name="uptime",
|
||||||
|
description="Check how long the bot has been running.",
|
||||||
|
)
|
||||||
|
async def uptime(self, context: Context) -> None:
|
||||||
|
"""
|
||||||
|
Check how long the bot has been running.
|
||||||
|
|
||||||
|
:param context: The hybrid command context.
|
||||||
|
"""
|
||||||
|
embed = discord.Embed(
|
||||||
|
title="Bot Uptime",
|
||||||
|
description=f"The bot has been running for **{self.bot.get_uptime()}**",
|
||||||
|
color=0x7289DA,
|
||||||
|
)
|
||||||
|
embed.set_author(name="Uptime", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
|
||||||
|
view = UptimeView(self.bot)
|
||||||
|
if getattr(context, "interaction", None):
|
||||||
|
inter = context.interaction
|
||||||
|
if not inter.response.is_done():
|
||||||
|
await inter.response.send_message(embed=embed, view=view, ephemeral=True)
|
||||||
|
else:
|
||||||
|
await inter.followup.send(embed=embed, view=view, ephemeral=True)
|
||||||
|
else:
|
||||||
|
await context.send(embed=embed, view=view)
|
||||||
|
|
||||||
|
|
||||||
|
async def setup(bot) -> None:
|
||||||
|
await bot.add_cog(Uptime(bot))
|
||||||
Reference in New Issue
Block a user